PFNSHGETFOLDERPATHA GetFuncPtr_SHGetFolderPathA()
{
static HMODULE hMod = NULL;
PFNSHGETFOLDERPATHA pSHGetFolderPath = NULL;
// Load SHFolder.dll only once
if (!hMod)
hMod = LoadLibrary("SHFolder.dll");
// Obtain a pointer to the SHGetFolderPathA function
if (hMod)
pSHGetFolderPath = (PFNSHGETFOLDERPATHA)GetProcAddress(hMod, "SHGetFolderPathA");
return pSHGetFolderPath;
}
/********************************************************************/
/* */
/* Function name : CreateStartMenuShortcut */
/* Description : Create a shortcut using the IShellLink interface.*/
/* */
/********************************************************************/
HRESULT CreateStartMenuShortcut(LPSTR lpszShortcutFile, LPSTR lpszDescription, LPTSTR lpszRelativeFolder)
{
HRESULT hr;
PFNSHGETFOLDERPATHA pSHGetFolderPath = NULL;
TCHAR lpszLink[MAX_PATH];
BOOL bFound = FALSE;
pSHGetFolderPath = GetFuncPtr_SHGetFolderPathA();
// Find the current user's Start Menu Programs folder
if (pSHGetFolderPath)
bFound = SUCCEEDED(pSHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL, 0, lpszLink));
if (bFound)
{
// Proceed to create the shortcut
IShellLink *pIShellLink = NULL;
IPersistFile *ppf = NULL;
WCHAR pLinkUnicode[MAX_PATH];
CoInitialize(NULL);
// Get a pointer to the IShellLink interface.
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (void **)&pIShellLink);
if (SUCCEEDED(hr))
{
// Get a pointer to IPersistFile interface for saving shortcut
hr = pIShellLink->QueryInterface(IID_IPersistFile, (void **)&ppf);
if (SUCCEEDED(hr))
{
hr = pIShellLink->SetPath(lpszShortcutFile);
hr = pIShellLink->SetDescription(lpszDescription);
if (SUCCEEDED(hr))
{
// Add the target folder to the Start Menu Programs path
lstrcat(lpszLink, "//");
lstrcat(lpszLink, lpszRelativeFolder);
lstrcat(lpszLink, "//");
// Create the directory if it does not exist
CreateDirectory(lpszLink,NULL);
// Add the file name for the shortcut
lstrcat(lpszLink, lpszDescription);
lstrcat(lpszLink, ".lnk");
// Convert string to Unicode, and call IPersistFile::Save()
MultiByteToWideChar(CP_ACP, 0, lpszLink, -1, pLinkUnicode, MAX_PATH);
hr = ppf->Save(pLinkUnicode, TRUE);
}
ppf->Release();
}
pIShellLink->Release();
}
CoUninitialize();
}
return hr;
}