天天看点

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; 

 } 

}

继续阅读