Microsoft KB Archive/307394

= How to determine the version of Windows by using Visual C++ =

Article ID: 307394

Article Last Modified on 1/18/2006

-

APPLIES TO


 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ 2005 Express Edition

-



This article was previously published under Q307394



For a Microsoft Visual Basic .NET version of this article, see 304289.

For a Microsoft Visual C# .NET version of this article, see 304283.

IN THIS TASK
SUMMARY
 * Requirements
 * Windows Version Data
 * Obtain the Operating System Information
 * Determine the Platform ID
 * Determine the Specific Windows 9x Version
 * Determine the Specific Windows NT Version
 * Building the Example

REFERENCES



SUMMARY
This article demonstrates how to determine which operating system is in use on the system where your application is running. It differentiates between Windows 95, Windows 98, Windows Millennium Edition, Windows NT 3.51, Windows NT 4.0, Windows 2000, and Windows XP.

back to the top

Requirements
This article assumes that you have an intermediate-level understanding of C++ programming.

back to the top

Windows Version Data
To determine the operating system that is running on a given system, the following data is needed:

NOTE: although the code in this article does check for all 32-bit Windows versions, neither Visual Studio .NET nor the .NET Framework is supported on Windows 95 or Windows NT 3.51.

back to the top

Obtain the Operating System Information
Create a pointer to an OperatingSystem class and assign the current operating system information to it: System::OperatingSystem *osInfo = System::Environment::OSVersion; back to the top

Determine the Platform ID
The first step in the logical evaluation of the operating system information is to determine which platform is in use, as shown below. This is done by using the PlatformID. PlatformID is a property of the OperatingSystem class. An enumerated value of &quot;Win32Windows&quot; means an operating system of the Windows 9x family of products. &quot;WinNT&quot; indicates an operating system of the Windows NT family. switch(osInfo->Platform) {       case System::PlatformID::Win32Windows: {            //code to determine specific Windows 9x version }

case System::PlatformID::Win32NT: {             //code to determine specific Windows NT version }    } back to the top

Determine the Specific Windows 9x Version
If the platform has been determined to be Windows 9x, the major or minor version can be analyzed to determine the specific version, as the following code demonstrates. //platform is win9x case System::PlatformID::Win32Windows: switch (osInfo->Version->Minor) {       case 0: Console::WriteLine (&quot;Windows 95&quot;); break; case 10: if(osInfo->Version->Revision.ToString==&quot;2222A&quot;) Console::WriteLine(&quot;Windows 98 Second Edition&quot;); else Console::WriteLine(&quot;Windows 98&quot;); break; case 90: Console::WriteLine(&quot;Windows ME&quot;); break; }break;

back to the top

Determine the Specific Windows NT Version
If the platform has been determined to be Windows NT, the major or minor version can be analyzed to determine the specific version: //platform is NT case System::PlatformID::Win32NT:

switch(osInfo->Version->Major) {       case 3: Console::WriteLine(&quot;Windows NT 3.51&quot;); break; case 4: Console::WriteLine(&quot;Windows NT 4.0&quot;); break; case 5: if (osInfo->Version->Minor==0) Console::WriteLine(&quot;Windows 2000&quot;); else Console::WriteLine(&quot;Windows XP&quot;); break; }break; back to the top

Building the Example
The following steps show how you can build a test scenario to demonstrate this functionality.  In Visual Studio .NET, create a new Managed C++ application called determine0S. This creates a simple &quot;hello world&quot; application. Open DetermineOS.cpp in the code editor by double-clicking it in the Solution Explorer window. Delete all of the code in DetermineOS.cpp.  Paste in the following code:
 * 1) include &quot;stdafx.h&quot;


 * 1) using 

using namespace System;

// This is the entry point for this application int wmain(void) int main(void) {  //get OperatingSystem info from the system namespace System::OperatingSystem *osInfo = System::Environment::OSVersion;
 * 1) ifdef _UNICODE
 * 1) else
 * 1) endif

//is the platform win9x or NT  switch(osInfo->Platform) {     //platform is win9x case System::PlatformID::Win32Windows: switch (osInfo->Version->Minor) {             case 0: Console::WriteLine (&quot;Windows 95&quot;); break; case 10: if(osInfo->Version->Revision.ToString == &quot;2222A&quot;) Console::WriteLine(&quot;Windows 98 Second Edition&quot;); else Console::WriteLine(&quot;Windows 98&quot;); break; case 90: Console::WriteLine(&quot;Windows ME&quot;); break; }        break; //platform is NT        case System::PlatformID::Win32NT:

switch(osInfo->Version->Major) {              case 3: Console::WriteLine(&quot;Windows NT 3.51&quot;); break; case 4: Console::WriteLine(&quot;Windows NT 4.0&quot;); break; case 5: if (osInfo->Version->Minor==0) Console::WriteLine(&quot;Windows 2000&quot;); else Console::WriteLine(&quot;Windows XP&quot;); break; }break; }

Console::ReadLine; return 0; }                    Press CTRL+F5 to run the application. Note that the Windows version is displayed in the console window.

back to the top

