Skip to content

Commit

Permalink
Get windows version via kernel32.dll
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jp9000 committed Aug 1, 2015
1 parent 8cbe130 commit 247859b
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions Source/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 247859b

Please sign in to comment.