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>
This commit is contained in:
Mike Kaganski
2022-01-05 13:20:09 +03:00
parent 9fb45044d9
commit d75bf2c770
6 changed files with 25 additions and 10 deletions

View File

@@ -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"

View File

@@ -25,6 +25,8 @@
#endif
#include <windows.h>
HMODULE GetCurrentModuleHandle();
extern LONG g_DllRefCnt;
#endif

View File

@@ -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);

View File

@@ -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);

View File

@@ -303,7 +303,7 @@ STDAPI DllRegisterServer()
WCHAR ModuleFileName[MAX_PATH];
GetModuleFileNameW(
GetModuleHandleW(MODULE_NAME),
GetCurrentModuleHandle(),
ModuleFileName,
sizeof(ModuleFileName)/sizeof(ModuleFileName[0]));

View File

@@ -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: */