在 Windows 2008 R2 SP1 上使用 GetVersionEx 时出现错误行为
我想检查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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否咨询一下您的客户,他们的服务器 2008 R2 是否配置为域控制器?
因为在结构文档中 OSVERSIONINFOEX表明,在wProductType/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:
And in this case, your code will not give the expected result.