检查Windows版本

时间:2018-06-13 16:18:02

标签: c++ wxwidgets windowsversion

我想要为Windows-XP执行代码,但我必须检查当前的Windows是否是Windows-XP。

wxPlatformInfo windows = wxPlatformInfo::Get();
int winXP[] = { 5,1 };
int winXP64[] = { 5,2 };
if (windows.GetOSMajorVersion() == winXP[0] && windows.GetOSMinorVersion() == winXP[1]) {
    wxLogMessage("Windows XP");
} else if (windows.GetOSMajorVersion() == winXP64[0] && windows.GetOSMinorVersion() == winXP64[1]) {
    wxLogMessage("Windows XP 64Bit");
}

以前的代码为我提供了解Windows版本的直接方法。

还有另一种简短的方法可以提供Windows版本吗?
但不要忘记(== window-XP不是> = windows-XP)?

3 个答案:

答案 0 :(得分:1)

BOOL WINAPI IsWindowsXPOrGreater(void); 在这里查看其他人:https://msdn.microsoft.com/en-us/library/windows/desktop/dn424972(v=vs.85).aspx

答案 1 :(得分:0)

您已经拥有的内容尽可能短,但您应该缓存wxPlatformInfo的结果:

wxPlatformInfo windows = wxPlatformInfo::Get();
int major = windows.GetOSMajorVersion();
int minor = windows.GetOSMinorVersion();
if (major == 5 && minor == 1) {
    wxLogMessage("Windows XP");
} else if (major == 5 && minor == 2) {
    wxLogMessage("Windows XP 64Bit");
}

否则,请考虑使用VerifyVersionInfo()让操作系统为您比较版本号:

bool IsWinXP32Bit()
{
   OSVERSIONINFOEX osvi = {};
   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
   osvi.dwMajorVersion = 5;
   osvi.dwMinorVersion = 1;
   osvi.wServicePackMajor = 0;
   osvi.wServicePackMinor = 0;

   DWORDLONG dwlConditionMask = 0;
   VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_EQUAL);
   VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_EQUAL);
   VER_SET_CONDITION(dwlConditionMask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
   VER_SET_CONDITION(dwlConditionMask, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL);

   return VerifyVersionInfo(&osvi,
      VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR,
      dwlConditionMask);
}

bool IsWinXP64Bit()
{
   OSVERSIONINFOEX osvi = {};
   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
   osvi.dwMajorVersion = 5;
   osvi.dwMinorVersion = 2;
   osvi.wServicePackMajor = 0;
   osvi.wServicePackMinor = 0;

   DWORDLONG dwlConditionMask = 0;
   VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_EQUAL);
   VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_EQUAL);
   VER_SET_CONDITION(dwlConditionMask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
   VER_SET_CONDITION(dwlConditionMask, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL);

   return VerifyVersionInfo(&osvi,
      VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR,
      dwlConditionMask);
}

...

if (IsWinXP32Bit())
    wxLogMessage("Windows XP");
} else if (IsWinXP64Bit()) {
    wxLogMessage("Windows XP 64Bit");
}

答案 2 :(得分:-2)

如果您不反对使用宏,则可以使用

#ifdef WINDOWS_XP
  // Code
#elif defined WINDOWS_7
  // Other code
#endif