How to detect true Windows version?

2019-04-15 21:25发布

I know I can call the GetVersionEx Win32 API function to retrieve Windows version. In most cases returned value reflects the version of my Windows, but sometimes that is not so.

If a user runs my application under the compatibility layer, then GetVersionEx won't be reporting the real version but the version enforced by the compatibility layer. For example, if I'm running Vista and execute my program in "Windows NT 4" compatibility mode, GetVersionEx won't return version 6.0 but 4.0.

Is there a way to bypass this behaviour and get true Windows version?

9条回答
等我变得足够好
2楼-- · 2019-04-15 22:16

One note about using NetServerGetInfo(), which does work still on Windows 10 (10240.th1_st1)...

https://msdn.microsoft.com/en-us/library/windows/desktop/aa370903%28v=vs.85%29.aspx

sv101_version_major

The major version number and the server type.

The major release version number of the operating system is specified in the least significant 4 bits. The server type is specified in the most significant 4 bits. The MAJOR_VERSION_MASK bitmask defined in the Lmserver.h header {0x0F} should be used by an application to obtain the major version number from this member.

In other words, (sv101_version_major & MAJOR_VERSION_MASK).

查看更多
Lonely孤独者°
3楼-- · 2019-04-15 22:20

Another solution:

read the following registry entry:

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName

or other keys from

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion
查看更多
爷、活的狠高调
4楼-- · 2019-04-15 22:20

Essentially to answer duplicate Q: Getting OS major, minor, and build versions for Windows 8.1 and up in Delphi 2007

Starting with W2K you can use NetServerGetInfo. NetServerGetInfo returns the correct info on W7 and W8.1, unable to test on W10..

function GetWinVersion: string;
var
  Buffer: PServerInfo101;
begin
  Buffer := nil;
  if NetServerGetInfo(nil, 101, Pointer(Buffer)) = NO_ERROR then
  try
     Result := <Build You Version String here>(
      Buffer.sv101_version_major,
      Buffer.sv101_version_minor,
      VER_PLATFORM_WIN32_NT // Save since minimum support begins in W2K
      );
  finally
    NetApiBufferFree(Buffer);
  end;
end;
查看更多
登录 后发表回答