Click to See Complete Forum and Search --> : How to determine OS using C language ?


anish2006
November 16th, 2010, 10:45 PM
Hello Everyone,

My requirement is such that, I need to identify OS (in my case it is Windows 7) in C language and based on that condition have to proceed further.
As we have 2 OS(Windows 7 and Windows Server 2008 R2) having the similar dwMajorVersion(6) and dwMinorVersion(1) but different product type.

So we can differentiate "Windows 7" and "Windows Server 2008 R2" by product type.

But my problem begins here:
Currently I am working on "Microsoft Windows XP".

On Windows XP Structure OSVERSIONINFOEX->

typedef struct _OSVERSIONINFOEXA {
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
CHAR szCSDVersion[ 128 ]; // Maintenance string for PSS usage
WORD wServicePackMajor;
WORD wServicePackMinor;
WORD wReserved[2];
}

It does not contain variable to get information about product type that is "wProductType".

Request you to please help me, how to determine the OS type between "Windows 7" and "Windows Server 2008 R2" considering I am coding on Microsft Windows XP.

Thanks.

VictorN
November 17th, 2010, 06:43 AM
Struct OSVERSIONINFOEX is defined (beginning with the Windows 2000) as typedef struct _OSVERSIONINFOEX {
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
TCHAR szCSDVersion[ 128 ];
WORD wServicePackMajor;
WORD wServicePackMinor;
WORD wSuiteMask;
BYTE wProductType;
BYTE wReserved;
} OSVERSIONINFOEX, *POSVERSIONINFOEX, *LPOSVERSIONINFOEX;
At least it is what my very old MSDN from October 2000 says...

S_M_A
November 17th, 2010, 02:50 PM
and here's what MSDN says about it http://msdn.microsoft.com/en-us/library/ms724833(VS.85).aspx

anish2006
November 18th, 2010, 07:56 PM
Hello Everyone,

Thank you all for your quick responses.
But I don't know the reason behind the structure OSVERSIONINFOEX that is showing on Microsoft Windows XP professional version 2002 as below.

It does not have following 2 variables:

WORD wSuiteMask;
BYTE wProductType;


On Microsoft Windows XP Structure OSVERSIONINFOEX->

typedef struct _OSVERSIONINFOEXA {
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
CHAR szCSDVersion[ 128 ]; // Maintenance string for PSS usage
WORD wServicePackMajor;
WORD wServicePackMinor;
WORD wReserved[2];
}OSVERSIONINFOEXA, *POSVERSIONINFOEX,A *LPOSVERSIONINFOEXA;


Thanks.

S_M_A
November 20th, 2010, 04:49 AM
What compiler do you use?

anish2006
November 20th, 2010, 09:23 AM
Hi,

I am using Visual Studio.

Thanks.

VictorN
November 20th, 2010, 04:19 PM
Hi,
I am using Visual Studio.
Which one: 6.0, 2003, 2005, 2008, 2010, or maybe 4.2? :confused:

anish2006
November 21st, 2010, 07:33 PM
It is Microsoft Visual C++ 6.0


Thanks.

anish2006
November 22nd, 2010, 12:16 AM
Please find below code which I am trying to compile on Microsoft Visual C++ 6.0, however compilation is unsuccessful due to Line 13,

include <stdio.h>
#include <windows.h>

#pragma comment(lib, "User32.lib")
#define BUFSIZE 256

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


Line 1: int main()
Line 2: {
Line 3: OSVERSIONINFOEX osvi;
Line 4:
Line 5: ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
Line 6: osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
Line 7: GetVersionEx((OSVERSIONINFO *)&osvi);
Line 8:
Line 9: if ( osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
Line 10: {
Line 11: if(osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 1)
Line 12: {
Line 13: if(osvi.wProductType == VER_NT_WORKSTATION)
Line 14: {
Line 15: // do something
Line 16: }
Line 17: }
Line 18: }
Line 19: }


Giving following compilation error->

C:\Test\CheckOS.c(51): error C2039: 'wProductType' : '_OSVERSIONINFOEXA' is not a memeber.
C:\program files\microsoft visual studio\vc98\include\winbase.h(7998) : OSVERSIONINFOEXA' Please check the declaration.



Thanks.

VictorN
November 22nd, 2010, 01:08 AM
I guess you need to download and install the Platform SDK for VC++6.0 (the last version is from Feb. 2000) to correctly compile it

anish2006
November 22nd, 2010, 02:48 AM
Thanks VictorN,

I will download and install the Platform SDK for VC++6.0 and try again and will let you know, once it works.

Thanks a lot.

S_M_A
November 22nd, 2010, 03:08 PM
You could also consider getting yourself a modern compiler. Even with that SDK the headers will not contain the new stuff added during the last 10 years.