From 50091df9f572a569b42059b30142b859190febd0 Mon Sep 17 00:00:00 2001 From: Holger Frydrych Date: Wed, 12 Jan 2022 19:30:38 +0100 Subject: [PATCH] Prevent MSVC from merging identical functions - we need them to have separate pointer addresses --- CMakeLists.txt | 2 ++ src/dllmain.cpp | 7 ++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f622339..379a455 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,6 +140,8 @@ include_directories( ) add_definitions(-D_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING) +# do not merge functions with identical bodies; we need them to be separate entities for hooking purposes +add_link_options("/OPT:NOICF") if(CMAKE_SIZEOF_VOID_P EQUAL 8) add_definitions(-DWIN64) diff --git a/src/dllmain.cpp b/src/dllmain.cpp index facb8ed..8c1d557 100644 --- a/src/dllmain.cpp +++ b/src/dllmain.cpp @@ -54,9 +54,7 @@ namespace { } HMODULE WINAPI Hook_LoadLibraryW(LPCWSTR lpFileName) { - // necessary, or else VS merges this function with Hook_LoadLibraryA and assigns them the same address - std::wstring fileName (lpFileName); - HMODULE handle = vrperfkit::hooks::CallOriginal(Hook_LoadLibraryW)(fileName.c_str()); + HMODULE handle = vrperfkit::hooks::CallOriginal(Hook_LoadLibraryW)(lpFileName); if (handle != nullptr && handle != vrperfkit::g_moduleSelf) { InstallVrHooks(); @@ -66,8 +64,7 @@ namespace { } HMODULE WINAPI Hook_LoadLibraryExW(LPCWSTR lpFileName, HANDLE hFile, DWORD dwFlags) { - std::wstring fileName (lpFileName); - HMODULE handle = vrperfkit::hooks::CallOriginal(Hook_LoadLibraryExW)(fileName.c_str(), hFile, dwFlags); + HMODULE handle = vrperfkit::hooks::CallOriginal(Hook_LoadLibraryExW)(lpFileName, hFile, dwFlags); if (handle != nullptr && handle != vrperfkit::g_moduleSelf && (dwFlags & (LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE | LOAD_LIBRARY_AS_IMAGE_RESOURCE)) == 0) { InstallVrHooks();