summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--extensions/source/scanner/twain32shim.cxx6
-rw-r--r--include/systools/win32/comtools.hxx19
-rw-r--r--sal/osl/w32/security.cxx9
-rw-r--r--vcl/win/app/fileregistration.cxx16
-rw-r--r--xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx12
5 files changed, 36 insertions, 26 deletions
diff --git a/extensions/source/scanner/twain32shim.cxx b/extensions/source/scanner/twain32shim.cxx
index beca35f4f26b..6e0be8149471 100644
--- a/extensions/source/scanner/twain32shim.cxx
+++ b/extensions/source/scanner/twain32shim.cxx
@@ -28,6 +28,7 @@
*/
#include "twain32shim.hxx"
+#include <systools/win32/comtools.hxx>
#include <tools/helpers.hxx>
#include <twain/twain.h>
#include <o3tl/unit_conversion.hxx>
@@ -251,11 +252,10 @@ void ImpTwain::ImplOpenSourceManager()
if (!m_hMod)
{
// Windows directory might not be in DLL search path sometimes, so try the full path
- PWSTR sPath;
+ sal::systools::CoTaskMemAllocated<wchar_t> sPath;
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Windows, 0, nullptr, &sPath)))
{
- std::wstring sPathAndFile = sPath;
- CoTaskMemFree(sPath);
+ std::wstring sPathAndFile(sPath);
sPathAndFile += L"\\TWAIN_32.DLL";
m_hMod = LoadLibraryW(sPathAndFile.c_str());
}
diff --git a/include/systools/win32/comtools.hxx b/include/systools/win32/comtools.hxx
index b141842882aa..d54b1a22ce02 100644
--- a/include/systools/win32/comtools.hxx
+++ b/include/systools/win32/comtools.hxx
@@ -240,6 +240,25 @@ namespace sal::systools
T* com_ptr_;
};
+ // A class to use with functions taking an out pointer argument,
+ // that needs to be freed with CoTaskMemFree - like SHGetKnownFolderPath
+ template <typename T> class CoTaskMemAllocated
+ {
+ public:
+ ~CoTaskMemAllocated() { CoTaskMemFree(m_pv); }
+
+ T** operator&()
+ {
+ CoTaskMemFree(std::exchange(m_pv, nullptr));
+ return &m_pv;
+ };
+
+ operator T*() { return m_pv; }
+
+ private:
+ T* m_pv = nullptr;
+ };
+
} // sal::systools
/* Typedefs for some popular COM interfaces */
diff --git a/sal/osl/w32/security.cxx b/sal/osl/w32/security.cxx
index 21ed64a7862a..0e9bc96c9b78 100644
--- a/sal/osl/w32/security.cxx
+++ b/sal/osl/w32/security.cxx
@@ -25,6 +25,7 @@
#include <osl/diagnose.h>
#include <osl/thread.h>
#include <osl/file.h>
+#include <systools/win32/comtools.hxx>
#include <systools/win32/uwinapi.h>
#include <sddl.h>
#include <sal/macros.h>
@@ -509,16 +510,14 @@ void SAL_CALL osl_unloadUserProfile(oslSecurity Security)
static bool GetSpecialFolder(rtl_uString **strPath, REFKNOWNFOLDERID rFolder)
{
- bool bRet = false;
- PWSTR PathW;
+ sal::systools::CoTaskMemAllocated<wchar_t> PathW;
if (SUCCEEDED(SHGetKnownFolderPath(rFolder, KF_FLAG_CREATE, nullptr, &PathW)))
{
rtl_uString_newFromStr(strPath, o3tl::toU(PathW));
- CoTaskMemFree(PathW);
- bRet = true;
+ return true;
}
- return bRet;
+ return false;
}
// We use LPCTSTR here, because we use it with SE_foo_NAME constants
diff --git a/vcl/win/app/fileregistration.cxx b/vcl/win/app/fileregistration.cxx
index bd31c4acd607..ec7ccbcee2aa 100644
--- a/vcl/win/app/fileregistration.cxx
+++ b/vcl/win/app/fileregistration.cxx
@@ -61,21 +61,17 @@ IsPathDefaultForClass(sal::systools::COMReference<IApplicationAssociationRegistr
LPCWSTR aClassName, LPCWSTR progID)
{
// Make sure the Prog ID matches what we have
- LPWSTR registeredApp;
+ sal::systools::CoTaskMemAllocated<wchar_t> registeredApp;
HRESULT hr
= pAAR->QueryCurrentDefault(aClassName, AT_FILEEXTENSION, AL_EFFECTIVE, &registeredApp);
- if (FAILED(hr))
+ if (SUCCEEDED(hr))
{
- return hr;
+ if (wcsnicmp(registeredApp, progID, wcslen(progID)) == 0)
+ hr = S_OK;
+ else
+ hr = S_FALSE;
}
- if (!wcsnicmp(registeredApp, progID, wcslen(progID)))
- hr = S_OK;
- else
- hr = S_FALSE;
-
- CoTaskMemFree(registeredApp);
-
return hr;
}
diff --git a/xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx b/xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx
index 6e4593696234..0a306a008db0 100644
--- a/xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx
+++ b/xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx
@@ -67,7 +67,7 @@
#ifdef _WIN32
#include <o3tl/char16_t2wchar_t.hxx>
-#include <prewin.h>
+#include <systools/win32/comtools.hxx>
#include <Shlobj.h>
#endif
@@ -490,16 +490,12 @@ IMPL_LINK_NOARG(DigitalSignaturesDialog, CertMgrButtonHdl, weld::Button&, void)
u"GNU\\GnuPG\\bin\\gpa.exe",
};
static const OUString aPath = [] {
- OUString sRet;
- PWSTR sPath = nullptr;
+ sal::systools::CoTaskMemAllocated<wchar_t> sPath;
HRESULT hr
= SHGetKnownFolderPath(FOLDERID_ProgramFilesX86, KF_FLAG_DEFAULT, nullptr, &sPath);
if (SUCCEEDED(hr))
- {
- sRet = o3tl::toU(sPath);
- CoTaskMemFree(sPath);
- }
- return sRet;
+ return OUString(o3tl::toU(sPath));
+ return OUString();
}();
if (aPath.isEmpty())
return;