天天看點

windows版本檢測函數

//複制一下内容到文本中,改名文本為WindowsVersion.h,這樣就可以在你的程式中調用BOOL DisplaySystemVersion(LPTSTR buffer)函數了,當然包含頭檔案了;

//下面的函數其實也不是原創了,這裡我簡單添加了一些定義,這樣這個檔案就可以在VC6中應用了;

#define VER_NT_WORKSTATION              0x0000001

#define VER_NT_DOMAIN_CONTROLLER        0x0000002

#define VER_NT_SERVER                   0x0000003

//

// dwPlatformId defines:

//

#define VER_PLATFORM_WIN32s             0

#define VER_PLATFORM_WIN32_WINDOWS      1

#define VER_PLATFORM_WIN32_NT           2

#define VER_SERVER_NT                       0x80000000

#define VER_WORKSTATION_NT                  0x40000000

#define VER_SUITE_SMALLBUSINESS             0x00000001

#define VER_SUITE_ENTERPRISE                0x00000002

#define VER_SUITE_BACKOFFICE                0x00000004

#define VER_SUITE_COMMUNICATIONS            0x00000008

#define VER_SUITE_TERMINAL                  0x00000010

#define VER_SUITE_SMALLBUSINESS_RESTRICTED  0x00000020

#define VER_SUITE_EMBEDDEDNT                0x00000040

#define VER_SUITE_DATACENTER                0x00000080

#define VER_SUITE_SINGLEUSERTS              0x00000100

#define VER_SUITE_PERSONAL                  0x00000200

#define VER_SUITE_BLADE                     0x00000400

#define VER_SUITE_EMBEDDED_RESTRICTED       0x00000800

#define VER_SUITE_SECURITY_APPLIANCE        0x00001000

#define VER_SUITE_STORAGE_SERVER            0x00002000

#define VER_SUITE_COMPUTE_SERVER            0x00004000

#define VER_SUITE_WH_SERVER                 0x00008000

typedef   struct   OSVERSIONINFOEX1

{

 DWORD   dwOSVersionInfoSize;

 DWORD   dwMajorVersion;

 DWORD   dwMinorVersion;

 DWORD   dwBuildNumber;

 DWORD   dwPlatformId;

 WCHAR     szCSDVersion[   128   ];           //   Maintenance   string   for   PSS   usage

 WORD       wServicePackMajor;

 WORD       wServicePackMinor;

 WORD       wSuiteMask;

 BYTE     wProductType;

 BYTE     wReserved;

}   OSVERSIONINFOEXW1;

//#ifdef   UNICODE

//typedef   OSVERSIONINFOEXW1   OSVERSIONINFOEX ;

BOOL DisplaySystemVersion(LPTSTR buffer)

