Click here to Skip to main content
15,898,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
// Get the OS version on your PC

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

using namespace std;

int main(void)
{
    OSVERSIONINFO osver;

    
    osver.dwOSVersionInfoSize = sizeof(osver);
    if (GetVersionEx(&osver))
    {
        
        if (osver.dwPlatformId == VER_PLATFORM_WIN32s)
            cout << "Win32 ";
        else if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
        {
            if (osver.dwMinorVersion == 0 && (strchr(osver.szCSDVersion,'B') == NULL) && (strchr(osver.szCSDVersion,'C') == NULL))
                cout << "Windows 95 ";
            else if (osver.dwMinorVersion == 0 && (strchr(osver.szCSDVersion,'B') != NULL))
                cout << "Windows 95 OSR 2 ";
            else if (osver.dwMinorVersion == 0 && (strchr(osver.szCSDVersion,'C') != NULL))
                cout << "Windows 95 OSR 2.5 ";
            else if (osver.dwMinorVersion == 10 && (strchr(osver.szCSDVersion,'A') == NULL))
                cout << "Windows 98 ";
            else if (osver.dwMinorVersion == 10 && (strchr(osver.szCSDVersion,'A') != NULL))
                cout << "Windows 98 Second Edition ";
            else if (osver.dwMinorVersion == 90)
                cout << "Windows Millenium Edition ";
            else
                cout<<"Unknown Windows ";
        }
        else if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT){
            cout << "Windows NT ";
            cout << osver.dwMajorVersion << "." << osver.dwMinorVersion << "." << (osver.dwBuildNumber & 0xffff) << osver.szCSDVersion << endl;
        }
        else 
        {
            cout << "Cannot gain version information." << endl;
        }   
            
    }
system("pause");
return 0;

}


Here are the errors:
error: 'osver' was not declared in this scope
error: expected primary-expression before ';' token

Any Help will be much appriciated

Thank You,
Posted
Comments
Richard MacCutchan 20-May-12 3:44am    
Which line does the error occur on?
Richard MacCutchan 20-May-12 7:26am    
I just pasted your code into VC++ 2010 Express and it built and ran just fine. Given that you are testing for Windows 95 I suspect you may be using a very old compiler.
Anikan1297 20-May-12 13:39pm    
And i'm actually using sample code, im just starting out and trying to learn the language.
Anikan1297 20-May-12 13:31pm    
Well, i'm using CodeBlocks and its not working. Why is it different with each IDE?
Richard MacCutchan 21-May-12 2:50am    
Sorry, I have never used CodeBlocks; why not use VC++ 2010 Express as I suggested above. It's developed and supported by Microsoft and used by many people in this forum.

This code builds with Unicode set -
#include <windows.h>
#include <iostream>
#include <dos.h>
#include <string.h> 
#include <iostream>
using namespace std;
int main(void)

{
	OSVERSIONINFO osver;
	osver.dwOSVersionInfoSize = sizeof(osver);
	if (GetVersionEx(&osver))

	{
		if (osver.dwPlatformId == VER_PLATFORM_WIN32s)
			cout <<"Win32 ";
		else if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)

		{
			if (osver.dwMinorVersion == 0 && (wcschr(osver.szCSDVersion,'B') == NULL)
				&& (wcschr(osver.szCSDVersion,'C') == NULL))
				cout <<"Windows 95 ";
			else if (osver.dwMinorVersion == 0 && (wcschr(osver.szCSDVersion,'B') != NULL))
				cout <<"Windows 95 OSR 2 ";
			else if (osver.dwMinorVersion == 0 && (wcschr(osver.szCSDVersion,'C') != NULL))
				cout <<"Windows 95 OSR 2.5 ";
			else if (osver.dwMinorVersion == 10 && (wcschr(osver.szCSDVersion,'A') == NULL))
				cout <<"Windows 98 ";
			else if (osver.dwMinorVersion == 10 && (wcschr(osver.szCSDVersion,'A') != NULL))
				cout <<"Windows 98 Second Edition ";
			else if (osver.dwMinorVersion == 90)
				cout <<"Windows Millenium Edition ";
			else
				cout<<"Unknown Windows ";
		}
		else if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
			cout <<"Windows NT ";
		cout <<osver.dwMajorVersion<<"."<<osver.dwMinorVersion<<"."
			<<(osver.dwBuildNumber & 0xffff)<<osver.szCSDVersion<<endl;
	}
	else


	{
		cout <<"Cannot gain version information."<<endl;
	}
	system("pause");
	return 0;
}


By the way, if it says something like: Windows NT 6.1.7600002BFB08, this means Windows 7 (see List of Microsoft Windows versions[^])
 
Share this answer
 
v2
Comments
Anikan1297 20-May-12 9:37am    
Thanks!
This is not an answer at all, but a little tip.
The better way to choose OS version (in your code), instead of if, is to use Switch command[^]
C#
switch (expression)
{
   case constant-expression:
      statement
      jump-statement
   [default:
      statement
      jump-statement]
}


More at: MSDN[^]
 
Share this answer
 
Comments
Anikan1297 20-May-12 13:37pm    
Thanks for the Advice!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900