From 247859b5f299714e5b2f36339650a9270083a8c0 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Sat, 1 Aug 2015 13:38:14 -0700 Subject: [PATCH] Get windows version via kernel32.dll Microsoft basically deprecated GetVersion/GetVersionEx so now they'll always report invalid version numbers. The way to get the actual version of windows after 8+ is to get the actual file version of kernel32.dll according to microsoft's documentation. --- Source/Main.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/Source/Main.cpp b/Source/Main.cpp index 8fe642312..3041e2356 100644 --- a/Source/Main.cpp +++ b/Source/Main.cpp @@ -99,6 +99,67 @@ static void LogModule(CTSTR lpModuleName, HMODULE addr) #endif } +typedef DWORD (WINAPI *GETFILEVERSIONINFOSIZEWPROC)(LPCWSTR module, DWORD unused); +typedef DWORD (WINAPI *GETFILEVERSIONINFOWPROC)(LPCWSTR module, DWORD unused, DWORD len, LPVOID data); +typedef DWORD (WINAPI *VERQUERYVALUEWPROC)(LPVOID data, LPCWSTR subblock, LPVOID *buf, PUINT sizeout); + +static void LogWindowsVersion() +{ + HMODULE ver_module = GetModuleHandleW(L"version"); + if (!ver_module) + { + ver_module = LoadLibraryW(L"version"); + if (!ver_module) + { + Log(L"Couldn't get version module"); + return; + } + } + + GETFILEVERSIONINFOSIZEWPROC pGetFileVersionInfoSizeW = (GETFILEVERSIONINFOSIZEWPROC)GetProcAddress(ver_module, "GetFileVersionInfoSizeW"); + GETFILEVERSIONINFOWPROC pGetFileVersionInfoW = (GETFILEVERSIONINFOWPROC)GetProcAddress(ver_module, "GetFileVersionInfoW"); + VERQUERYVALUEWPROC pVerQueryValueW = (VERQUERYVALUEWPROC)GetProcAddress(ver_module, "VerQueryValueW"); + + if (!pGetFileVersionInfoSizeW || !pGetFileVersionInfoW || !pVerQueryValueW) + { + Log(L"Couldn't get version functions"); + return; + } + + DWORD size = pGetFileVersionInfoSizeW(L"kernel32", 0); + VS_FIXEDFILEINFO *info = NULL; + UINT len; + + if (!size) + { + Log(L"Couldn't get windows version info size"); + return; + } + + LPVOID data = malloc(size); + if (!pGetFileVersionInfoW(L"kernel32", 0, size, data)) + { + Log(L"Couldn't get windows version info"); + free(data); + return; + } + BOOL success = pVerQueryValueW(data, L"\\", (LPVOID*)&info, &len); + if (!success || !info || !len) + { + Log(L"Couldn't query version value"); + free(data); + return; + } + + WORD major = HIWORD(info->dwFileVersionMS); + WORD minor = LOWORD(info->dwFileVersionMS); + WORD build = HIWORD(info->dwFileVersionLS); + WORD revis = LOWORD(info->dwFileVersionLS); + + Log(TEXT("Windows Version: %u.%u Build %u (revision %d)"), major, minor, build, revis); + free(data); +} + void LogSystemStats() { HKEY key; @@ -155,10 +216,7 @@ void LogSystemStats() Log(TEXT("monitor %u: pos={%d, %d}, size={%d, %d}"), i+1, info.rect.left, info.rect.top, info.rect.right-info.rect.left, info.rect.bottom-info.rect.top); } - OSVERSIONINFO osvi; - osvi.dwOSVersionInfoSize = sizeof(osvi); - GetVersionEx(&osvi); - Log(TEXT("Windows Version: %u.%u Build %u %S"), osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber, osvi.szCSDVersion); + LogWindowsVersion(); BOOL bComposition; DwmIsCompositionEnabled(&bComposition);