tags:

views:

167

answers:

2

How would one programmaticly determine the version of windows currently installed? As in differentiate between vista and xp.

+3  A: 

If you are using Win32 then you can use GetVersionEx API to identify the OS.

Naveen
+1  A: 

The immediate question here is what are you trying to test? It's better to check for features than compare version numbers (since there are various cut-down versions of Windows).

VerifyVersionInfo will do both, and is the recommended way to compare major/minor numbers.

BOOL IsAtLeast2008DC() {

  OSVERSIONINFOEX osvi = {0};
  DWORDLONG mask = 0;

  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  osvi.dwMajorVersion = 6;
  osvi.dwMinorVersion = 0;
  osvi.wServicePackMajor = 0;
  osvi.wServicePackMinor = 0;
  osvi.wProductType = VER_NT_DOMAIN_CONTROLLER;


  VER_SET_CONDITION(mask, VER_MAJORVERSION, VER_GREATER_EQUAL);
  VER_SET_CONDITION(mask, VER_MINORVERSION, VER_GREATER_EQUAL);
  VER_SET_CONDITION(mask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
  VER_SET_CONDITION(mask, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL);
  VER_SET_CONDITION(mask, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL);
  VER_SET_CONDITION(mask, VER_PRODUCT_TYPE, VER_EQUAL);

  return VerifyVersionInfo(&osvi,
      VER_MAJORVERSION | VER_MINORVERSION |
      VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR |
      VER_PRODUCT_TYPE, 
      mask
      );

}

To just test for Vista or later, remove lines that mention product type. You'll find more examples here, but be careful about what you're checking for.

Mark