PDA

View Full Version : Qt app: Create a shell link to my app with a pixmap as the link icon



ddonate
25th January 2021, 12:29
Hi,

I have a Qt app and I need to save in runtime a link to my .exe file (with some arguments) in user desktop The icon must be a pixmap I have in runtime. Can I use this pixmap somehow as the link icon or must I save the pixmap to a .ico file?

Another doubt: when I create the link with an icon path my app icon is changed to that icon... Can I avoid it? I want an icon for the link different from my app icon

I am (successfully) creating a link with this snipped code:



QString sPathTarget = [MY_EXE_PATH];
QString sLinkName = "link name";
QString sLinkArguments = "link arguments";
QString sLinkIcon = [MY_EXE_PATH]; // Here: can I use the pixmap I have in runtime?

WCHAR pathDesktop[MAX_PATH];
HRESULT result = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY, NULL, 0, pathDesktop);

if (SUCCEEDED(result)) {
QString linkPath = QDir(QString::fromWCharArray(pathDesktop)).absolut eFilePath(QString("%1.lnk").arg(sLinkName));

CoInitialize(NULL);
IShellLinkW* shellLink = NULL;
result = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLinkW, (void**)&shellLink);
if (SUCCEEDED(result)) {
shellLink->SetPath(sPathTarget.toStdWString().c_str());
shellLink->SetArguments(sLinkArguments.toStdWString().c_str() );
shellLink->SetIconLocation(sLinkIcon.toStdWString().c_str(), 0);
IPersistFile* persistFile;
result = shellLink->QueryInterface(IID_IPersistFile, (void**)&persistFile);

if (SUCCEEDED(result)) {
result = persistFile->Save(linkPath.toStdWString().c_str(), TRUE);

persistFile->Release();

bRet = true;
}
shellLink->Release();
}
}
Thanks,
Diego