Add missing fail check for CoInitialize

Add a few comments

Signed-off-by: ADudeCalledLeo <7997354+Leo40Git@users.noreply.github.com>
This commit is contained in:
ADudeCalledLeo 2022-10-28 15:46:54 +03:00
parent 6ae3b183fd
commit b83f9be1bd
No known key found for this signature in database
GPG Key ID: 2E08DA5D6AF36F3B

View File

@ -427,14 +427,21 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri
return false; return false;
} }
HRESULT hres;
// ...yes, you need to initialize the entire COM stack just to make a shortcut
hres = CoInitialize(nullptr);
if (FAILED(hres))
{
qWarning() << "Failed to initialize COM!";
return false;
}
WCHAR wsz[MAX_PATH]; WCHAR wsz[MAX_PATH];
// ...yes, you need to initialize the entire COM stack to make a shortcut in Windows
CoInitialize(nullptr);
HRESULT hres;
IShellLink* psl; IShellLink* psl;
// create an IShellLink instance - this stores the shortcut's attributes
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres)) if (SUCCEEDED(hres))
{ {
@ -448,7 +455,7 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri
wmemset(wsz, 0, MAX_PATH); wmemset(wsz, 0, MAX_PATH);
targetInfo.absolutePath().toWCharArray(wsz); targetInfo.absolutePath().toWCharArray(wsz);
psl->SetWorkingDirectory(wsz); psl->SetWorkingDirectory(wsz); // "Starts in" attribute
if (!icon.isEmpty()) if (!icon.isEmpty())
{ {
@ -457,6 +464,8 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri
psl->SetIconLocation(wsz, 0); psl->SetIconLocation(wsz, 0);
} }
// query an IPersistFile interface from our IShellLink instance
// this is the interface that will actually let us save the shortcut to disk!
IPersistFile* ppf; IPersistFile* ppf;
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if (SUCCEEDED(hres)) if (SUCCEEDED(hres))
@ -484,6 +493,7 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri
qWarning() << "hres = " << hres; qWarning() << "hres = " << hres;
} }
// go away COM, nobody likes you
CoUninitialize(); CoUninitialize();
return SUCCEEDED(hres); return SUCCEEDED(hres);