Get Windows version in C

I am not gonna use GetVersionEx or GetVersion. Both these functions may be altered or unavailable for releases after Windows 8.1. I am not gonna use either Version Helper functions.

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <stdbool.h>
 
 
#define WIN_XP			0x0501
#define WIN_XP64PRO		0x0502
#define WIN_VISTA		0x0600
#define WIN_WIN7		0x0601
#define WIN_WIN8		0x0602
#define WIN_WIN81		0x0603
 
#define WIN_S03			0x0502
#define WIN_S08			0x0600
#define WIN_S08R2		0x0601
#define WIN_S12			0x0602
#define WIN_S12R2		0x0603
 
bool IsWindowsVersion(WORD wMajorVersion, WORD wMinorVersion, WORD wServicePackMajor)
{
    OSVERSIONINFOEXW osverinfex = { sizeof(osverinfex), 0, 0, 0, 0, {0}, 0, 0 };
    DWORDLONG        const dwlConditionMask = VerSetConditionMask(
        VerSetConditionMask(
        VerSetConditionMask(
            0, VER_MAJORVERSION, VER_EQUAL),
               VER_MINORVERSION, VER_EQUAL),
               VER_SERVICEPACKMAJOR, VER_EQUAL);
 
    osverinfex.dwMajorVersion = wMajorVersion;
    osverinfex.dwMinorVersion = wMinorVersion;
    osverinfex.wServicePackMajor = wServicePackMajor;
 
    return VerifyVersionInfoW(&osverinfex, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != false;
}
 
bool IsWindowsServer()
{
    OSVERSIONINFOEXW osverinfex = { sizeof(osverinfex), 0, 0, 0, 0, {0}, 0, 0, 0, VER_NT_WORKSTATION };
    DWORDLONG        const dwlConditionMask = VerSetConditionMask( 0, VER_PRODUCT_TYPE, VER_EQUAL );
    return !VerifyVersionInfoW(&osverinfex, VER_PRODUCT_TYPE, dwlConditionMask);
}
 
 
void GetOsVersion() {
  if(!IsWindowsServer()) {
    if(IsWindowsVersion(HIBYTE(WIN_XP), LOBYTE(WIN_XP), 0)) printf("Windows XP\n");
    else if(IsWindowsVersion(HIBYTE(WIN_XP64PRO), LOBYTE(WIN_XP64PRO), 0)) printf("Windows XP 64 PRO\n");
    else if(IsWindowsVersion(HIBYTE(WIN_XP), LOBYTE(WIN_XP), 1)) printf("Windows XP SP1\n");
    else if(IsWindowsVersion(HIBYTE(WIN_XP), LOBYTE(WIN_XP), 2)) printf("Windows XP SP2\n");
    else if(IsWindowsVersion(HIBYTE(WIN_XP), LOBYTE(WIN_XP), 3)) printf("Windows XP SP3\n");
    else if(IsWindowsVersion(HIBYTE(WIN_VISTA), LOBYTE(WIN_VISTA), 0)) printf("Windows VISTA\n");
    else if(IsWindowsVersion(HIBYTE(WIN_VISTA), LOBYTE(WIN_VISTA), 1)) printf("Windows VISTA SP1\n");
    else if(IsWindowsVersion(HIBYTE(WIN_VISTA), LOBYTE(WIN_VISTA), 2)) printf("Windows VISTA SP2\n");
    else if(IsWindowsVersion(HIBYTE(WIN_WIN7), LOBYTE(WIN_WIN7), 0)) printf("Windows 7\n");
    else if(IsWindowsVersion(HIBYTE(WIN_WIN7), LOBYTE(WIN_WIN7), 1)) printf("Windows 7 SP1\n");
    else if(IsWindowsVersion(HIBYTE(WIN_WIN8), LOBYTE(WIN_WIN8), 0)) printf("Windows 8\n");
    else if(IsWindowsVersion(HIBYTE(WIN_WIN81), LOBYTE(WIN_WIN81), 0)) printf("Windows 8.1\n");
    else printf("Windows Unknown\n");
  }
  else {
      if(IsWindowsVersion(HIBYTE(WIN_S03), LOBYTE(WIN_S03), 0)) printf("Windows Server 2003\n");
      else if(IsWindowsVersion(HIBYTE(WIN_S08), LOBYTE(WIN_S08), 0)) printf("Windows Server 2008\n");
      else if(IsWindowsVersion(HIBYTE(WIN_S08R2), LOBYTE(WIN_S08R2), 0)) printf("Windows Server 2008 R2\n");
      else if(IsWindowsVersion(HIBYTE(WIN_S12), LOBYTE(WIN_S12), 0)) printf("Windows Server 2012\n");
      else if(IsWindowsVersion(HIBYTE(WIN_S12R2), LOBYTE(WIN_S12R2), 0)) printf("Windows Server 2012 R2\n");
      else printf("Windows Unknown\n");
  }
}
 
int main(void) {
	GetOsVersion();
	return EXIT_SUCCESS;
}
Categories: C Tags: , , ,
%d bloggers like this: