HRESULT CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc, LPCSTR wrkdir) { HRESULT hres; IShellLinkA* psl; // Get a pointer to the IShellLink interface. It is assumed that CoInitialize // has already been called. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; // Set the path to the shortcut target and add the description. psl->SetPath(lpszPathObj); psl->SetDescription(lpszDesc); psl->SetWorkingDirectory(wrkdir); // Query IShellLink for the IPersistFile interface, used for saving the // shortcut in persistent storage. hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); if (SUCCEEDED(hres)) { WCHAR wsz[MAX_PATH]; // Ensure that the string is Unicode. MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH); // Add code here to check return value from MultiByteWideChar // for success. // Save the link by calling IPersistFile::Save. hres = ppf->Save(wsz, TRUE); ppf->Release(); } psl->Release(); } return hres; }
// Create a shortcut bool CreateLinkInnerA(char *filename, char *target, char *workdir, char *args, char *comment, char *icon, UINT icon_index) { HRESULT r; wchar_t tmp[MAX_SIZE]; IShellLinkA* pShellLink; IPersistFile* pPersistFile; r = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkA, (void **)&pShellLink); if (FAILED(r)) { return false; } r = pShellLink->QueryInterface(IID_IPersistFile,(void **)&pPersistFile); if (FAILED(r)) { pShellLink->Release(); return false; } r = pShellLink->SetPath(target); if (FAILED(r)) { pShellLink->Release(); pPersistFile->Release(); return false; } if (workdir != NULL) { r = pShellLink->SetWorkingDirectory(workdir); if (FAILED(r)) { pShellLink->Release(); pPersistFile->Release(); return false; } } if (args != NULL) { r = pShellLink->SetArguments(args); if (FAILED(r)) { pShellLink->Release(); pPersistFile->Release(); return false; } } if (comment != NULL) { r = pShellLink->SetDescription(comment); if (FAILED(r)) { pShellLink->Release(); pPersistFile->Release(); return false; } } if (icon != NULL) { r = pShellLink->SetIconLocation(icon, icon_index); if (FAILED(r)) { pShellLink->Release(); pPersistFile->Release(); return false; } } StrToUni(tmp, sizeof(tmp), filename); r = pPersistFile->Save(tmp, true); if (FAILED(r)) { pShellLink->Release(); pPersistFile->Release(); return false; } pShellLink->Release(); pPersistFile->Release(); return true; }