3 >>> This code is released to the public domain. No warranty
4 whatsoever is provided. Use it at your own risk. <<<
12 Determine the Windows version number running and set the
13 errorlevel appropriately. Intended for use with batch scripts.
28 // This code _should_ run on win9x/ME or any version of NT. Note: I have
29 // not personally tested it on 9x/ME though. It prints out a message on
30 // stdout about which version of the windows operating system is running
31 // and sets the errorlevel to one of the values below accordingly. Might
32 // be useful in your CMD/BAT script...
34 #define EXIT_WIN95 (1)
35 #define EXIT_WIN98 (2)
36 #define EXIT_WINME (3)
37 #define EXIT_CONSUMER_OTHER (4)
38 #define EXIT_NT351 (5)
39 #define EXIT_NT3OTHER (6)
41 #define EXIT_NT4OTHER (8)
42 #define EXIT_WIN2K (9)
43 #define EXIT_WINXP (10)
44 #define EXIT_WIN_SERVER_2003 (11)
45 #define EXIT_NT5OTHER (12)
46 #define EXIT_VISTA (13)
47 #define EXIT_UNKNOWN (14)
49 CHAR *g_szWindowsNames[] =
55 "Unknown Consumer Windows Release",
62 "Windows Server 2003",
64 "Longhorn Prerelease",
65 "Unknown Operating System"
68 typedef BOOL (*FUNCTION_PTR)(OSVERSIONINFO *);
72 TryWinNT(DWORD *pdwMajor, DWORD *pdwMinor, DWORD *pdwPlatform)
77 GetVersionEx is not present on downlevel versions of Windows.
78 Deal with this by using LoadLibrary/GetProcAddress.
95 OSVERSIONINFOEXA osvi;
97 h = LoadLibraryA("kernel32.dll");
100 fprintf(stderr, "Failed to load kernel32.dll, error=%u.\n",
105 p = (FUNCTION_PTR)GetProcAddress(h, "GetVersionExA");
108 fprintf(stderr, "GetProcAddress for GetVersionEx failed, error=%u.\n",
113 ZeroMemory(&osvi, sizeof(osvi));
114 osvi.dwOSVersionInfoSize = sizeof(osvi);
115 fRet = (*p)((OSVERSIONINFO *)&osvi);
118 fprintf(stderr, "GetVersionEx failed, error=%u.\n", GetLastError());
121 *pdwMajor = osvi.dwMajorVersion;
122 *pdwMinor = osvi.dwMinorVersion;
123 *pdwPlatform = osvi.dwPlatformId;
153 DWORD dwMajor, dwMinor, dwPlatform;
155 int iRet = EXIT_UNKNOWN;
158 // This call should be available on all versions of Windows
160 dwVersion = GetVersion();
161 if ((dwVersion & 0x80000000) ||
162 (FALSE == TryWinNT(&dwMajor, &dwMinor, &dwPlatform)))
164 dwMajor = (DWORD)(LOBYTE(LOWORD(dwVersion)));
165 dwMinor = (DWORD)(HIBYTE(LOWORD(dwVersion)));
166 dwPlatform = VER_PLATFORM_WIN32_WINDOWS;
172 if (dwPlatform == VER_PLATFORM_WIN32_NT)
177 iRet = EXIT_NT3OTHER;
185 iRet = EXIT_NT4OTHER;
193 iRet = EXIT_NT5OTHER;
198 else if (1 == dwMinor)
202 else if (2 == dwMinor)
204 iRet = EXIT_WIN_SERVER_2003;
216 else if (dwPlatform == VER_PLATFORM_WIN32_WINDOWS)
221 iRet = EXIT_CONSUMER_OTHER;
226 else if (10 == dwMinor)
230 else if (90 == dwMinor)
242 printf("%s %u.%u -- errorlevel is %d\n",
243 g_szWindowsNames[iRet],