MSDN例子获取操作系统版本信息Getting the System Version

Getting the System Version

The following example uses the GetVersionEx function to display the version of the currently running operating system.

Relying on version information is not the best way to test for a feature. Instead, refer to the documentation for the feature of interest. For more information on common techniques for feature detection, see Operating System Version.

If you must require a particular operating system, be sure to use it as a minimum supported version, rather than design the test for the one operating system. This way, your detection code will continue to work on future versions of Windows.





#include <windows.h>
#include <stdio.h>

#define BUFSIZE 80

int main()
{
   OSVERSIONINFOEX osvi;
   BOOL bOsVersionInfoEx;

   // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
   // If that fails, try using the OSVERSIONINFO structure.

   ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

   if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
   {
      osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
      if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) 
         return FALSE;
   }

   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 )
         printf ("Microsoft Windows Server 2003, ");

      if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
         printf ("Microsoft Windows XP ");

      if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
         printf ("Microsoft Windows 2000 ");

      if ( osvi.dwMajorVersion <= 4 )
         printf("Microsoft Windows NT ");

      // Test for specific product on Windows NT 4.0 SP6 and later.
      if( bOsVersionInfoEx )
      {
         // Test for the workstation type.
         if ( osvi.wProductType == VER_NT_WORKSTATION )
         {
            if( osvi.dwMajorVersion == 4 )
               printf ( "Workstation 4.0 " );
            else if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
               printf ( "Home Edition " );
            else printf ( "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 )
                  printf ( "Datacenter Edition " );
               else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                  printf ( "Enterprise Edition " );
               else if ( osvi.wSuiteMask == VER_SUITE_BLADE )
                  printf ( "Web Edition " );
               else printf ( "Standard Edition " );
            }
            else if(osvi.dwMajorVersion==5 && osvi.dwMinorVersion==0)
            {
               if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                  printf ( "Datacenter Server " );
               else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                  printf ( "Advanced Server " );
               else printf ( "Server " );
            }
            else  // Windows NT 4.0 
            {
               if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                  printf ("Server 4.0, Enterprise Edition " );
               else printf ( "Server 4.0 " );
            }
         }
      }
      // Test for specific product on Windows NT 4.0 SP5 and earlier
      else  
      {
         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 FALSE;

         lRet = RegQueryValueEx( hKey, "ProductType", NULL, NULL,
            (LPBYTE) szProductType, &dwBufLen);
         if( (lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE) )
            return FALSE;

         RegCloseKey( hKey );

         if ( lstrcmpi( "WINNT", szProductType) == 0 )
            printf( "Workstation " );
         if ( lstrcmpi( "LANMANNT", szProductType) == 0 )
            printf( "Server " );
         if ( lstrcmpi( "SERVERNT", szProductType) == 0 )
            printf( "Advanced Server " );
         printf( "%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 )
            printf( "Service Pack 6a (Build %d)\n", 
            osvi.dwBuildNumber & 0xFFFF );         
         else // Windows NT 4.0 prior to SP6a
         {
            printf( "%s (Build %d)\n",
               osvi.szCSDVersion,
               osvi.dwBuildNumber & 0xFFFF);
         }

         RegCloseKey( hKey );
      }
      else // not Windows NT 4.0 
      {
         printf( "%s (Build %d)\n",
            osvi.szCSDVersion,
            osvi.dwBuildNumber & 0xFFFF);
      }

      break;

      // Test for the Windows Me/98/95.
      case VER_PLATFORM_WIN32_WINDOWS:

      if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
      {
          printf ("Microsoft Windows 95 ");
          if (osvi.szCSDVersion[1]=='C' || osvi.szCSDVersion[1]=='B')
             printf("OSR2 " );
      } 

      if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
      {
          printf ("Microsoft Windows 98 ");
          if ( osvi.szCSDVersion[1] == 'A' )
             printf("SE " );
      } 

      if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
      {
          printf ("Microsoft Windows Millennium Edition\n");
      } 
      break;

      case VER_PLATFORM_WIN32s:

      printf ("Microsoft Win32s\n");
      break;
   }
   return TRUE; 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Win10 VS2013 ************************************************ * BOOL GetDiskSpaceInfo(LPCSTR pszDrive * 功能 根据输入的驱动器,获取磁盘总容量 * 空闲空间、簇数量等磁盘信息 * 参数 驱动器根路径,比如“D:\”。 ************************************************ * void GetTime() * 功能 获取并显示本地与系统时间 ************************************************ * void GetLang() * 功能 获取并显示系统的默认语言 ************************************************ * void ShowVersionInfo() * 功能 获取并显示系统版本信息 ************************************************ * void ShowSystemInfo() * 功能 获取并显示硬件相关信息 ************************************************ *void GetFolders() * 功能 获取系统目录等信息 ************************************************ *void WINAPI EnumProcess1() * 功能 调用EnumProcess遍历进程 * 并调用ListProcessModules1函数和 * ListProcessThreads函数列举模块 * 和线程 ************************************************ * VOID ListProcessModules1( DWORD dwPID ) * 功能 调用EnumProcessModules函数 * 列举和显示进程加载的模块 * * 参数 DWORD dwPID 进程PID ************************************************ * VOID ListProcessThreads( DWORD dwPID ) * 功能 调用Thread32First和Thread32Next * 显示一个进程的线程 * * 参数 DWORD dwPID 进程PID ************************************************ * VOID PrintError( LPTSTR msg ) * 功能 打印出错信息 * * 参数 LPTSTR msg ************************************************ * void GetDriveInfo( LPCTSTR lpRootPath) * 功能 返 回 指 定 驱 动 器 的 类 型。 * * 参数 lpRootPathName ************************************************ * void GetSystemMetric(int nIndex) * 功能 返 回 显 示 器 分 辨 率。 * * 参数 ************************************************ * void GetKeyboardInfo() * 功能 返回键盘类型。 * * 参数 *************************************************

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值