Windows 系统版本判断

时间:2021-07-11 00:05:13

很多情况下,需要软件判断操作系统,其实网上写的都很少,我希望这篇文章能给大家帮助

首先我们要在.h中定义下面的东西

Cpp代码  

#define PRODUCT_UNDEFINED                       0x00000000  

  

#define PRODUCT_ULTIMATE                        0x00000001  

#define PRODUCT_HOME_BASIC                      0x00000002  

#define PRODUCT_HOME_PREMIUM                    0x00000003  

#define PRODUCT_ENTERPRISE                      0x00000004  

#define PRODUCT_HOME_BASIC_N                    0x00000005  

#define PRODUCT_BUSINESS                        0x00000006  

#define PRODUCT_STANDARD_SERVER                 0x00000007  

#define PRODUCT_DATACENTER_SERVER               0x00000008  

#define PRODUCT_SMALLBUSINESS_SERVER            0x00000009  

#define PRODUCT_ENTERPRISE_SERVER               0x0000000A  

#define PRODUCT_STARTER                         0x0000000B  

#define PRODUCT_DATACENTER_SERVER_CORE          0x0000000C  

#define PRODUCT_STANDARD_SERVER_CORE            0x0000000D  

#define PRODUCT_ENTERPRISE_SERVER_CORE          0x0000000E  

#define PRODUCT_ENTERPRISE_SERVER_IA64          0x0000000F  

#define PRODUCT_BUSINESS_N                      0x00000010  

#define PRODUCT_WEB_SERVER                      0x00000011  

#define PRODUCT_CLUSTER_SERVER                  0x00000012  

#define PRODUCT_HOME_SERVER                     0x00000013  

#define PRODUCT_STORAGE_EXPRESS_SERVER          0x00000014  

#define PRODUCT_STORAGE_STANDARD_SERVER         0x00000015  

#define PRODUCT_STORAGE_WORKGROUP_SERVER        0x00000016  

#define PRODUCT_STORAGE_ENTERPRISE_SERVER       0x00000017  

#define PRODUCT_SERVER_FOR_SMALLBUSINESS        0x00000018  

#define PRODUCT_SMALLBUSINESS_SERVER_PREMIUM    0x00000019  

  

#define PRODUCT_UNLICENSED                      0xABCDABCD  

好了,这是必须的定义,为了就是判断操作系统

下面是判断操作系统的代码

Cpp代码  

#include <windows.h>  

#include <tchar.h>  

#include <stdio.h>  

#include <strsafe.h>  

  

#pragma comment(lib, "User32.lib")  

  

#define BUFSIZE 256  

  

typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);  

typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);  

  

BOOL GetOSDisplayString( LPTSTR pszOS)  

{  

   OSVERSIONINFOEX osvi;  

   SYSTEM_INFO si;  

   PGNSI pGNSI;  

   PGPI pGPI;  

   BOOL bOsVersionInfoEx;  

   DWORD dwType;  

  

   ZeroMemory(&si, sizeof(SYSTEM_INFO));  

   ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));  

  

   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);  

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

  

   if(bOsVersionInfoEx != NULL ) return 1;  

  

   // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.  

  

   pGNSI = (PGNSI) GetProcAddress(  

      GetModuleHandle(TEXT("kernel32.dll")),   

      "GetNativeSystemInfo");  

   if(NULL != pGNSI)  

      pGNSI(&si);  

   else GetSystemInfo(&si);  

  

   if ( VER_PLATFORM_WIN32_NT==osvi.dwPlatformId &&   

        osvi.dwMajorVersion > 4 )  

   {  

      StringCchCopy(pszOS, BUFSIZE, TEXT("Microsoft "));  

  

      // Test for the specific product.  

  

      if ( osvi.dwMajorVersion == 6 )  

      {  

         if( osvi.dwMinorVersion == 0 )  

         {  

            if( osvi.wProductType == VER_NT_WORKSTATION )  

                StringCchCat(pszOS, BUFSIZE, TEXT("Windows Vista "));