C++ Snippet: Detect Windows Version
89
Post Rating
Thanks!
An error occurred!

Jan 08, 2012 1 Comment by

Summary

This nicely put together C++ header file that will allow you to detect which version of windows your program is currently running on. Currently it supports these versions of Windows:

 

  • Windows 7
  • Windows Vista
  • Windows XP
  • Windows 2000
  • Windows Server

I hope to add more support and more functions at a later date :)

How It Works

Most of the details needed for us to detect the Windows version are in the Windows structure “OSVERSIONINFOEX,” which can be populated with data by calling the “GetVersionEx()” function found in “Windows.h.” However its not as easy as it seams, there is much more work put into detecting the version information, such as:

  • Service Pack Version
  • OS Edition( Ultimate, Pro, etc. )
  • OS Architecture ( x64, x86 )
  • Build Number

How To Use It

Using this code is extremely easy, just include the “os_detection.h” header file into your C++ project and don’t forget to put “#include “os_detection.h”” in your main.cpp file :P

Code: os_detection.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#ifndef _OS_DETECTION_H_
#define _OS_DETECTION_H_

#include <windows.h>
#include <string>

typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);

#define PRODUCT_PROFESSIONAL 0x00000030
#define VER_SUITE_WH_SERVER  0x00008000

struct OSINFO
{
    string
        OS_Name,
        OS_Edition,
        OS_Service_Pack,
        OS_Architecture,
        OS_Info;

    int
        OS_Build_Number;
};

