Thread: Detect OS version

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    Detect OS version

    Hi, I'm trying to compile this code, and I'm pretty sure I just don't have my project configured properly, but I don't know what to do, and Google hasn't given me any headway this time... I actually copied and pasted the code from this site:
    Getting the System Version (Windows)
    and when I go to compile, I get this error:
    1>c:\users\eric\documents\visual studio 10\projects\test002\test002\main.cpp(73): error C2065: 'PRODUCT_PROFESSIONAL' : undeclared identifier
    1>c:\users\eric\documents\visual studio 10\projects\test002\test002\main.cpp(73): error C2051: case expression not constant

    This seems strange to me, that it doesn't recognize this one particular value that is supposedly defined in the Windows library itself... What am I doing wrong?
    I'm sure you need to know: Visual Studio 2010 Professional (Beta1), just opened a new empty Win32 project. Oh, and I'm running Windows 7 Professional 64 bit...
    Thanks for any tips!
    Last edited by linucksrox; 05-15-2010 at 04:21 PM.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  2. #2
    Registered User lpaulgib's Avatar
    Join Date
    May 2010
    Posts
    65
    Hey so I know I'm new and a beginner when it comes to programming..... With that said....If you look under the code you posted, it says its buggy code and wont work with anything after vista.

    1 point to me for being able to read properly!



    Edit: Further reading will tell you that it was posted 2 days ago on the comments area...

  3. #3
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    Thanks, I actually read that too, but that doesn't help. I still need the code to work... and as a matter of fact, I was unable to compile the same exact code under Windows XP 32 bit with the same exact compilation errors. There's a problem on my end, and I need someone to help me figure out what that is. Then I'll figure out the bugs in the code. Any help please? I'm not trying to be lazy, I just don't know where to start... can anyone help me figure out where to start so I can learn how to do this? I can't find a tutorial that wasn't written a million years ago and thus not compiling on my current systems.

    edit: And by the way, not to be mean, but minus 1 point for you being able to read properly, because it clearly states that this code won't work properly on systems OLDER than Windows Vista... Also, it's obvious that my problem isn't with calling the function GetProductInfo which wasn't implemented until Vista, because that would assume I'm compiling the code, but getting a runtime error when actually calling that function. I'm actually not able to compile because for some reason the constant PRODUCT_PROFESSIONAL is undefined. Anyone else have any suggestions other than "the code is buggy?" To be clear, I know this code isn't working (obviously). How do I make it work (or what do I need to learn to figure it out for myself)? Thanks.
    Last edited by linucksrox; 05-16-2010 at 11:40 AM.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  4. #4
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    Well, it turns out PRODUCT_PROFESSIONAL doesn't exist, so if you delete that entry entirely, the code just compiles, and everything is fine. Now on to my next big project...
    Here's my working code by the way, if anyone wants it:
    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <strsafe.h>
    
    #pragma comment(lib, "User32.lib")
    
    #define BUFSIZE 256
    
    typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
    typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
    
    BOOL GetOSDisplayString( LPTSTR pszOS)
    {
       OSVERSIONINFOEX osvi;
       SYSTEM_INFO si;
       PGNSI pGNSI;
       PGPI pGPI;
       BOOL bOsVersionInfoEx;
       DWORD dwType;
    
       ZeroMemory(&si, sizeof(SYSTEM_INFO));
       ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
    
       osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    
       if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
          return 1;
    
       // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
    
       pGNSI = (PGNSI) GetProcAddress(
          GetModuleHandle(TEXT("kernel32.dll")), 
          "GetNativeSystemInfo");
       if(NULL != pGNSI)
          pGNSI(&si);
       else GetSystemInfo(&si);
    
       if ( VER_PLATFORM_WIN32_NT==osvi.dwPlatformId && 
            osvi.dwMajorVersion > 4 )
       {
          StringCchCopy(pszOS, BUFSIZE, TEXT("Microsoft "));
    
          // Test for the specific product.
    
          if ( osvi.dwMajorVersion == 6 )
          {
             if( osvi.dwMinorVersion == 0 )
             {
                if( osvi.wProductType == VER_NT_WORKSTATION )
                    StringCchCat(pszOS, BUFSIZE, TEXT("Windows Vista "));
                else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 " ));
             }
    
             if ( osvi.dwMinorVersion == 1 )
             {
                if( osvi.wProductType == VER_NT_WORKSTATION )
                    StringCchCat(pszOS, BUFSIZE, TEXT("Windows 7 "));
                else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 R2 " ));
             }
             
             pGPI = (PGPI) GetProcAddress(
                GetModuleHandle(TEXT("kernel32.dll")), 
                "GetProductInfo");
    
             pGPI( osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);
    
             switch( dwType )
             {
                case PRODUCT_ULTIMATE:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Ultimate Edition" ));
                   break;
                case PRODUCT_HOME_PREMIUM:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Home Premium Edition" ));
                   break;
                case PRODUCT_HOME_BASIC:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Home Basic Edition" ));
                   break;
                case PRODUCT_ENTERPRISE:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition" ));
                   break;
                case PRODUCT_BUSINESS:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Business Edition" ));
                   break;
                case PRODUCT_STARTER:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Starter Edition" ));
                   break;
                case PRODUCT_CLUSTER_SERVER:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Cluster Server Edition" ));
                   break;
                case PRODUCT_DATACENTER_SERVER:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition" ));
                   break;
                case PRODUCT_DATACENTER_SERVER_CORE:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition (core installation)" ));
                   break;
                case PRODUCT_ENTERPRISE_SERVER:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition" ));
                   break;
                case PRODUCT_ENTERPRISE_SERVER_CORE:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition (core installation)" ));
                   break;
                case PRODUCT_ENTERPRISE_SERVER_IA64:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems" ));
                   break;
                case PRODUCT_SMALLBUSINESS_SERVER:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server" ));
                   break;
                case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server Premium Edition" ));
                   break;
                case PRODUCT_STANDARD_SERVER:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition" ));
                   break;
                case PRODUCT_STANDARD_SERVER_CORE:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition (core installation)" ));
                   break;
                case PRODUCT_WEB_SERVER:
                   StringCchCat(pszOS, BUFSIZE, TEXT("Web Server Edition" ));
                   break;
             }
          }
    
          if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
          {
             if( GetSystemMetrics(SM_SERVERR2) )
                StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Server 2003 R2, "));
             else if ( osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER )
                StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Storage Server 2003"));
             else if ( osvi.wSuiteMask & VER_SUITE_WH_SERVER )
                StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Home Server"));
             else if( osvi.wProductType == VER_NT_WORKSTATION &&
                      si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
             {
                StringCchCat(pszOS, BUFSIZE, TEXT( "Windows XP Professional x64 Edition"));
             }
             else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2003, "));
    
             // Test for the server type.
             if ( osvi.wProductType != VER_NT_WORKSTATION )
             {
                if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64 )
                {
                    if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                       StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition for Itanium-based Systems" ));
                    else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                       StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition for Itanium-based Systems" ));
                }
    
                else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
                {
                    if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                       StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter x64 Edition" ));
                    else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                       StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise x64 Edition" ));
                    else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard x64 Edition" ));
                }
    
                else
                {
                    if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER )
                       StringCchCat(pszOS, BUFSIZE, TEXT( "Compute Cluster Edition" ));
                    else if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                       StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition" ));
                    else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                       StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition" ));
                    else if ( osvi.wSuiteMask & VER_SUITE_BLADE )
                       StringCchCat(pszOS, BUFSIZE, TEXT( "Web Edition" ));
                    else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard Edition" ));
                }
             }
          }
    
          if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
          {
             StringCchCat(pszOS, BUFSIZE, TEXT("Windows XP "));
             if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
                StringCchCat(pszOS, BUFSIZE, TEXT( "Home Edition" ));
             else StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" ));
          }
    
          if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
          {
             StringCchCat(pszOS, BUFSIZE, TEXT("Windows 2000 "));
    
             if ( osvi.wProductType == VER_NT_WORKSTATION )
             {
                StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" ));
             }
             else 
             {
                if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Server" ));
                else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Advanced Server" ));
                else StringCchCat(pszOS, BUFSIZE, TEXT( "Server" ));
             }
          }
    
           // Include service pack (if any) and build number.
    
          if( _tcslen(osvi.szCSDVersion) > 0 )
          {
              StringCchCat(pszOS, BUFSIZE, TEXT(" ") );
              StringCchCat(pszOS, BUFSIZE, osvi.szCSDVersion);
          }
    
          TCHAR buf[80];
    
          StringCchPrintf( buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber);
          StringCchCat(pszOS, BUFSIZE, buf);
    
          if ( osvi.dwMajorVersion >= 6 )
          {
             if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
                StringCchCat(pszOS, BUFSIZE, TEXT( ", 64-bit" ));
             else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL )
                StringCchCat(pszOS, BUFSIZE, TEXT(", 32-bit"));
          }
          
          return TRUE; 
       }
    
       else
       {  
          printf( "This sample does not support this version of Windows.\n");
          return FALSE;
       }
    }
    
    int __cdecl _tmain()
    {
        TCHAR szOS[BUFSIZE];
    
        if( GetOSDisplayString( szOS ) )
            _tprintf( TEXT("\n%s\n"), szOS );
    }
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    27
    For details on versions. OSVERSIONINFOEX Structure (Windows)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. detectine OS type?
    By techevo in forum C Programming
    Replies: 4
    Last Post: 03-12-2010, 05:09 AM
  3. No Version info tab in file properties?
    By cpjust in forum Windows Programming
    Replies: 2
    Last Post: 06-03-2008, 03:42 PM
  4. Replies: 5
    Last Post: 11-20-2003, 01:27 AM
  5. Dev C++ Version 5
    By Zoalord in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 01:56 PM