现在的位置: 首页 > 综合 > 正文

获取Windows操作系统版本

2012年12月15日 ⁄ 综合 ⁄ 共 7332字 ⁄ 字号 评论关闭
根据MSDN改的。
可以判断系统版本是Professional、Server、Home、Advanced Server、Enterprise Edition、Workstation等。还不支持Vista。以后可能支持Mac、Unix和Linux。

/**
* Author:    Soli
* Date:    2008-10-09
*/


#include 
<string>

#ifdef _WIN32
#include 
<windows.h>

#define VER_SUITE_BACKOFFICE                0x00000004
#define VER_SUITE_BLADE                        0x00000400
#define VER_SUITE_DATACENTER                0x00000080
#define VER_SUITE_ENTERPRISE                0x00000002
#define VER_SUITE_EMBEDDEDNT                0x00000040
#define VER_SUITE_PERSONAL                    0x00000200
#define VER_SUITE_SINGLEUSERTS                0x00000100
#define VER_SUITE_SMALLBUSINESS                0x00000001
#define VER_SUITE_SMALLBUSINESS_RESTRICTED    0x00000020
#define VER_SUITE_TERMINAL                    0x00000010

#define VER_NT_DOMAIN_CONTROLLER            0x0000002
#define VER_NT_SERVER                        0x0000003
#define VER_NT_WORKSTATION                    0x0000001

typedef 
struct _OSVERSIONINFOEX_VC7 {
    DWORD dwOSVersionInfoSize;
    DWORD dwMajorVersion;
    DWORD dwMinorVersion;
    DWORD dwBuildNumber;
    DWORD dwPlatformId;
    TCHAR szCSDVersion[
128];
    WORD wServicePackMajor;
    WORD wServicePackMinor;
    WORD wSuiteMask;
    BYTE wProductType;
    BYTE wReserved;
}
 OSVERSIONINFOEX_VC7, *POSVERSIONINFOEX_VC7, *LPOSVERSIONINFOEX_VC7;

#endif

template 
<class T>
const std::string ToString(const T data, const std::string format)
{
    
char* ptmp = NULL;
    
for (int i=100; ; i += 100)
    
{
        ptmp 
= new char[i];
        
        
if(NULL == ptmp)
            
break;
        
        
if(_snprintf(ptmp, i-1, format.c_str(), data) < 0)
        
{
            delete [] ptmp;
            ptmp 
= NULL;
        }

        
else
            
break;
    }

    
    
if(NULL == ptmp)
        
return "";
    
    std::
string str;
    str 
= ptmp;
    delete [] ptmp;
    ptmp 
= NULL;

    
return str;
}