OSINFO getWindowsVersionInfo()
{
    OSINFO osInfo;

    OSVERSIONINFOEX osvi;
    SYSTEM_INFO si;
    BOOL bOsVersionInfoEx;
    DWORD dwType;

    ZeroMemory(&si, sizeof(SYSTEM_INFO));
    ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

    bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*) &osvi);

    if (!bOsVersionInfoEx)
    {
    }

    PGNSI pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");

    if (pGNSI != NULL)
    {
        pGNSI(&si);
    }

    else
    {
        GetSystemInfo(&si);
    }

    if (VER_PLATFORM_WIN32_NT != osvi.dwPlatformId ¦¦ osvi.dwMajorVersion <= 4 )
    {
    }

    osInfo.OS_Info += "Microsoft ";

    if( osvi.dwMinorVersion == 0 )
    {
        if( osvi.wProductType == VER_NT_WORKSTATION )
        {
            osInfo.OS_Info += "Windows Vista ";
            osInfo.OS_Name = "Windows Vista";
        }

        else
        {
            osInfo.OS_Info += "Windows Server 2008 ";
            osInfo.OS_Name = "Windows Server 2008";
        }
    }

    if ( osvi.dwMinorVersion == 1 )
    {
        if( osvi.wProductType == VER_NT_WORKSTATION )
        {
            osInfo.OS_Info += "Windows 7 ";
            osInfo.OS_Name = "Windows 7";
        }

        else
        {
            osInfo.OS_Info += "Windows Server 2008 R2 ";
            osInfo.OS_Name = "Windows Server 2008 R2";
        }
    }

    PGPI pGPI = (PGPI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");
    pGPI( osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);

    switch( dwType )
    {

    case PRODUCT_ULTIMATE:
        {
            osInfo.OS_Info += "Ultimate Edition ";
            osInfo.OS_Edition = "Ultimate Edition";
            break;
        }

    case PRODUCT_PROFESSIONAL:
        {
            osInfo.OS_Info += "Professional ";
            osInfo.OS_Edition = "Professional";
            break;
        }

    case PRODUCT_HOME_PREMIUM:
        {
            osInfo.OS_Info += "Home Premium Edition ";
            osInfo.OS_Edition = "Home Premium Edition";
            break;
        }

    case PRODUCT_HOME_BASIC:
        {
            osInfo.OS_Info += "Home Basic Edition ";
            osInfo.OS_Edition = "Home Basic Edition";
            break;
        }

    case PRODUCT_ENTERPRISE:
        {
            osInfo.OS_Info += "Enterprise Edition ";
            osInfo.OS_Edition = "Enterprise Edition";
            break;
        }

    case PRODUCT_BUSINESS:
        {
            osInfo.OS_Info += "Business Edition ";
            osInfo.OS_Edition = "Business Edition";
            break;
        }

    case PRODUCT_STARTER:
        {
            osInfo.OS_Info += "Starter Edition ";
            osInfo.OS_Edition = "Starter Edition";
            break;
        }

    case PRODUCT_CLUSTER_SERVER:
        {
            osInfo.OS_Info += "Cluster Server Edition ";
            osInfo.OS_Edition = "Cluster Server Edition";
            break;
        }

    case PRODUCT_DATACENTER_SERVER:
        {
            osInfo.OS_Info += "Datacenter Edition ";
            osInfo.OS_Edition = "Datacenter Edition";
            break;
        }

    case PRODUCT_DATACENTER_SERVER_CORE:
        {
            osInfo.OS_Info += "Datacenter Edition (core installation) ";
            osInfo.OS_Edition = "Datacenter Edition (core installation)";
            break;
        }

    case PRODUCT_ENTERPRISE_SERVER:
        {
            osInfo.OS_Info += "Enterprise Edition ";
            osInfo.OS_Edition = "Enterprise Edition";
            break;
        }

    case PRODUCT_ENTERPRISE_SERVER_CORE:
        {
            osInfo.OS_Info += "Enterprise Edition (core installation) ";
            osInfo.OS_Edition = "Enterprise Edition (core installation)";
            break;
        }

    case PRODUCT_ENTERPRISE_SERVER_IA64:
        {
            osInfo.OS_Info += "Enterprise Edition for Itanium-based Systems ";
            osInfo.OS_Edition = "Enterprise Edition for Itanium-based Systems";
            break;
        }

    case PRODUCT_SMALLBUSINESS_SERVER:
        {
            osInfo.OS_Info += "Small Business Server ";
            osInfo.OS_Edition = "Small Business Server";
            break;
        }

    case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
        {
            osInfo.OS_Info += "Small Business Server Premium Edition ";
            osInfo.OS_Edition = "Small Business Server Premium Edition";
            break;
        }

    case PRODUCT_STANDARD_SERVER:
        {
            osInfo.OS_Info += "Standard Edition ";
            osInfo.OS_Edition = "Standard Edition";
            break;
        }

    case PRODUCT_STANDARD_SERVER_CORE:
        {
            osInfo.OS_Info += "Standard Edition (core installation) ";
            osInfo.OS_Edition = "Standard Edition (core installation)";
            break;
        }

    case PRODUCT_WEB_SERVER:
        {
            osInfo.OS_Info += "Web Server Edition ";
            osInfo.OS_Edition = "Web Server Edition";
            break;
        }
    }

    if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
    {
        if (GetSystemMetrics(SM_SERVERR2))
        {
            osInfo.OS_Info += "Windows Server 2003 R2, ";
            osInfo.OS_Edition = "Windows Server 2003 R2";
        }

        else if (osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER)
        {
            osInfo.OS_Info += "Windows Storage Server 2003";
            osInfo.OS_Edition = "Windows Storage Server 2003";
        }

        else if (osvi.wSuiteMask & VER_SUITE_WH_SERVER)
        {
            osInfo.OS_Info += "Windows Home Server";
            osInfo.OS_Edition = "Windows Home Server";
        }

        else if (osvi.wProductType == VER_NT_WORKSTATION && si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
        {
            osInfo.OS_Info += "Windows XP Professional x64 Edition";
            osInfo.OS_Edition = "Professional x64 Edition";
        }

        else
        {
            osInfo.OS_Info += "Windows Server 2003, ";
            osInfo.OS_Edition = "Windows Server 2003";
        }

        if (osvi.wProductType != VER_NT_WORKSTATION)
        {
            if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64)
            {
                if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
                {
                    osInfo.OS_Info += "Datacenter Edition for Itanium-based Systems";
                    osInfo.OS_Edition = "Datacenter Edition for Itanium-based Systems";
                }

                else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
                {
                    osInfo.OS_Info += "Enterprise Edition for Itanium-based Systems";
                    osInfo.OS_Edition = "Enterprise Edition for Itanium-based Systems";
                }
            }

            else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
            {
                if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
                {
                    osInfo.OS_Info += "Datacenter x64 Edition";
                    osInfo.OS_Edition = "Datacenter x64 Edition";
                }

                else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
                {
                    osInfo.OS_Info += "Enterprise x64 Edition";
                    osInfo.OS_Edition = "Enterprise x64 Edition";
                }

                else
                {
                    osInfo.OS_Info += "Standard x64 Edition";
                    osInfo.OS_Edition = "Standard x64 Edition";
                }
            }

            else
            {
                if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
                {
                    osInfo.OS_Info += "Compute Cluster Edition";
                    osInfo.OS_Edition = "Compute Cluster Edition";
                }

                else if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
                {
                    osInfo.OS_Info += "Datacenter Edition";
                    osInfo.OS_Edition = "Datacenter Edition";
                }

                else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
                {
                    osInfo.OS_Info += "Enterprise Edition";
                    osInfo.OS_Edition = "Enterprise Edition";
                }

                else if (osvi.wSuiteMask & VER_SUITE_BLADE)
                {
                    osInfo.OS_Info += "Web Edition";
                    osInfo.OS_Edition = "Web Edition";
                }

                else
                {
                    osInfo.OS_Info += "Standard Edition";
                    osInfo.OS_Edition = "Standard Edition";
                }
            }
        }
    }

    if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
    {
        osInfo.OS_Info += "Windows XP ";
        osInfo.OS_Name = "Windows XP";

        if(osvi.wSuiteMask & VER_SUITE_PERSONAL)
        {
            osInfo.OS_Info += "Home Edition";
            osInfo.OS_Edition = "Home Edition";
        }

        else
        {
            osInfo.OS_Info += "Professional";
            osInfo.OS_Edition = "Professional";
        }
    }

    if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
    {
        osInfo.OS_Info += "Windows 2000 ";
        osInfo.OS_Edition = "Windows 2000";

        if (osvi.wProductType == VER_NT_WORKSTATION)
        {
            osInfo.OS_Info += "Professional";
            osInfo.OS_Edition = "Professional";
        }

        else
        {
            if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
            {
                osInfo.OS_Info += "Datacenter Server";
                osInfo.OS_Edition = "Datacenter Server";
            }

            else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
            {
                osInfo.OS_Info += "Advanced Server";
                osInfo.OS_Edition = "Advanced Server";
            }

            else
            {
                osInfo.OS_Info += "Server";
                osInfo.OS_Edition = "Server";
            }
        }
    }

    osInfo.OS_Info += osvi.szCSDVersion;
    osInfo.OS_Service_Pack += " ";
    osInfo.OS_Service_Pack += osvi.szCSDVersion;

    osInfo.OS_Info += " (build ";
    char strBuild[64];
    _itoa(osvi.dwBuildNumber, strBuild, 10);
    osInfo.OS_Info += strBuild;
    osInfo.OS_Info += ")";
    osInfo.OS_Build_Number = osvi.dwBuildNumber;

    if (osvi.dwMajorVersion >= 6)
    {
        if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
        {
            osInfo.OS_Info += ", 64-bit";
            osInfo.OS_Architecture = "64-bit";
        }

        else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
        {
            osInfo.OS_Info += ", 32-bit";
            osInfo.OS_Architecture = "32-bit";
        }
    }

    return osInfo;
}

#endif

Now, just call the “getWindowsVersionInfo()” function like below:

OSINFO OperatingSystemInfo = getWindowsVersionInfo();

From then on, the version info can be read like such:

string strOSname = OperatingSystemInfo.OS_Name;

For reference, I have put the structure of OSINFO below:

1
2
3
4
5
6
7
8
9
10
11
12
struct OSINFO
{
    string
        OS_Name,
        OS_Edition,
        OS_Service_Pack,
        OS_Architecture,
        OS_Info;

    int
        OS_Build_Number;
};

Uses

So what might one use this source code for? Well, perhaps a feature of your application only works with Windows 7, and you don’t want to have multiple versions of the same application for each Windows version. In that case, you could use the header file to detect the Version of windows and run the specific function that works for that version of windows. You can also use it for:

  • Optimize For Windows Version
  • Better Application Compatibility
  • OS Specific Applications

C++, Programming, Snippet

About the author

The author didnt add any Information to his profile yet

One Response to “C++ Snippet: Detect Windows Version”

  1. dr_snipe says:

    Yo brock, can we get the forum back or did GoDaddy get rid of all of it?

Leave a Reply