tdf#146554: use GetModuleHandleExW instead of GetModuleHandleW

This allows to avoid use of module name when obtaining current module
handle, which needs to be synchronized and thus is error-prone.

Change-Id: I2f0e0af7f616c3582b0a3271cf9e06420a9dfc8e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/127993
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
diff --git a/shell/inc/config.hxx b/shell/inc/config.hxx
index 58b5dd4..4f48476 100644
--- a/shell/inc/config.hxx
+++ b/shell/inc/config.hxx
@@ -20,12 +20,6 @@
#ifndef INCLUDED_SHELL_INC_INTERNAL_CONFIG_HXX
#define INCLUDED_SHELL_INC_INTERNAL_CONFIG_HXX

#ifdef _AMD64_
#define MODULE_NAME L"shlxthdl_x64.dll"
#else
#define MODULE_NAME L"shlxthdl.dll"
#endif

#define COLUMN_HANDLER_DESCRIPTIVE_NAME    L"LibreOffice Column Handler"
#define INFOTIP_HANDLER_DESCRIPTIVE_NAME   L"LibreOffice Infotip Handler"
#define PROPSHEET_HANDLER_DESCRIPTIVE_NAME L"LibreOffice Property Sheet Handler"
diff --git a/shell/inc/global.hxx b/shell/inc/global.hxx
index a729d59..fa2b655 100644
--- a/shell/inc/global.hxx
+++ b/shell/inc/global.hxx
@@ -25,6 +25,8 @@
#endif
#include <windows.h>

HMODULE GetCurrentModuleHandle();

extern LONG g_DllRefCnt;

#endif
diff --git a/shell/source/win32/shlxthandler/propsheets/listviewbuilder.cxx b/shell/source/win32/shlxthandler/propsheets/listviewbuilder.cxx
index 08beac8..5f0705c 100644
--- a/shell/source/win32/shlxthandler/propsheets/listviewbuilder.cxx
+++ b/shell/source/win32/shlxthandler/propsheets/listviewbuilder.cxx
@@ -22,6 +22,7 @@
#include "document_statistic.hxx"
#include <utilities.hxx>
#include <config.hxx>
#include <global.hxx>

#include <commctrl.h>
#include <resource.h>
@@ -87,7 +88,7 @@ void list_view_builder::build(statistic_group_list_t& gl)
void list_view_builder::setup_list_view()
{
    HIMAGELIST h_ils = ImageList_Create(16,15,ILC_MASK, 7, 0);
    HBITMAP    h_bmp = LoadBitmapW(GetModuleHandleW(MODULE_NAME), MAKEINTRESOURCEW(IDB_PROPERTY_IMAGES));
    HBITMAP    h_bmp = LoadBitmapW(GetCurrentModuleHandle(), MAKEINTRESOURCEW(IDB_PROPERTY_IMAGES));
    ImageList_AddMasked(h_ils, h_bmp, RGB(255, 0, 255));

    (void) ListView_SetImageList(hwnd_list_view_, h_ils, LVSIL_SMALL);
diff --git a/shell/source/win32/shlxthandler/propsheets/propsheets.cxx b/shell/source/win32/shlxthandler/propsheets/propsheets.cxx
index 218b921..48a125d 100644
--- a/shell/source/win32/shlxthandler/propsheets/propsheets.cxx
+++ b/shell/source/win32/shlxthandler/propsheets/propsheets.cxx
@@ -171,7 +171,7 @@ HRESULT STDMETHODCALLTYPE CPropertySheet::AddPages(LPFNSVADDPROPSHEETPAGE lpfnAd
    // add the summary property page
    psp.dwSize      = sizeof(psp);
    psp.dwFlags     = PSP_DEFAULT | PSP_USETITLE | PSP_USECALLBACK;
    psp.hInstance   = GetModuleHandleW(MODULE_NAME);
    psp.hInstance   = GetCurrentModuleHandle();
    psp.lParam      = reinterpret_cast<LPARAM>(this);
    psp.pfnCallback = reinterpret_cast<LPFNPSPCALLBACKW>(CPropertySheet::PropPageSummaryCallback);

diff --git a/shell/source/win32/shlxthandler/shlxthdl.cxx b/shell/source/win32/shlxthandler/shlxthdl.cxx
index 6383cc2f..9a5b8a3 100644
--- a/shell/source/win32/shlxthandler/shlxthdl.cxx
+++ b/shell/source/win32/shlxthandler/shlxthdl.cxx
@@ -303,7 +303,7 @@ STDAPI DllRegisterServer()
    WCHAR ModuleFileName[MAX_PATH];

    GetModuleFileNameW(
        GetModuleHandleW(MODULE_NAME),
        GetCurrentModuleHandle(),
        ModuleFileName,
        sizeof(ModuleFileName)/sizeof(ModuleFileName[0]));

diff --git a/shell/source/win32/shlxthandler/util/utilities.cxx b/shell/source/win32/shlxthandler/util/utilities.cxx
index 489474b..27bf12c 100644
--- a/shell/source/win32/shlxthandler/util/utilities.cxx
+++ b/shell/source/win32/shlxthandler/util/utilities.cxx
@@ -22,6 +22,7 @@
#include <memory>

#include <config.hxx>
#include <global.hxx>
#include <utilities.hxx>

// constants
@@ -81,7 +82,7 @@ std::wstring GetResString(int ResId)
{
    wchar_t szResStr[MAX_RES_STRING];

    int rc = LoadStringW( GetModuleHandleW(MODULE_NAME), ResId, szResStr, sizeof(szResStr) );
    int rc = LoadStringW( GetCurrentModuleHandle(), ResId, szResStr, sizeof(szResStr) );

    OutputDebugStringFormatW( L"GetResString: read %d chars\n", rc );
    // OSL_ENSURE(rc, "String resource not found");
@@ -544,4 +545,21 @@ LCID LocaleSetToLCID( const LocaleSet_t & Locale )
    return MAKELCID( MAKELANGID( usPrimaryLang, usSubLang ), SORT_DEFAULT );
}

// The function is defined in the static library, and thus its address is local to current module
HMODULE GetCurrentModuleHandle()
{
    HMODULE h{};

    if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
                               | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
                           reinterpret_cast<LPCWSTR>(&GetCurrentModuleHandle), &h)
        == 0)
    {
        const DWORD dwError = GetLastError();
        OutputDebugStringFormatW(
            L"GetCurrentModuleHandle: GetModuleHandleExW failed, error is 0x%X", dwError);
    }
    return h;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */