Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / MFC
Article

Detecting the Operating System Version

Rate me:
Please Sign up or sign in to vote.
3.12/5 (12 votes)
22 Oct 20011 min read 198.7K   4.1K   40   24
Detecting the computer's Operating System version

Sample Image - GUI_model.jpg

Introduction

I wrote this program to detect the operating system version under which my application is running. The GetVersionEx function obtains extended information about the version of the operating system that is currently running. Here's the source code:

OSVERSIONINFO OSversion;
	
OSversion.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
::GetVersionEx(&OSversion);

switch(OSversion.dwPlatformId)
{
   case VER_PLATFORM_WIN32s: 
           m_sStr.Format("Windows %d.%d",OSversion.dwMajorVersion,OSversion.dwMinorVersion);
	   break;
   case VER_PLATFORM_WIN32_WINDOWS:
  	  if(OSversion.dwMinorVersion==0)
	      m_sStr="Windows 95";  
	  else
          if(OSversion.dwMinorVersion==10)  
	      m_sStr="Windows 98";
       	  else
          if(OSversion.dwMinorVersion==90)  
	      m_sStr="Windows Me";
          break;
   case VER_PLATFORM_WIN32_NT:
  	 if(OSversion.dwMajorVersion==5 && OSversion.dwMinorVersion==0)
             m_sStr.Format("Windows 2000 With %s", OSversion.szCSDVersion);
         else	
         if(OSversion.dwMajorVersion==5 &&   OSversion.dwMinorVersion==1)
             m_sStr.Format("Windows XP %s",OSversion.szCSDVersion);
         else	
	 if(OSversion.dwMajorVersion<=4)
	    m_sStr.Format("Windows NT %d.%d with %s",
                           OSversion.dwMajorVersion,
                           OSversion.dwMinorVersion,
                           OSversion.szCSDVersion);			
         else	
             //for unknown windows/newest windows version
	    m_sStr.Format("Windows %d.%d ",
                           OSversion.dwMajorVersion,
                           OSversion.dwMinorVersion);
         break;
}
		
UpdateData(FALSE);

The <code<osversioninfo< code=""> data structure contains operating system version information: <pre>typedef struct _OSVERSIONINFO{ DWORD dwOSVersionInfoSize; DWORD dwMajorVersion; DWORD dwMinorVersion; DWORD dwBuildNumber; DWORD dwPlatformId; TCHAR szCSDVersion[ 128 ]; } OSVERSIONINFO; </pre> </p> <p>dwOSVersionInfoSize</p><p>Specifies the size, in bytes, of this data structure.</p> <p>dwMajorVersion </p><p>Identifies the major version number of the operating system. For example, for Windows NT version 3.51, the major version number is 3; and for Windows NT version 4.0, the major version number is 4.</p> <p>dwMinorVersion </p> <p>Identifies the minor version number of the operating system. For example, for Windows NT version 3.51, the minor version number is 51; and for Windows NT version 4.0, the minor version number is 0.</p> <p>dwBuildNumber <p>Windows NT/2000: Identifies the build number of the operating system. <p>Windows 95/98: Identifies the build number of the operating system in the low-order word. The high-order word contains the major and minor version numbers. <p>dwPlatformId <p>Identifies the operating system platform. <p>szCSDVersion <p>Windows NT/2000: Contains a null-terminated string, such as "Service Pack 2", that indicates the latest Service Pack installed on the system. If no Service Pack has been installed, the string is empty. <p>Windows 95/98: Contains a null-terminated string that provides arbitrary additional information about the operating system. </p> <p>If we want to detect Windows CE then value of <code>OSversion.dwPlatformId is VER_PLATFORM_WIN32_CE

switch(OSversion.dwPlatformId)
{
   case VER_PLATFORM_WIN32_CE: 
           m_sStr.Format("Windows CE %d.%d",
                         OSversion.dwMajorVersion,
                         OSversion.dwMinorVersion);
}

References

Microsoft Platform SDK : Windows System Information

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder PE College
Indonesia Indonesia
He gradueted from Sepuluh Nopember Institute of Technology (ITS) in Department of Electrical Engineering, Indonesia. His programming interest is VC++, C#, VB, VB.NET, .NET, VBScript, Delphi, C++ Builder, Assembly,and ASP/ASP.NET. He's founder for PE College(www.pecollege.net), free video tutorial about programming, infrastructure, and computer science. He's currently based in Depok, Indonesia. His blog is http://geeks.netindonesia.net/blogs/agus and http://blog.aguskurniawan.net

Comments and Discussions

 
GeneralWine detection Pin
jk200117-Feb-09 1:17
jk200117-Feb-09 1:17 
QuestionDetecting Different OS version within a giving OS Pin
hcraes24-Oct-07 8:09
hcraes24-Oct-07 8:09 
GeneralHelp Pin
Nagarajoorao9-Feb-07 8:32
Nagarajoorao9-Feb-07 8:32 
QuestionBits? Pin
asp-12315-Jul-06 2:18
asp-12315-Jul-06 2:18 
QuestionWindows 2k3 ? Pin
linhduylv14-Nov-05 16:24
linhduylv14-Nov-05 16:24 
Generalmfc42d.dll Pin
baggy27-Jul-05 4:04
baggy27-Jul-05 4:04 
GeneralRe: mfc42d.dll Pin
jk200117-Feb-09 1:37
jk200117-Feb-09 1:37 
GeneralThanks, nice simple example Pin
Doug Brower16-May-02 4:55
Doug Brower16-May-02 4:55 
GeneralProblem on Windows Xp : cannot catch events of smart-card .... Pin
vanta3-Feb-02 23:51
vanta3-Feb-02 23:51 
GeneralRe: Problem on Windows Xp : cannot catch events of smart-card .... Pin
lordneo7-Dec-04 23:31
lordneo7-Dec-04 23:31 
Generaldetermining OS remotely Pin
pesglobe11-Dec-01 6:24
pesglobe11-Dec-01 6:24 
Generalother system-infos (e.g. terminal server or server-edition at all) Pin
mloibl23-Oct-01 9:06
mloibl23-Oct-01 9:06 
GeneralRe: other system-infos (e.g. terminal server or server-edition at all) Pin
23-Oct-01 22:57
suss23-Oct-01 22:57 
GeneralNice job! Pin
Phil Sidari23-Oct-01 8:20
Phil Sidari23-Oct-01 8:20 
GeneralRe: Nice job! Pin
23-Oct-01 22:55
suss23-Oct-01 22:55 
Generalyou can also type "ver" from cmd prompt Pin
22-Oct-01 6:44
suss22-Oct-01 6:44 
GeneralRe: you can also type "ver" from cmd prompt Pin
22-Oct-01 18:07
suss22-Oct-01 18:07 
GeneralRe: you can also type &quot;ver&quot; from cmd prompt Pin
argila0030-Jul-04 0:10
argila0030-Jul-04 0:10 
GeneralVery poor Pin
Jerry III21-Oct-01 22:52
Jerry III21-Oct-01 22:52 
GeneralRe: Very poor Pin
23-Oct-01 7:08
suss23-Oct-01 7:08 
GeneralRe: Very poor Pin
23-Oct-01 22:43
suss23-Oct-01 22:43 
GeneralRe: Very poor Pin
28-May-02 3:59
suss28-May-02 3:59 
QuestionWindows XP? Pin
Anatoly Ivasyuk17-Oct-01 10:57
Anatoly Ivasyuk17-Oct-01 10:57 
AnswerRe: Windows XP? Pin
17-Oct-01 14:56
suss17-Oct-01 14:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.