简体   繁体   中英

Checking the Windows version

I want a code to be executed just for Windows-XP but I must check whether the current windows is Windows-XP or not.

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

The previous code gives me the direct way to know the windows version.

is there another short way that gives me the windows version?
But don't forget (== window-XP not >= windows-XP)?

BOOL WINAPI IsWindowsXPOrGreater(void); check others here : https://msdn.microsoft.com/en-us/library/windows/desktop/dn424972(v=vs.85).aspx

What you already have is about as short as you can get, though you should cache the results from 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");
}

Otherwise, consider using VerifyVersionInfo() to let the OS compare the version numbers for you:

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

If you're not against using macros, you can use

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

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