{

 OSVERSIONINFOEX1  osvi;

 BOOL bOsVersionInfoEx;

 LPTSTR tmp=buffer;

 // Try calling GetVersionEx using the OSVERSIONINFOEX structure.

 // If that fails, try using the OSVERSIONINFO structure.

 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));

 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

 bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi);

 if( !bOsVersionInfoEx )

 {

  // If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.

  osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);

  if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )

   return FALSE;

 }

 switch (osvi.dwPlatformId)

 {

  // Tests for Windows NT product family.

 case VER_PLATFORM_WIN32_NT:

  // Test for the product.

  if ( osvi.dwMajorVersion <= 4 )

   tmp+=_stprintf( tmp, _T("Microsoft Windows NT ") );

  if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )

   tmp+=_stprintf( tmp, _T("Microsoft Windows 2000 ") );

  if( bOsVersionInfoEx )  // Use information from GetVersionEx.

  {

   // Test for the workstation type.

   if ( osvi.wProductType == VER_NT_WORKSTATION )

   {

    if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )

     tmp+=_stprintf ( tmp, _T("Microsoft Windows XP ") );

    if( osvi.wSuiteMask & VER_SUITE_PERSONAL )

     tmp+=_stprintf ( tmp, _T("Home Edition ") );

    else

     tmp+=_stprintf ( tmp, _T("Professional ") );

   }

   // Test for the server type.

   else if ( osvi.wProductType == VER_NT_SERVER )

   {

    if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )

     tmp+=_stprintf ( tmp, _T("Microsoft Windows .NET ") );

    if( osvi.wSuiteMask & VER_SUITE_DATACENTER )

     tmp+=_stprintf ( tmp, _T("DataCenter Server ") );

    else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )

     if( osvi.dwMajorVersion == 4 )

      tmp+=_stprintf ( tmp, _T("Advanced Server ") );

     else

      tmp+=_stprintf ( tmp, _T("Enterprise Server ") );

    else if ( osvi.wSuiteMask == VER_SUITE_BLADE )

     tmp+=_stprintf ( tmp, _T("Web Server ") );

    else

     tmp+=_stprintf ( tmp, _T("Server ") );

   }

  }

  else   // Use the registry on early versions of Windows NT.

  {

   HKEY hKey;

   TCHAR szProductType[80];

   DWORD dwBufLen=80*sizeof(TCHAR);

   RegOpenKeyEx( HKEY_LOCAL_MACHINE,

    _T("SYSTEM\\CurrentControlSet\\Control\\ProductOptions"),

    0, KEY_QUERY_VALUE, &hKey );

   RegQueryValueEx( hKey, _T("ProductType"), NULL, NULL,

    (LPBYTE) szProductType, &dwBufLen);

   RegCloseKey( hKey );

   if ( lstrcmpi( _T("WINNT"), szProductType) == 0 )

    tmp+=_stprintf( tmp, _T("Professional ") );

   if ( lstrcmpi( _T("LANMANNT"), szProductType) == 0 )

    tmp+=_stprintf( tmp, _T("Server ") );

   if ( lstrcmpi( _T("SERVERNT"), szProductType) == 0 )

    tmp+=_stprintf( tmp, _T("Advanced Server ") );

  }

  // Display version, service pack (if any), and build number.

  if ( osvi.dwMajorVersion <= 4 )

  {

   tmp+=_stprintf ( tmp, _T("version %d.%d %s (Build %d)"),

    osvi.dwMajorVersion,

    osvi.dwMinorVersion,

    osvi.szCSDVersion,

    osvi.dwBuildNumber & 0xFFFF);

  }

  else

  {

   tmp+=_stprintf ( tmp, _T("%s (Build %d)"),

    osvi.szCSDVersion,

    osvi.dwBuildNumber & 0xFFFF);

  }

  break;

  // Test for the Windows 95 product family.

 case VER_PLATFORM_WIN32_WINDOWS:

  if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)

  {

   tmp+=_stprintf ( tmp, _T("Microsoft Windows 95 ") );

   if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' )

    tmp+=_stprintf( tmp, _T("OSR2 ") );

  }

  if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)

  {

   tmp+=_stprintf ( tmp, _T("Microsoft Windows 98 ") );

   if ( osvi.szCSDVersion[1] == 'A' )

    tmp+=_stprintf( tmp, _T("SE ") );

  }

  if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)

  {

   tmp+=_stprintf ( tmp, _T("Microsoft Windows Millennium Edition ") );

  }

  break;

 }

 if (tmp==buffer)

  tmp+=_stprintf( tmp, _T("%d.%d build %d"), osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber);

 return TRUE;

}

//--------------------------------------------------------------

這裡還有一個更簡單的函數判斷系統是否為win7:

bool isWin7() 

 OSVERSIONINFOEX osvi; 

    BOOL bOsVersionInfoEx; 

 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); 

    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); 

    bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*) &osvi); 

 // win7的系統版本為NT6.1 

    if ( VER_PLATFORM_WIN32_NT == osvi.dwPlatformId &&   

        osvi.dwMajorVersion == 6 &&  

        osvi.dwMinorVersion == 1 ) 

    { 

        return true;     

    } 

 else 

 { 

  return false; 

 } 

}

繼續閱讀