From c537539c6e3b95b57b44488657ee2aa49cd327c9 Mon Sep 17 00:00:00 2001 From: PatrickvL Date: Thu, 7 Feb 2019 15:15:18 +0100 Subject: [PATCH] Extracted VerifyWow64() function, so that the Cxbx and CxbxLoader projects share the same implementation. For this, renamed LoaderTooling.cpp to AddressRanges.cpp, which better aligns with AddressRanges.h anyway. --- CMakeLists.txt | 2 +- projects/CxbxLoader/CMakeLists.txt | 2 +- .../{LoaderTooling.cpp => AddressRanges.cpp} | 16 +++++++++++++++- src/common/AddressRanges.h | 2 ++ src/gui/WinMain.cpp | 10 ++-------- src/loader/loader.cpp | 15 +++++---------- 6 files changed, 26 insertions(+), 21 deletions(-) rename src/common/{LoaderTooling.cpp => AddressRanges.cpp} (82%) diff --git a/CMakeLists.txt b/CMakeLists.txt index a3f2a20b1..bbd5c966c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -262,7 +262,7 @@ file (GLOB CXBXR_KRNL_CPP file (GLOB CXBXR_SOURCE_EMU "${CXBXR_KRNL_CPP}" "${CXBXR_ROOT_DIR}/HighPerformanceGraphicsEnabler.c" - "${CXBXR_ROOT_DIR}/src/common/LoaderTooling.cpp" + "${CXBXR_ROOT_DIR}/src/common/AddressRanges.cpp" "${CXBXR_ROOT_DIR}/src/common/util/gloffscreen/glextensions.cpp" "${CXBXR_ROOT_DIR}/src/common/util/gloffscreen/gloffscreen_common.cpp" "${CXBXR_ROOT_DIR}/src/common/util/gloffscreen/gloffscreen_wgl.cpp" diff --git a/projects/CxbxLoader/CMakeLists.txt b/projects/CxbxLoader/CMakeLists.txt index 677b18d2d..7ea95b7c6 100644 --- a/projects/CxbxLoader/CMakeLists.txt +++ b/projects/CxbxLoader/CMakeLists.txt @@ -28,7 +28,7 @@ file (GLOB HEADERS ) file (GLOB SOURCES - "${CXBXR_ROOT_DIR}/src/common/LoaderTooling.cpp" + "${CXBXR_ROOT_DIR}/src/common/AddressRanges.cpp" "${CXBXR_ROOT_DIR}/src/common/ReserveAddressRanges.cpp" "${CXBXR_ROOT_DIR}/src/loader/loader.cpp" ) diff --git a/src/common/LoaderTooling.cpp b/src/common/AddressRanges.cpp similarity index 82% rename from src/common/LoaderTooling.cpp rename to src/common/AddressRanges.cpp index 3bf95f3f6..d99d1caef 100644 --- a/src/common/LoaderTooling.cpp +++ b/src/common/AddressRanges.cpp @@ -9,7 +9,7 @@ // * `88bo,__,o, oP"``"Yo, _88o,,od8P oP"``"Yo, // * "YUMMMMMP",m" "Mm,""YUMMMP" ,m" "Mm, // * -// * Common->LoaderTooling.cpp +// * Common->AddressRanges.cpp // * // * This file is part of the Cxbx project. // * @@ -46,6 +46,20 @@ bool IsOptionalAddressRange(const int index) return AddressRangeMatchesFlags(index, MAY_FAIL); } +bool VerifyWow64() +{ + typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); + BOOL bIsWow64 = FALSE; + HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32")); + LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(hKernel32, "IsWow64Process"); + if (fnIsWow64Process != nullptr) { + HANDLE hCurrentProcess = GetCurrentProcess(); + fnIsWow64Process(hCurrentProcess, &bIsWow64); + } + + return (bIsWow64 != FALSE); +} + LPTSTR GetLastErrorString() { DWORD err = GetLastError(); diff --git a/src/common/AddressRanges.h b/src/common/AddressRanges.h index 3396480f5..0ae0ba4b9 100644 --- a/src/common/AddressRanges.h +++ b/src/common/AddressRanges.h @@ -119,5 +119,7 @@ const struct { extern bool AddressRangeMatchesFlags(const int index, const int flags); extern bool IsOptionalAddressRange(const int index); +extern bool VerifyWow64(); + extern LPTSTR GetLastErrorString(); extern void FreeLastErrorString(LPTSTR Error); diff --git a/src/gui/WinMain.cpp b/src/gui/WinMain.cpp index 51207ba43..3202423b5 100644 --- a/src/gui/WinMain.cpp +++ b/src/gui/WinMain.cpp @@ -27,6 +27,7 @@ #include "WndMain.h" +#include "AddressRanges.h" // For VerifyWow64() #include "core\kernel\init\CxbxKrnl.h" #include "core\kernel\support\Emu.h" #include "EmuShared.h" @@ -45,14 +46,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine // First detect if we are running on WoW64, if not, prevent Cxbx-Reloaded from starting // Cxbx-Relaoded needs access to high memory, only exposed to WoW64. - typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); - BOOL bIsWow64 = FALSE; - LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process"); - if (fnIsWow64Process != nullptr) { - fnIsWow64Process(GetCurrentProcess(), &bIsWow64); - } - - if (bIsWow64 == FALSE) { + if (!VerifyWow64()) { MessageBox(NULL, "Cxbx-Reloaded can only run under WoW64\nThis means either a 64-bit version of Windows or Wine with a 64-bit prefix", "Cxbx-Reloaded", MB_OK | MB_ICONERROR); return EXIT_FAILURE; diff --git a/src/loader/loader.cpp b/src/loader/loader.cpp index 7f3b023b8..5cdfdf0f7 100644 --- a/src/loader/loader.cpp +++ b/src/loader/loader.cpp @@ -117,15 +117,10 @@ DWORD CALLBACK rawMain() { (void)virtual_memory_placeholder; // prevent optimization removing this data - // Verify we're running under WOW64 - BOOL bIsWow64Process; - - if (!IsWow64Process(GetCurrentProcess(), &bIsWow64Process)) { - bIsWow64Process = false; - } - - if (!bIsWow64Process) { - OutputMessage("Not running as a WOW64 process!\n"); + // First detect if we are running on WoW64, if not, prevent Cxbx-Reloaded from starting + // Cxbx-Relaoded needs access to high memory, only exposed to WoW64. + if (!VerifyWow64()) { + OutputMessage("Cxbx-Reloaded can only run under WoW64\nThis means either a 64-bit version of Windows or Wine with a 64-bit prefix\n"); return ERROR_BAD_ENVIRONMENT; } @@ -169,7 +164,7 @@ DWORD CALLBACK rawMain() } // Only after the required memory ranges are reserved, load our emulation DLL - HMODULE hEmulationDLL = LoadLibraryA("CxbxEmulator.dll"); + HMODULE hEmulationDLL = LoadLibrary(TEXT("CxbxEmulator.dll")); if (!hEmulationDLL) { OutputMessage("Error loading CxbxEmulator.dll\n"); LPTSTR Error = GetLastErrorString();