From b83f9be1bd65f784bbed603e550fbe9f650b0367 Mon Sep 17 00:00:00 2001 From: ADudeCalledLeo <7997354+Leo40Git@users.noreply.github.com> Date: Fri, 28 Oct 2022 15:46:54 +0300 Subject: [PATCH] Add missing fail check for CoInitialize Add a few comments Signed-off-by: ADudeCalledLeo <7997354+Leo40Git@users.noreply.github.com> --- launcher/FileSystem.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/launcher/FileSystem.cpp b/launcher/FileSystem.cpp index 90f0313f6..587753a0e 100644 --- a/launcher/FileSystem.cpp +++ b/launcher/FileSystem.cpp @@ -427,14 +427,21 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri 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]; - // ...yes, you need to initialize the entire COM stack to make a shortcut in Windows - CoInitialize(nullptr); - - HRESULT hres; IShellLink* psl; + // create an IShellLink instance - this stores the shortcut's attributes hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if (SUCCEEDED(hres)) { @@ -448,7 +455,7 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri wmemset(wsz, 0, MAX_PATH); targetInfo.absolutePath().toWCharArray(wsz); - psl->SetWorkingDirectory(wsz); + psl->SetWorkingDirectory(wsz); // "Starts in" attribute if (!icon.isEmpty()) { @@ -457,6 +464,8 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri 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; hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); if (SUCCEEDED(hres)) @@ -484,6 +493,7 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri qWarning() << "hres = " << hres; } + // go away COM, nobody likes you CoUninitialize(); return SUCCEEDED(hres);