#ifdef _WIN32
std::
string GetOSVersionWin()
{
    std::
string osv;
    
    OSVERSIONINFOEX_VC7 osvi;
    BOOL bOsVersionInfoEx;
    BOOL bOsVersionInfoEx_Vc7;
    
    
// Try calling GetVersionEx using the OSVERSIONINFOEX structure.
    
// If that fails, try using the OSVERSIONINFO structure.
    
    ZeroMemory(
&osvi, sizeof(OSVERSIONINFOEX_VC7));
    osvi.dwOSVersionInfoSize 
= sizeof(OSVERSIONINFOEX_VC7);
    
    
if!(bOsVersionInfoEx_Vc7 = GetVersionEx ((OSVERSIONINFO *&osvi)) )
    
{
        osvi.dwOSVersionInfoSize 
= sizeof (OSVERSIONINFOEX);
        
if (! (bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *&osvi)) ) 
        
{
            osvi.dwOSVersionInfoSize 
= sizeof (OSVERSIONINFO);
            
if (! GetVersionEx ((OSVERSIONINFO *&osvi) )
                
return osv;
        }

    }

    
    
switch (osvi.dwPlatformId)
    
{
        
// Test for the Windows NT product family.
    case VER_PLATFORM_WIN32_NT:
        
        
// Test for the specific product.
        if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
            osv 
= ("Microsoft Windows Server 2003, ");
        
        
else if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
            osv 
= ("Microsoft Windows XP ");
        
        
else if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
            osv 
= ("Microsoft Windows 2000 ");
        
        
else if ( osvi.dwMajorVersion <= 4 )
            osv 
= ("Microsoft Windows NT ");
        
        
// Test for specific product on Windows NT 4.0 SP6 and later.
        if( bOsVersionInfoEx_Vc7 )
        
{
            
// Test for the workstation type.
            if ( osvi.wProductType == VER_NT_WORKSTATION )
            
{
                
if( osvi.dwMajorVersion == 4 )
                    osv 
+= ( "Workstation 4.0 " );
                
else if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
                    osv 
+= ( "Home Edition " );
                
else osv += ( "Professional " );
            }

            
            
// Test for the server type.
            else if ( osvi.wProductType == VER_NT_SERVER || 
                osvi.wProductType 
== VER_NT_DOMAIN_CONTROLLER )
            
{
                
if(osvi.dwMajorVersion==5 && osvi.dwMinorVersion==2)
                
{
                    
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                        osv 
+= ( "Datacenter Edition " );
                    
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                        osv 
+= ( "Enterprise Edition " );
                    
else if ( osvi.wSuiteMask == VER_SUITE_BLADE )
                        osv 
+= ( "Web Edition " );
                    
else osv += ( "Standard Edition " );
                }

                
else if(osvi.dwMajorVersion==5 && osvi.dwMinorVersion==0)
                
{
                    
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                        osv 
+= ( "Datacenter Server " );
                    
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                        osv 
+= ( "Advanced Server " );
                    
else osv += ( "Server " );
                }

                
else  // Windows NT 4.0 
                {
                    
if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                        osv 
+= ("Server 4.0, Enterprise Edition " );
                    
else osv += ( "Server 4.0 " );
                }

            }

        }

        
// Test for specific product on Windows NT 4.0 SP5 and earlier
        else  
        
{
            
const int BUFSIZE = 80;
            HKEY hKey;
            
char szProductType[BUFSIZE];
            DWORD dwBufLen
=BUFSIZE;
            LONG lRet;
            
            lRet 
= RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                
"SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
                
0, KEY_QUERY_VALUE, &hKey );
            
if( lRet != ERROR_SUCCESS )
                
return osv;
            
            lRet 
= RegQueryValueEx( hKey, "ProductType", NULL, NULL,
                (LPBYTE) szProductType, 
&dwBufLen);

            RegCloseKey( hKey );

            
if( (lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE) )
                
return osv;
            
            
if ( lstrcmpi( "WINNT", szProductType) == 0 )
                osv 
+= ( "Workstation " );
            
else if ( lstrcmpi( "LANMANNT", szProductType) == 0 )
                osv 
+= ( "Server " );
            
else if ( lstrcmpi( "SERVERNT", szProductType) == 0 )
                osv 
+= ( "Advanced Server " );
            osv 
+= ( "%d.%d ", osvi.dwMajorVersion, osvi.dwMinorVersion );
        }

        
        
// Display service pack (if any) and build number.
        
        
if( osvi.dwMajorVersion == 4 && 
            lstrcmpi( osvi.szCSDVersion, 
"Service Pack 6" ) == 0 )
        

            HKEY hKey;
            LONG lRet;
            
            
// Test for SP6 versus SP6a.
            lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009",
                
0, KEY_QUERY_VALUE, &hKey );
            
if( lRet == ERROR_SUCCESS )
                osv 
+= "Service Pack 6a (Build " + ToString(osvi.dwBuildNumber & 0xFFFF"%d" ) + ")";
            
else // Windows NT 4.0 prior to SP6a
            {
                osv 
+= osvi.szCSDVersion;
                osv 
+= " (Build " + ToString(osvi.dwBuildNumber & 0xFFFF"%d" ) + ")";
            }

            
            RegCloseKey( hKey );
        }

        
else // not Windows NT 4.0 
        {
            osv 
+= osvi.szCSDVersion;
            osv 
+= " (Build " + ToString(osvi.dwBuildNumber & 0xFFFF"%d" ) + ")";
        }

抱歉!评论已关闭.