I tried to create a new shortcut link file using IShellLink and IPersistFIle interface.
After I finish saving the lnk file by calling IPersistFIle::save(), and I want to delete the ico file used to create the lnk right after save function returns. But the problem is that the lnk file is showed in a blank page, not the icon I used.
If I don’t call deletefile() after save, the lnk file looks correct.
My question is that when can I delete the icon file after save the lnk file?
PS: I tried to sleep, for, like 2 seconds before delete file, and the lnk looks correct too.
Part of my code is bellow:
hRes = pShellLink->QueryInterface(IID_IPersistFile,(LPVOID*)&pPerFile);
if (SUCCEEDED(hRes))
{
WCHAR wszLinkFile[MAX_PATH + 1] = {0};
#ifdef _UNICODE
StringCchCopy(wszLinkFile, sizeof(wszLinkFile) / sizeof(wszLinkFile[0]), lpszLinkFilePath);
#else
MultiByteToWideChar(CP_ACP, 0, lpszLinkFilePath, -1, wszLinkFile, MAX_PATH);
#endif
hRes = pPerFile->Save(wszLinkFile, TRUE);
pPerFile->Release();
}
pShellLink->Release();
::CoUninitialize();
DeleteFile(lpszIconPath);
You didn’t post enough code, but the method name is IShellLink::SetIconLocation().
You delete the icon and the location is no longer valid.
Boilerplate is to use an icon that’s embedded in the executable as a resource. But if you use a separate .ico file then you’ll need to keep it valid.