use ComPtr for misc. things

This commit is contained in:
oltolm
2025-01-17 18:06:24 +01:00
parent 60c98a6483
commit 5c1412f84d
12 changed files with 110 additions and 126 deletions
+5 -6
View File
@@ -13,6 +13,7 @@
#include <shlobj.h>
#include <commdlg.h>
#include <cderr.h>
#include <wrl/client.h>
namespace W32Util {
std::string BrowseForFolder(HWND parent, std::string_view title, std::string_view initialPath) {
@@ -192,16 +193,16 @@ namespace W32Util {
// http://msdn.microsoft.com/en-us/library/aa969393.aspx
static HRESULT CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszArguments, LPCWSTR lpszPathLink, LPCWSTR lpszDesc, LPCWSTR lpszIcon, int iconIndex) {
HRESULT hres;
IShellLink *psl = nullptr;
Microsoft::WRL::ComPtr<IShellLink> psl;
hres = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hres))
return hres;
// 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);
hres = CoCreateInstance(__uuidof(ShellLink), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&psl));
if (SUCCEEDED(hres) && psl) {
IPersistFile *ppf = nullptr;
Microsoft::WRL::ComPtr<IPersistFile> ppf;
// Set the path to the shortcut target and add the description.
psl->SetPath(lpszPathObj);
@@ -213,14 +214,12 @@ static HRESULT CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszArguments, LPCWSTR lp
// Query IShellLink for the IPersistFile interface, used for saving the
// shortcut in persistent storage.
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf);
hres = psl.As(&ppf);
if (SUCCEEDED(hres) && ppf) {
// Save the link by calling IPersistFile::Save.
hres = ppf->Save(lpszPathLink, TRUE);
ppf->Release();
}
psl->Release();
}
CoUninitialize();