extensions/source/activex/so_activex.cxx |   28 +++++---------
 sal/osl/w32/module.cxx                   |   62 ++++++-------------------------
 2 files changed, 23 insertions(+), 67 deletions(-)

New commits:
commit 9d886e80ab742de49e2fe4fe62763edb21485d4e
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Jan 5 19:20:36 2022 +0300
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Wed Jan 5 19:49:13 2022 +0100

    Simplify osl_getModuleURLFromAddress using GetModuleHandleExW
    
    Somehow missed that in commit 515d2579d305a6127c6c194319a58eac62437e33
        Date    Fri Apr 05 13:15:42 2019 +0300
        Replace legacy dynamically-loaded functions with statically linked ones
    
    Also drop obsolete suppressions of warning not emitted by MSVC anymore.
    
    Change-Id: If4a4c2ec76b275fb358b325652422e81e9003eb6
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/128019
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/sal/osl/w32/module.cxx b/sal/osl/w32/module.cxx
index 798da4183e7c..201f2dd164ee 100644
--- a/sal/osl/w32/module.cxx
+++ b/sal/osl/w32/module.cxx
@@ -18,7 +18,6 @@
  */
 
 #include "system.h"
-#include <psapi.h>
 
 #include "file_url.hxx"
 #include "path_helper.hxx"
@@ -137,14 +136,7 @@ void* SAL_CALL osl_getSymbol(oslModule Module, rtl_uString 
*strSymbolName)
        be in this case unavoidable because the API has to stay
        compatible. We need to keep this function which returns a
        void* by definition */
-#ifdef _MSC_VER
-#pragma warning(push)
-#pragma warning(disable:4054)
-#endif
     return reinterpret_cast<void*>(osl_getFunctionSymbol(Module, 
strSymbolName));
-#ifdef _MSC_VER
-#pragma warning(pop)
-#endif
 }
 
 oslGenericFunction SAL_CALL osl_getFunctionSymbol( oslModule Module, 
rtl_uString *strSymbolName )
@@ -182,44 +174,23 @@ osl_getAsciiFunctionSymbol( oslModule Module, const char 
*pSymbol )
 
 sal_Bool SAL_CALL osl_getModuleURLFromAddress( void *pv, rtl_uString 
**pustrURL )
 {
-    bool bSuccess    = false;    /* Assume failure */
-    DWORD cbNeeded = 0;
-    HMODULE* lpModules = nullptr;
-    DWORD nModules = 0;
-    UINT iModule = 0;
-    MODULEINFO modinfo;
-
-    EnumProcessModules(GetCurrentProcess(), nullptr, 0, &cbNeeded);
-
-    lpModules = static_cast<HMODULE*>(_alloca(cbNeeded));
-    EnumProcessModules(GetCurrentProcess(), lpModules, cbNeeded, &cbNeeded);
+    HMODULE hModule{};
+    GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
+                           | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
+                       reinterpret_cast<LPCWSTR>(pv), &hModule);
+    if (!hModule)
+        return false;
 
-    nModules = cbNeeded / sizeof(HMODULE);
+    ::osl::LongPathBuffer<sal_Unicode> aBuffer(MAX_LONG_PATH);
+    rtl_uString* ustrSysPath = nullptr;
 
