CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Nov 2010
    Posts
    15

    Question How to determine OS using C language ?

    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->
    Code:
    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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: How to determine OS using C language ?

    Struct OSVERSIONINFOEX is defined (beginning with the Windows 2000) as
    Code:
    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...
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: How to determine OS using C language ?

    and here's what MSDN says about it http://msdn.microsoft.com/en-us/libr...33(VS.85).aspx
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Nov 2010
    Posts
    15

    Unhappy Re: How to determine OS using C language ?

    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:
    Code:
    WORD wSuiteMask;
    BYTE wProductType;
    On Microsoft Windows XP Structure OSVERSIONINFOEX->
    Code:
    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.

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: How to determine OS using C language ?

    What compiler do you use?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  6. #6
    Join Date
    Nov 2010
    Posts
    15

    Unhappy Re: How to determine OS using C language ?

    Hi,

    I am using Visual Studio.

    Thanks.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: How to determine OS using C language ?

    Quote Originally Posted by anish2006 View Post
    Hi,
    I am using Visual Studio.
    Which one: 6.0, 2003, 2005, 2008, 2010, or maybe 4.2?
    Victor Nijegorodov

  8. #8
    Join Date
    Nov 2010
    Posts
    15

    Re: How to determine OS using C language ?

    It is Microsoft Visual C++ 6.0


    Thanks.

  9. #9
    Join Date
    Nov 2010
    Posts
    15

    Unhappy Re: How to determine OS using C language ?

    Please find below code which I am trying to compile on Microsoft Visual C++ 6.0, however compilation is unsuccessful due to Line 13,
    Code:
    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->
    Code:
     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.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: How to determine OS using C language ?

    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
    Victor Nijegorodov

  11. #11
    Join Date
    Nov 2010
    Posts
    15

    Smile Re: How to determine OS using C language ?

    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.

  12. #12
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: How to determine OS using C language ?

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured