在 Windows 2008 R2 SP1 上使用 GetVersionEx 时出现错误行为

发布于 2024-12-21 00:38:17 字数 888 浏览 0 评论 0原文

我想检查Windows操作系统的版本是否为Windows 2008或更高版本。我正在使用下面的代码,它在我的环境中运行得很好,但有人(客户)报告说它不能在他们的生产操作系统环境中运行,但可以在具有 Windows 2008 R2 SP1 的其他系统上运行。它不起作用意味着即使操作系统是 Windows 2008 R2 SP1,它也会返回 false。代码有什么问题吗?

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;
}

我认为唯一缺少的部分是没有使用 ZeroMemory(&winfo, sizeof(OSVERSIONINFOEX)); 将 winOSInfo 初始化为值 0

你有什么意见?您认为不初始化 OSVERSIONINFOEX 结构会导致此类问题吗?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哽咽笑 2024-12-28 00:38:17

您能否咨询一下您的客户,他们的服务器 2008 R2 是否配置为域控制器?

因为在结构文档中 OSVERSIONINFOEX表明,在wProductType/VER_NT_SERVER中:

请注意,同时也是域控制器的服务器被报告为 VER_NT_DOMAIN_CONTROLLER,而不是 VER_NT_SERVER。

在这种情况下,您的代码将不会给出预期的结果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文