- HRESULT hRes;
- IShellLink* psl;
- CoInitialize(NULL);
- if(isFile)
- hRes = CoCreateInstance(CLSID_ShellLink, NULL,
- CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
- else
- {
- //hRes = CoCreateInstance(CLSID_FolderShortcut, NULL,
- // CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
- hRes = CoCreateInstance(CLSID_ShellLink, NULL,
- CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
- }
- if(SUCCEEDED(hRes))
- {
- IPersistFile* ppf;
- psl->SetPath(pathToObj);
- psl->SetDescription(description);
- psl->SetHotkey(hotkey);
- psl->SetArguments(cmdLine);
- //Query IShellLink for IPersistFile interface
- hRes = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
- if(SUCCEEDED(hRes))
- {
- //Build the output path
- char fname[MAX_PATH] = {0};
- char* drive = new char[MAX_PATH];
- _splitpath(mpathObj, drive, NULL, fname, NULL);
- if(strcmp(fname,"") != 0)
- {
- strcat_s(path, MAX_PATH, fname);
- }
- else if(strcmp(drive,"") != 0)
- {
- char d[2];
- d[0] = drive[0];
- d[1] = '\\0';
- strcat_s(path, MAX_PATH, d);
- }
- delete [] drive;
- strcat_s(path, MAX_PATH, ".lnk");
- // convert pathLink to unicode
- WCHAR wsz[MAX_PATH];
- if(MultiByteToWideChar(CP_ACP, 0, path, -1, wsz, MAX_PATH) != 0)
- {
- ppf->Save(wsz, TRUE);
- }
- ppf->Release();
- }
- psl->Release();
- }
- else
- {
- ReportError("CoCreateInstance Error");
- CoUninitialize();
- return 0;
- }
- CoUninitialize();
- //该片段来自于http://www.codesnippet.cn/detail/240220148758.html
来源: http://www.codesnippet.cn/detail/240220148758.html