2

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)?

Lion King
  • 32,851
  • 25
  • 81
  • 143
  • According to [the documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396) XP is both 5.1 and 5.2. – tadman Jun 13 '18 at 16:22
  • @tadman: For that reason, I want another short way. – Lion King Jun 13 '18 at 16:25
  • Your code is right. In a portable app you should also check the return of [wxPlatformInfo::GetOperatingSystemId](http://docs.wxwidgets.org/trunk/classwx_platform_info.html#ab70a9c0bac9a38f05930e5f05a153cff) – Ripi2 Jun 13 '18 at 17:43

3 Answers3

1

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

ROCFER
  • 267
  • 2
  • 9
  • I saw that functions before, but the problem that all of them "OrGreater ". Please check the last line of my question. – Lion King Jun 13 '18 at 16:35
  • 1
    you should create that like here , https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx – ROCFER Jun 13 '18 at 16:41
  • I am looking for a short code, but that API code is too long. My question is "is there another short way that gives me the windows version?" – Lion King Jun 13 '18 at 16:47
0

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");
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
-2

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

#ifdef WINDOWS_XP
  // Code
#elif defined WINDOWS_7
  // Other code
#endif
mkamerath
  • 312
  • 2
  • 12
  • This won't get the version of windows running the application; only if they've been set on the build machine – UKMonkey Jun 13 '18 at 16:36