Microsoft KB Archive/307394

From BetaArchive Wiki

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

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:

Windows 95 Windows 98 Windows ME Windows NT 4 Windows 2000 Windows XP
PlatformID 1 1 1 2 2 2
Major version 4 4 4 4 5 5
Minor version 0 10 90 0 0 1


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 "Win32Windows" means an operating system of the Windows 9x family of products. "WinNT" 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 ("Windows 95");
            break;
        case 10:
            if(osInfo->Version->Revision.ToString()=="2222A")
                Console::WriteLine("Windows 98 Second Edition");
            else
                 Console::WriteLine("Windows 98");
                 break;
        case  90:
                 Console::WriteLine("Windows ME");
                 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("Windows NT 3.51");
            break;
        case 4:
            Console::WriteLine("Windows NT 4.0");
            break;
        case 5:
            if (osInfo->Version->Minor==0) 
                Console::WriteLine("Windows 2000");
            else
                Console::WriteLine("Windows XP");
                break;
     }break;
                

back to the top

Building the Example

The following steps show how you can build a test scenario to demonstrate this functionality.

  1. In Visual Studio .NET, create a new Managed C++ application called determine0S. This creates a simple "hello world" application. Open DetermineOS.cpp in the code editor by double-clicking it in the Solution Explorer window.
  2. Delete all of the code in DetermineOS.cpp.
  3. Paste in the following code:

    #include "stdafx.h"
    
    #using <mscorlib.dll>
    
    using namespace System;
    
    // This is the entry point for this application
    #ifdef _UNICODE
    int wmain(void)
    #else
    int main(void)
    #endif
    {
       //get OperatingSystem info from the system namespace
       System::OperatingSystem *osInfo = System::Environment::OSVersion;
    
       //is the platform win9x or NT
       switch(osInfo->Platform)
       {
          //platform is win9x
          case System::PlatformID::Win32Windows:
        
             switch (osInfo->Version->Minor)
             {
                  case 0:
                     Console::WriteLine ("Windows 95");
                  break;
                  case 10:
                  if(osInfo->Version->Revision.ToString() == "2222A")
                     Console::WriteLine("Windows 98 Second Edition");
                  else
                     Console::WriteLine("Windows 98");
                  break;
                  case  90:
                     Console::WriteLine("Windows ME");
                  break;
             }
             break;
                
             //platform is NT
             case System::PlatformID::Win32NT:
    
                switch(osInfo->Version->Major)
                {
                   case 3:
                      Console::WriteLine("Windows NT 3.51");
                   break;
                   case 4:
                      Console::WriteLine("Windows NT 4.0");
                   break;
                   case 5:
                   if (osInfo->Version->Minor==0) 
                      Console::WriteLine("Windows 2000");
                   else
                      Console::WriteLine("Windows XP");
                   break;
                }break;
       }
    
       Console::ReadLine();
       return 0;
    }
                        
  4. Press CTRL+F5 to run the application. Note that the Windows version is displayed in the console window.

back to the top

REFERENCES

For more general information about Visual C++ .NET, visit the following Microsoft Usenet newsgroup and Microsoft Web site:

back to the top

Keywords: kbhowtomaster kbnewsgrouplink KB307394