From 2ce97329a9daafd6a9108b8401d30c6b7b013557 Mon Sep 17 00:00:00 2001 From: narzoul Date: Sat, 20 Jun 2020 13:26:02 +0200 Subject: [PATCH] Limit GlobalMemoryStatus values to 2GB --- DDrawCompat/DDrawCompat.vcxproj | 2 ++ DDrawCompat/DDrawCompat.vcxproj.filters | 6 ++++ DDrawCompat/Dll/DllMain.cpp | 2 ++ DDrawCompat/Win32/Log.cpp | 13 ++++++++ DDrawCompat/Win32/Log.h | 1 + DDrawCompat/Win32/MemoryManagement.cpp | 41 +++++++++++++++++++++++++ DDrawCompat/Win32/MemoryManagement.h | 9 ++++++ 7 files changed, 74 insertions(+) create mode 100644 DDrawCompat/Win32/MemoryManagement.cpp create mode 100644 DDrawCompat/Win32/MemoryManagement.h diff --git a/DDrawCompat/DDrawCompat.vcxproj b/DDrawCompat/DDrawCompat.vcxproj index b5e74e5..b1911d4 100644 --- a/DDrawCompat/DDrawCompat.vcxproj +++ b/DDrawCompat/DDrawCompat.vcxproj @@ -231,6 +231,7 @@ + @@ -303,6 +304,7 @@ + diff --git a/DDrawCompat/DDrawCompat.vcxproj.filters b/DDrawCompat/DDrawCompat.vcxproj.filters index d7d0a9b..cee5d4f 100644 --- a/DDrawCompat/DDrawCompat.vcxproj.filters +++ b/DDrawCompat/DDrawCompat.vcxproj.filters @@ -393,6 +393,9 @@ Header Files\D3dDdi + + Header Files\Win32 + @@ -605,5 +608,8 @@ Source Files\D3dDdi + + Source Files\Win32 + \ No newline at end of file diff --git a/DDrawCompat/Dll/DllMain.cpp b/DDrawCompat/Dll/DllMain.cpp index 5edc9ef..98869d2 100644 --- a/DDrawCompat/Dll/DllMain.cpp +++ b/DDrawCompat/Dll/DllMain.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -273,6 +274,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) setDpiAwareness(); SetThemeAppProperties(0); + Win32::MemoryManagement::installHooks(); Win32::MsgHooks::installHooks(); Time::init(); diff --git a/DDrawCompat/Win32/Log.cpp b/DDrawCompat/Win32/Log.cpp index 709f30c..d59b68d 100644 --- a/DDrawCompat/Win32/Log.cpp +++ b/DDrawCompat/Win32/Log.cpp @@ -110,6 +110,19 @@ std::ostream& operator<<(std::ostream& os, HWND hwnd) << Compat::hex(GetWindowLong(hwnd, GWL_EXSTYLE)); } +std::ostream& operator<<(std::ostream& os, const MEMORYSTATUS& ms) +{ + return Compat::LogStruct(os) + << ms.dwLength + << ms.dwMemoryLoad + << ms.dwTotalPhys + << ms.dwAvailPhys + << ms.dwTotalPageFile + << ms.dwAvailPageFile + << ms.dwTotalVirtual + << ms.dwAvailVirtual; +} + std::ostream& operator<<(std::ostream& os, const MSG& msg) { return Compat::LogStruct(os) diff --git a/DDrawCompat/Win32/Log.h b/DDrawCompat/Win32/Log.h index 6a949ce..ca0f5ca 100644 --- a/DDrawCompat/Win32/Log.h +++ b/DDrawCompat/Win32/Log.h @@ -12,6 +12,7 @@ std::ostream& operator<<(std::ostream& os, const DEVMODEW& dm); std::ostream& operator<<(std::ostream& os, HDC dc); std::ostream& operator<<(std::ostream& os, HRGN rgn); std::ostream& operator<<(std::ostream& os, HWND hwnd); +std::ostream& operator<<(std::ostream& os, const MEMORYSTATUS& ms); std::ostream& operator<<(std::ostream& os, const MSG& msg); std::ostream& operator<<(std::ostream& os, const RECT& rect); diff --git a/DDrawCompat/Win32/MemoryManagement.cpp b/DDrawCompat/Win32/MemoryManagement.cpp new file mode 100644 index 0000000..ce53367 --- /dev/null +++ b/DDrawCompat/Win32/MemoryManagement.cpp @@ -0,0 +1,41 @@ +#include + +#include +#include +#include + +namespace +{ + void limitTo2Gb(SIZE_T& mem); + + void WINAPI globalMemoryStatus(LPMEMORYSTATUS lpBuffer) + { + LOG_FUNC("GlobalMemoryStatus", lpBuffer); + CALL_ORIG_FUNC(GlobalMemoryStatus)(lpBuffer); + limitTo2Gb(lpBuffer->dwTotalPhys); + limitTo2Gb(lpBuffer->dwAvailPhys); + limitTo2Gb(lpBuffer->dwTotalPageFile); + limitTo2Gb(lpBuffer->dwAvailPageFile); + limitTo2Gb(lpBuffer->dwTotalVirtual); + limitTo2Gb(lpBuffer->dwAvailVirtual); + } + + void limitTo2Gb(SIZE_T& mem) + { + if (mem > INT_MAX) + { + mem = INT_MAX; + } + } +} + +namespace Win32 +{ + namespace MemoryManagement + { + void installHooks() + { + HOOK_FUNCTION(kernel32, GlobalMemoryStatus, globalMemoryStatus); + } + } +} diff --git a/DDrawCompat/Win32/MemoryManagement.h b/DDrawCompat/Win32/MemoryManagement.h new file mode 100644 index 0000000..94003c5 --- /dev/null +++ b/DDrawCompat/Win32/MemoryManagement.h @@ -0,0 +1,9 @@ +#pragma once + +namespace Win32 +{ + namespace MemoryManagement + { + void installHooks(); + } +}