简体   繁体   中英

Incorrect behavior while using GetVersionEx on Windows 2008 R2 SP1

I want to check if Windows operating system's version is Windows 2008 or Above. I am using following piece of code, it works just fine in my environment but someone(customer) has reported that it's not working on their production OS environment but works on other systems having Windows 2008 R2 SP1. It's not working means it returns false even in case OS is Windows 2008 R2 SP1. What's wrong with the code?

bool CheckIfOperatingISWindowsServer2K8orAbove()
{
  OSVERSIONINFOEX winOSInfo;
  winOSInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFOEX);
  GetVersionEx(&winOSInfo);

  //Check if windows version is 6 (i.e longhorn) and its windows server 
  if( winOSInfo.dwPlatformId==VER_PLATFORM_WIN32_NT && winOSInfo.dwMajorVersion == 6 && winOSInfo.wProductType == VER_NT_SERVER)
  {
     if ( winOSInfo.dwMinorVersion == 0 || winOSInfo.dwMinorVersion == 1 ) 
      return true;  
  }

  return false;
}

I think of only missing part is not initializing winOSInfo to value 0 using ZeroMemory(&winfo, sizeof(OSVERSIONINFOEX));

What's your opinion? Do you think not initializing OSVERSIONINFOEX structure causes this kind of issues?

Thanks in advance.

Could you check with your customer if their server 2008 R2 is configured as domain controller?

Because in the documentation of the structure OSVERSIONINFOEX it is indicated, in wProductType/VER_NT_SERVER:

Note that a server that is also a domain controller is reported as VER_NT_DOMAIN_CONTROLLER, not VER_NT_SERVER.

And in this case, your code will not give the expected result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM