从网上搜罗得到一个创建快捷方式的解决方案,对其进行整理并写成可独立使用的函数:
// Create shortcut
// @param lpszSource LPCTSTR Source file name or file path
// @param lpszShortcut LPCTSTR Shortcut name, e.g. Notepad.lnk
BOOL CreateShortcut(LPCTSTR lpszSource, LPCTSTR lpszShortcut)
{
HRESULT hr;
IShellLink* psl = NULL;
IPersistFile* ppf = NULL;
WORD wsz[MAX_PATH];
BOOL bInitCom = FALSE;
BOOL bRet = FALSE;
do {
hr = CoCreateInstance(CLSID_ShellLink, NULL ,CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl);
if (!bInitCom && FAILED(hr))
{
bInitCom = TRUE;
hr = CoInitialize(NULL);
}
else
{
break;
}
} while(SUCCEEDED(hr));
if(SUCCEEDED(hr))
{
psl->SetPath(lpszSource);
hr = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
if(SUCCEEDED(hr))
{
MultiByteToWideChar(CP_ACP, 0, lpszShortcut, -1, wsz, MAX_PATH);
bRet = SUCCEEDED(ppf->Save(wsz, STGM_READWRITE));
ppf->Release();
}
psl->Release();
}
if (bInitCom)
{
CoUninitialize();
}
return bRet;
}