extensions/source/scanner/twain32shim.cxx              |    6 -
 fpicker/source/win32/VistaFilePickerImpl.cxx           |   54 +++++------------
 include/systools/win32/comtools.hxx                    |   19 +++++
 sal/osl/w32/security.cxx                               |    9 +-
 vcl/win/app/fileregistration.cxx                       |   16 +----
 xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx |   12 +--
 6 files changed, 52 insertions(+), 64 deletions(-)

New commits:
commit df4255c315f8061fbe7b3771122926589be7dfad
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sat Apr 22 16:12:23 2023 +0200
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Sat Apr 22 17:45:50 2023 +0200

    Simplify lcl_getURLFromShellItem
    
    And make the URL returned for the item by system to be the preferred result.
    
    Change-Id: Ifbe8acc0aef8d656526d1ae64010fb6bfa68eefc
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150784
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/fpicker/source/win32/VistaFilePickerImpl.cxx 
b/fpicker/source/win32/VistaFilePickerImpl.cxx
index 741fdadb621a..fa58efc6c5d9 100644
--- a/fpicker/source/win32/VistaFilePickerImpl.cxx
+++ b/fpicker/source/win32/VistaFilePickerImpl.cxx
@@ -169,55 +169,33 @@ using TFolderPickerDialogImpl = 
TDialogImpl<TFileOpenDialog, CLSID_FileOpenDialo
 
 static OUString lcl_getURLFromShellItem (IShellItem* pItem)
 {
-    LPWSTR pStr = nullptr;
-    OUString sURL;
-    HRESULT hr;
-
-    hr = pItem->GetDisplayName ( SIGDN_FILESYSPATH, &pStr );
-    if (SUCCEEDED(hr))
-    {
-        ::osl::FileBase::getFileURLFromSystemPath( OUString(o3tl::toU(pStr)), 
sURL );
-        goto cleanup;
-    }
-
-    hr = pItem->GetDisplayName ( SIGDN_URL, &pStr );
-    if (SUCCEEDED(hr))
-    {
-        sURL = o3tl::toU(pStr);
-        goto cleanup;
-    }
+    sal::systools::CoTaskMemAllocated<wchar_t> pStr;
+    if (SUCCEEDED(pItem->GetDisplayName(SIGDN_URL, &pStr)))
+        return OUString(o3tl::toU(pStr));
 
-    hr = pItem->GetDisplayName ( SIGDN_PARENTRELATIVEPARSING, &pStr );
-    if (SUCCEEDED(hr))
+    HRESULT hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pStr);
+    if (FAILED(hr))
     {
-        GUID known_folder_id;
-        std::wstring aStr = pStr;
-        CoTaskMemFree (pStr);
-
-        if (0 == aStr.compare(0, 3, L"::{"))
-            aStr = aStr.substr(2);
-        hr = IIDFromString(aStr.c_str(), &known_folder_id);
+        hr = pItem->GetDisplayName(SIGDN_PARENTRELATIVEPARSING, &pStr);
         if (SUCCEEDED(hr))
         {
-            hr = SHGetKnownFolderPath(known_folder_id, 0, nullptr, &pStr);
+            GUID known_folder_id;
+            wchar_t* pStr2 = pStr;
+            if (pStr2[0] == ':' && pStr2[1] == ':' && pStr2[2] == '{')
+                pStr2 += 2;
+            hr = IIDFromString(pStr2, &known_folder_id);
             if (SUCCEEDED(hr))
-            {
-                
::osl::FileBase::getFileURLFromSystemPath(OUString(o3tl::toU(pStr)), sURL);
-                goto cleanup;
-            }
+                hr = SHGetKnownFolderPath(known_folder_id, 0, nullptr, &pStr);
         }
     }
 
     // Default fallback
-    hr = SHGetKnownFolderPath(FOLDERID_Documents, 0, nullptr, &pStr);
+    if (FAILED(hr))
+        hr = SHGetKnownFolderPath(FOLDERID_Documents, 0, nullptr, &pStr);
+
+    OUString sURL;
     if (SUCCEEDED(hr))
         ::osl::FileBase::getFileURLFromSystemPath(OUString(o3tl::toU(pStr)), 
sURL);
-    else // shouldn't happen...
-        goto bailout;
-
-cleanup:
-    CoTaskMemFree (pStr);
-bailout:
     return sURL;
 }
 
commit 8de1bf3674b5f5391ff1be229d99115107514877
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sat Apr 22 16:06:07 2023 +0200
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Sat Apr 22 17:45:40 2023 +0200

    Introduce sal::systools::CoTaskMemAllocated to use RAII for CoTaskMemFree
    
    Change-Id: I5553138bfc8dd989e68b8bcc2be981746e8c1e84
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150783
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

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;

Reply via email to