-    for (iModule = 0; !bSuccess && iModule < nModules; iModule++)
-    {
-        GetModuleInformation(GetCurrentProcess(), lpModules[iModule], &modinfo,
-                                 sizeof(modinfo));
-
-        if (static_cast<BYTE*>(pv) >= static_cast<BYTE*>(modinfo.lpBaseOfDll)
-            && static_cast<BYTE*>(pv)
-                   < static_cast<BYTE*>(modinfo.lpBaseOfDll) + 
modinfo.SizeOfImage)
-        {
-            ::osl::LongPathBuffer<sal_Unicode> aBuffer(MAX_LONG_PATH);
-            rtl_uString* ustrSysPath = nullptr;
+    GetModuleFileNameW(hModule, o3tl::toW(aBuffer), 
aBuffer.getBufSizeInSymbols());
 
-            GetModuleFileNameW(lpModules[iModule], o3tl::toW(aBuffer),
-                               aBuffer.getBufSizeInSymbols());
+    rtl_uString_newFromStr(&ustrSysPath, aBuffer);
+    osl_getFileURLFromSystemPath(ustrSysPath, pustrURL);
+    rtl_uString_release(ustrSysPath);
 
-            rtl_uString_newFromStr(&ustrSysPath, aBuffer);
-            osl_getFileURLFromSystemPath(ustrSysPath, pustrURL);
-            rtl_uString_release(ustrSysPath);
-
-            bSuccess = true;
-        }
-    }
-
-    return bSuccess;
+    return true;
 }
 
 sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress( oslGenericFunction 
addr, rtl_uString ** ppLibraryUrl )
@@ -228,14 +199,7 @@ sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress( 
oslGenericFunction addr,
        not allowed according to the C/C++ standards. In this case
        it is unavoidable because we have to stay compatible we
        cannot remove any function. */
-#ifdef _MSC_VER
-#pragma warning(push)
-#pragma warning(disable:4054)
-#endif
     return osl_getModuleURLFromAddress(reinterpret_cast<void*>(addr), 
ppLibraryUrl);
-#ifdef _MSC_VER
-#pragma warning(pop)
-#endif
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
commit 9b85e4e3263fe2bc75c371969256064e952d7fd4
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Jan 5 18:35:27 2022 +0300
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Wed Jan 5 19:48:58 2022 +0100

    Use GetModuleHandleExW instead of GetModuleHandleW
    
    ... and find directory cearching for path separator.
    Also drop obsolete code related to Windows XP and mingw.
    
    Change-Id: Ib48cfeea8a2ae01d3f8b37728ce1e8ca4aa73b14
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/128017
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/extensions/source/activex/so_activex.cxx 
b/extensions/source/activex/so_activex.cxx
index c8e8828bdd93..70682a416b53 100644
--- a/extensions/source/activex/so_activex.cxx
+++ b/extensions/source/activex/so_activex.cxx
@@ -69,17 +69,6 @@ END_OBJECT_MAP()
 #define X64_LIB_NAME L"so_activex_x64.dll"
 #define X32_LIB_NAME L"so_activex.dll"
 
-// to provide windows xp as build systems for mingw we need to define 
KEY_WOW64_64KEY
-// in mingw 3.13 KEY_WOW64_64KEY isn't available < Win2003 systems.
-// Also defined in 
setup_native\source\win32\customactions\reg64\reg64.cxx,source\win32\customactions\shellextensions\shellextensions.cxx
 and
-// extensions\source\activex\main\so_activex.cpp
-#ifndef KEY_WOW64_64KEY
-    #define KEY_WOW64_64KEY (0x0100)
-#endif
-#ifndef KEY_WOW64_32KEY
-    #define KEY_WOW64_32KEY (0x0200)
-#endif
-
 const REGSAM n64KeyAccess = KEY_ALL_ACCESS | KEY_WOW64_64KEY;
 const REGSAM n32KeyAccess = KEY_ALL_ACCESS;
 
@@ -742,17 +731,20 @@ STDAPI DllRegisterServer()
 {
     HRESULT aResult = E_FAIL;
 
-    HMODULE aCurModule = GetModuleHandleW( bX64 ? X64_LIB_NAME : X32_LIB_NAME 
);
-    DWORD nLibNameLen = sal::static_int_cast<DWORD>(
-            wcslen(bX64 ? X64_LIB_NAME : X32_LIB_NAME));
-
+    HMODULE aCurModule{};
+    GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
+                           | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
+                       reinterpret_cast<LPCWSTR>(&DllRegisterServer), 
&aCurModule);
     if( aCurModule )
     {
         wchar_t pProgramPath[1024];
-        DWORD nLen = GetModuleFileNameW( aCurModule, pProgramPath, 1024 );
-        if ( nLen && nLen > nLibNameLen + 1 )
+        wchar_t* pPathEnd = nullptr;
+        DWORD nLen = GetModuleFileNameW( aCurModule, pProgramPath, 
SAL_N_ELEMENTS(pProgramPath) );
+        if ( nLen && nLen < SAL_N_ELEMENTS(pProgramPath) )
+            pPathEnd = wcsrchr(pProgramPath, '\\');
+        if (pPathEnd)
         {
-            pProgramPath[ nLen - nLibNameLen - 1 ] = 0;
+            *pPathEnd = 0;
             aResult = DllRegisterServerNative( 31, TRUE, bX64, pProgramPath );
             if( SUCCEEDED( aResult ) )
                 aResult = DllRegisterServerDoc( 31, TRUE, bX64 );

Reply via email to