windows版本检测函数

本文提供了一个C语言函数,用于从Windows系统中获取版本信息,并通过不同的条件判断展示系统的具体类型和版本,包括Windows NT家族、Windows 95家族以及Windows 7等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

//复制一下内容到文本中,改名文本为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; 
 } 
}

Delphi获取Windows版本号、描述等信息,可获取到主版本号、次版本号、系统描述、系统平台、构建号等,相关代码如下:   //设置版本信息结构的大小    GetVersionEx(OSVI);    //获取版本信息    is98orlater:=    //判断是否98或以后版本    (osvi.dwPlatformId=VER_PLATFORM_WIN32_WINDOWS) and    ((osvi.dwMajorVersion>4) or    ((osvi.dwMajorVersion=4) and (osvi.dwMinorVersion>0)));   //DOWNLOAD BY HTTP://WWW.CODEFANS.NET    //下面开始显示信息    case OSVI.dwPlatformId of    //根据OSVI.dwPlatformId的数值的不同显示具体的平台描述    VER_PLATFORM_WIN32s:    // Windows 3.1平台    s:='Windows 3.1';    VER_PLATFORM_WIN32_WINDOWS:    // Windows 95/98平台    if(is98orlater) then    //98    s:='Windows 98'    else    //95    s:='Windows 95';    VER_PLATFORM_WIN32_NT:    // Windows NT平台    s:='Windows NT/XP';    end;    Edit1.Text:=s;    Edit2.Text:=IntToStr(OSVI.dwMajorVersion);    Edit3.Text:=IntToStr(OSVI.dwMinorVersion);    case OSVI.dwPlatformId of    //根据平台的不同具体处理OSVI.dwBuildNumber信息    VER_PLATFORM_WIN32_WINDOWS:    // Windows 95/98平台则取OSVI.dwBuildNumber的低位字    Edit4.Text:=IntToStr(LOWORD(OSVI.dwBuildNumber));    VER_PLATFORM_WIN32_NT:    // Windows NT平台则取所有位的值    Edit4.Text:=IntToStr(OSVI.dwBuildNumber);    else    Edit4.Text:='';   // Windows 3.1平台此值位空    end;    Edit5.Text:=OSVI.szCSDVersion;   end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chinabinlang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值