Skip to content

Commit d042104

Browse files
authored
fix(cpudetect): Fix version and memory detection (#1393)
Memory detection was incorrect on systems with > 4GB. Version info detection was incorrect on Windows 8 and later and if manifested for an earlier version than currently running on.
1 parent 31f3a5c commit d042104

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

Core/Libraries/Source/WWVegas/WWLib/cpudetect.cpp

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,21 @@ unsigned CPUDetectClass::L1InstructionCacheSetAssociative;
7878
unsigned CPUDetectClass::L1InstructionTraceCacheSize;
7979
unsigned CPUDetectClass::L1InstructionTraceCacheSetAssociative;
8080

81+
#if defined(_MSC_VER) && _MSC_VER < 1300
8182
unsigned CPUDetectClass::TotalPhysicalMemory;
8283
unsigned CPUDetectClass::AvailablePhysicalMemory;
8384
unsigned CPUDetectClass::TotalPageMemory;
8485
unsigned CPUDetectClass::AvailablePageMemory;
8586
unsigned CPUDetectClass::TotalVirtualMemory;
8687
unsigned CPUDetectClass::AvailableVirtualMemory;
88+
#else
89+
unsigned long long CPUDetectClass::TotalPhysicalMemory;
90+
unsigned long long CPUDetectClass::AvailablePhysicalMemory;
91+
unsigned long long CPUDetectClass::TotalPageMemory;
92+
unsigned long long CPUDetectClass::AvailablePageMemory;
93+
unsigned long long CPUDetectClass::TotalVirtualMemory;
94+
unsigned long long CPUDetectClass::AvailableVirtualMemory;
95+
#endif
8796

8897
unsigned CPUDetectClass::OSVersionNumberMajor;
8998
unsigned CPUDetectClass::OSVersionNumberMinor;
@@ -885,6 +894,7 @@ void CPUDetectClass::Init_Memory()
885894
{
886895
#ifdef WIN32
887896

897+
#if defined(_MSC_VER) && _MSC_VER < 1300
888898
MEMORYSTATUS mem;
889899
GlobalMemoryStatus(&mem);
890900

@@ -894,14 +904,32 @@ void CPUDetectClass::Init_Memory()
894904
AvailablePageMemory = mem.dwAvailPageFile;
895905
TotalVirtualMemory = mem.dwTotalVirtual;
896906
AvailableVirtualMemory = mem.dwAvailVirtual;
897-
#elif defined(_UNIX)
907+
#else
908+
MEMORYSTATUSEX mem;
909+
mem.dwLength = sizeof(mem);
910+
GlobalMemoryStatusEx(&mem);
911+
912+
TotalPhysicalMemory = mem.ullTotalPhys;
913+
AvailablePhysicalMemory = mem.ullAvailPhys;
914+
TotalPageMemory = mem.ullTotalPageFile;
915+
AvailablePageMemory = mem.ullAvailPageFile;
916+
TotalVirtualMemory = mem.ullTotalVirtual;
917+
AvailableVirtualMemory = mem.ullAvailVirtual;
918+
#endif // defined(_MSC_VER) && _MSC_VER < 1300
919+
920+
#else
898921
#warning FIX Init_Memory()
899-
#endif
922+
#endif // WIN32
900923
}
901924

902925
void CPUDetectClass::Init_OS()
903926
{
904927
#ifdef WIN32
928+
929+
// TheSuperHackers @fix OmniBlade 30/07/2025
930+
// GetVersionEx only returns the version of Windows it was manifested for since Windows 8.
931+
// RtlGetVersion returns the correct information at least at the time of writing.
932+
#if defined(_MSC_VER) && _MSC_VER < 1300
905933
OSVERSIONINFO os;
906934
os.dwOSVersionInfoSize = sizeof(os);
907935
GetVersionEx(&os);
@@ -911,7 +939,33 @@ void CPUDetectClass::Init_OS()
911939
OSVersionBuildNumber = os.dwBuildNumber;
912940
OSVersionPlatformId = os.dwPlatformId;
913941
OSVersionExtraInfo = os.szCSDVersion;
914-
#elif defined(_UNIX)
942+
#else
943+
typedef LONG(WINAPI * RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
944+
HMODULE ntdll = LoadLibraryExA("ntdll", NULL, 0);
945+
if (ntdll != nullptr) {
946+
RtlGetVersionPtr RtlGetVersion = (RtlGetVersionPtr)::GetProcAddress(ntdll, "RtlGetVersion");
947+
948+
if (RtlGetVersion != nullptr) {
949+
RTL_OSVERSIONINFOW os = {0};
950+
os.dwOSVersionInfoSize = sizeof(os);
951+
RtlGetVersion(&os);
952+
OSVersionNumberMajor = os.dwMajorVersion;
953+
OSVersionNumberMinor = os.dwMinorVersion;
954+
OSVersionBuildNumber = os.dwBuildNumber;
955+
OSVersionPlatformId = os.dwPlatformId;
956+
OSVersionExtraInfo = os.szCSDVersion;
957+
return;
958+
}
959+
}
960+
961+
// GetVersionEx will return this info if no manifest is present so seems a safe fallback.
962+
OSVersionNumberMajor = 6;
963+
OSVersionNumberMinor = 2;
964+
OSVersionBuildNumber = 0;
965+
OSVersionPlatformId = 2;
966+
OSVersionExtraInfo = "";
967+
#endif // defined(_MSC_VER) && _MSC_VER < 1300
968+
#else
915969
#warning FIX Init_OS()
916970
#endif
917971
}

Core/Libraries/Source/WWVegas/WWLib/cpudetect.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,22 @@ class CPUDetectClass
194194
static unsigned Get_L1_Instruction_Trace_Cache_Set_Associative() { return L1InstructionTraceCacheSetAssociative; }
195195

196196
// System memory
197+
198+
#if defined(_MSC_VER) && _MSC_VER < 1300
197199
static unsigned Get_Total_Physical_Memory() { return TotalPhysicalMemory; }
198200
static unsigned Get_Available_Physical_Memory() { return AvailablePhysicalMemory; }
199201
static unsigned Get_Total_Page_File_Size() { return TotalPageMemory; }
200202
static unsigned Get_Available_Page_File_Size() { return AvailablePageMemory; }
201203
static unsigned Get_Total_Virtual_Memory() { return TotalVirtualMemory; }
202204
static unsigned Get_Available_Virtual_Memory() { return AvailableVirtualMemory; }
205+
#else
206+
static unsigned long long Get_Total_Physical_Memory() { return TotalPhysicalMemory; }
207+
static unsigned long long Get_Available_Physical_Memory() { return AvailablePhysicalMemory; }
208+
static unsigned long long Get_Total_Page_File_Size() { return TotalPageMemory; }
209+
static unsigned long long Get_Available_Page_File_Size() { return AvailablePageMemory; }
210+
static unsigned long long Get_Total_Virtual_Memory() { return TotalVirtualMemory; }
211+
static unsigned long long Get_Available_Virtual_Memory() { return AvailableVirtualMemory; }
212+
#endif
203213

204214
static unsigned Get_Processor_Type() { return ProcessorType; }
205215

@@ -278,14 +288,21 @@ class CPUDetectClass
278288
// L1 instruction trace cache information
279289
static unsigned L1InstructionTraceCacheSize;
280290
static unsigned L1InstructionTraceCacheSetAssociative;
281-
291+
#if defined(_MSC_VER) && _MSC_VER < 1300
282292
static unsigned TotalPhysicalMemory;
283293
static unsigned AvailablePhysicalMemory;
284294
static unsigned TotalPageMemory;
285295
static unsigned AvailablePageMemory;
286296
static unsigned TotalVirtualMemory;
287297
static unsigned AvailableVirtualMemory;
288-
298+
#else
299+
static unsigned long long TotalPhysicalMemory;
300+
static unsigned long long AvailablePhysicalMemory;
301+
static unsigned long long TotalPageMemory;
302+
static unsigned long long AvailablePageMemory;
303+
static unsigned long long TotalVirtualMemory;
304+
static unsigned long long AvailableVirtualMemory;
305+
#endif
289306
static unsigned OSVersionNumberMajor;
290307
static unsigned OSVersionNumberMinor;
291308
static unsigned OSVersionBuildNumber;

0 commit comments

Comments
 (0)