1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
/**********************************************************************
* Function......: CreateShortcut
* Parameters....: lpszFileName - string that specifies a valid file name
* lpszDesc - string that specifies a description for a
shortcut
* lpszShortcutPath - string that specifies a path and
file name of a shortcut
* Returns.......: S_OK on success, error code on failure
* Description...: Creates a Shell link object (shortcut)
**********************************************************************/
HRESULT CreateShortcut(/*in*/ LPCTSTR lpszFileName,
/*in*/ LPCTSTR lpszDesc,
/*in*/ LPCTSTR lpszShortcutPath,
/*in*/ LPCTSTR lpszArguments,
/*in*/ int iShowCmd
)
{
//::CoInitialize(NULL);
HRESULT hRes = E_FAIL;
DWORD dwRet = 0;
ATL::CComPtr<IShellLink> ipShellLink;
// buffer that receives the null-terminated string
// for the drive and path
TCHAR szPath[MAX_PATH];
// buffer that receives the address of the final
//file name component in the path
LPTSTR lpszFilePart;
WCHAR wszTemp[MAX_PATH];
// Retrieve the full path and file name of a specified file
dwRet = GetFullPathName(lpszFileName,
sizeof(szPath) / sizeof(TCHAR),
szPath, &lpszFilePart);
if (!dwRet)
return hRes;
// Get a pointer to the IShellLink interface
hRes = CoCreateInstance(CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(void**)&ipShellLink);
if (SUCCEEDED(hRes))
{
// Get a pointer to the IPersistFile interface
CComQIPtr<IPersistFile> ipPersistFile(ipShellLink);
// Set the path to the shortcut target and add the description
hRes = ipShellLink->SetPath(szPath);
if (FAILED(hRes))
return hRes;
hRes = ipShellLink->SetDescription(lpszDesc);
if (FAILED(hRes)){
return hRes;
}
hRes = ipShellLink->SetArguments(lpszArguments);
if (FAILED(hRes)){
return hRes;
}
hRes = ipShellLink->SetShowCmd(iShowCmd);
if (FAILED(hRes)){
return hRes;
}
// IPersistFile is using LPCOLESTR, so make sure
// that the string is Unicode
#if !defined _UNICODE
MultiByteToWideChar(CP_ACP, 0,
lpszShortcutPath, -1, wszTemp, MAX_PATH);
#else
wcsncpy_s(wszTemp, lpszShortcutPath, MAX_PATH);
#endif
// Write the shortcut to disk
hRes = ipPersistFile->Save(wszTemp, TRUE);
}
//::CoUninitialize();
return hRes;
}
|