C++ 正在检索MS Windows平台的操作系统名称

C++ 正在检索MS Windows平台的操作系统名称,c++,windows,wmi,wmi-query,C++,Windows,Wmi,Wmi Query,我需要获得有关MS Windows平台操作系统名称的信息。我在谷歌上搜索了一下,发现GetVersionEx可以。但我只需要OS名称,有没有最简单的方法来实现它?未使用系统()指导我谢谢 我正在使用WMI使用此代码: #define _WIN32_DCOM #include <iostream> using namespace std; #include <comdef.h> #include <Wbemidl.h> # pragma comment(lib

我需要获得有关MS Windows平台操作系统名称的信息。我在谷歌上搜索了一下,发现GetVersionEx可以。但我只需要OS名称,有没有最简单的方法来实现它?未使用系统()指导我谢谢

我正在使用WMI使用此代码:

#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>

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

int main(int argc, char **argv)
{
    HRESULT hres;

    // Step 1: --------------------------------------------------
    // Initialize COM. ------------------------------------------

    hres =  CoInitializeEx(0, COINIT_MULTITHREADED); 
    if (FAILED(hres))
    {
        cout << "Failed to initialize COM library. Error code = 0x" 
            << hex << hres << endl;
        return 1;                  // Program has failed.
    }

    // Step 2: --------------------------------------------------
    // Set general COM security levels --------------------------
    // Note: If you are using Windows 2000, you need to specify -
    // the default authentication credentials for a user by using
    // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
    // parameter of CoInitializeSecurity ------------------------

    hres =  CoInitializeSecurity(
        NULL, 
        -1,                          // COM authentication
        NULL,                        // Authentication services
        NULL,                        // Reserved
        RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  
        NULL,                        // Authentication info
        EOAC_NONE,                   // Additional capabilities 
        NULL                         // Reserved
        );


    if (FAILED(hres))
    {
        cout << "Failed to initialize security. Error code = 0x" 
            << hex << hres << endl;
        CoUninitialize();
        return 1;                    // Program has failed.
    }

    // Step 3: ---------------------------------------------------
    // Obtain the initial locator to WMI -------------------------

    IWbemLocator *pLoc = NULL;

    hres = CoCreateInstance(
        CLSID_WbemLocator,             
        0, 
        CLSCTX_INPROC_SERVER, 
        IID_IWbemLocator, (LPVOID *) &pLoc);

    if (FAILED(hres))
    {
        cout << "Failed to create IWbemLocator object."
            << " Err code = 0x"
            << hex << hres << endl;
        CoUninitialize();
        return 1;                 // Program has failed.
    }

    // Step 4: -----------------------------------------------------
    // Connect to WMI through the IWbemLocator::ConnectServer method

    IWbemServices *pSvc = NULL;

    // Connect to the root\cimv2 namespace with
    // the current user and obtain pointer pSvc
    // to make IWbemServices calls.
    hres = pLoc->ConnectServer(
         _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
         NULL,                    // User name. NULL = current user
         NULL,                    // User password. NULL = current
         0,                       // Locale. NULL indicates current
         NULL,                    // Security flags.
         0,                       // Authority (for example, Kerberos)
         0,                       // Context object 
         &pSvc                    // pointer to IWbemServices proxy
         );

    if (FAILED(hres))
    {
        cout << "Could not connect. Error code = 0x" 
             << hex << hres << endl;
        pLoc->Release();     
        CoUninitialize();
        return 1;                // Program has failed.
    }

    cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;


    // Step 5: --------------------------------------------------
    // Set security levels on the proxy -------------------------

    hres = CoSetProxyBlanket(
       pSvc,                        // Indicates the proxy to set
       RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
       RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
       NULL,                        // Server principal name 
       RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
       RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
       NULL,                        // client identity
       EOAC_NONE                    // proxy capabilities 
    );

    if (FAILED(hres))
    {
        cout << "Could not set proxy blanket. Error code = 0x" 
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();     
        CoUninitialize();
        return 1;               // Program has failed.
    }

    // Step 6: --------------------------------------------------
    // Use the IWbemServices pointer to make requests of WMI ----

    // For example, get the name of the operating system
    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("WQL"), 
        bstr_t("SELECT * FROM Win32_OperatingSystem"),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL,
        &pEnumerator);

    if (FAILED(hres))
    {
        cout << "Query for operating system name failed."
            << " Error code = 0x" 
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;               // Program has failed.
    }

    // Step 7: -------------------------------------------------
    // Get the data from the query in step 6 -------------------

    IWbemClassObject *pclsObj;
    ULONG uReturn = 0;

    while (pEnumerator)
    {
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, 
            &pclsObj, &uReturn);

        if(0 == uReturn)
        {
            break;
        }

        VARIANT vtProp;

        // Get the value of the Name property
        hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
        wcout << " OS Name : " << vtProp.bstrVal << endl;
        VariantClear(&vtProp);

        pclsObj->Release();
    }

    // Cleanup
    // ========

    pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();
    pclsObj->Release();
    CoUninitialize();

    return 0;   // Program successfully completed.

}
\define\u WIN32\u DCOM
#包括
使用名称空间std;
#包括
#包括
#pragma注释(lib,“wbemuid.lib”)
int main(int argc,字符**argv)
{
HRESULT hres;
//步骤1:--------------------------------------------------
//初始化COM------------------------------------------
hres=CoInitializeX(0,Conit_多线程);
如果(失败(hres))
{

cout这就是解决方案。如果有人得到.dll丢失错误,请在发布模式下重新编译它(我是VC++2010供参考)

#包括“stdafx.h”
#包括
#包括
使用名称空间std;
#包括
#包括
#包括
#pragma注释(lib,“User32.lib”)
#定义bufsize256
typedef void(WINAPI*PGNSI)(LPSYSTEM_信息);
typedef BOOL(WINAPI*PGPI)(DWORD、DWORD、DWORD、PDWORD);
BOOL GetOSDisplayString(LPTSTR pszOS)
{
OsVersionInfo-osvi;
系统信息系统;
PGNSI-PGNSI;
PGPI-PGPI;
BOOL BosversionInfo;
DWORD dwType;
零内存(&si,sizeof(系统信息));
零内存(&osvi,sizeof(OSVersionInfo));
osvi.dwosVersionInfo=sizeof(osVersionInfo);
BosVersionInfo=GetVersionEx((OSVERSIONINFO*)&osvi);
如果(!BosVersionInfo)返回1;
//如果支持,则调用GetNativeSystemInfo,否则调用GetSystemInfo。
pGNSI=(pGNSI)GetProcAddress(
GetModuleHandle(文本(“kernel32.dll”),
“GetNativeSystemInfo”);
如果(NULL!=pGNSI)
pGNSI&si;
else获取系统信息(&si);
if(VER_PLATFORM_WIN32_NT==osvi.dwPlatformId&&
osvi.dwMajorVersion>4)
{
StringCchCopy(pszOS、BUFSIZE、TEXT(“微软”);
//针对特定产品进行测试。
if(osvi.dwMajorVersion==6)
{
if(osvi.dwMinorVersion==0)
{
if(osvi.wProductType==VER\u NT\u工作站)
StringCchCat(pszOS、BUFSIZE、TEXT(“Windows Vista”);
else StringCchCat(pszOS、BUFSIZE、TEXT(“Windows Server 2008”);
}
if(osvi.dwMinorVersion==1 | | osvi.dwMinorVersion==2)
{
if(osvi.wProductType==VER\u NT\u工作站和&osvi.dwMinorVersion==1)
StringCchCat(pszOS,BUFSIZE,TEXT(“Windows 7”));
其他的
if(osvi.wProductType==VER\u NT\u工作站和&osvi.dwMinorVersion==2)
StringCchCat(pszOS,BUFSIZE,TEXT(“Windows 8”));
其他的
StringCchCat(pszOS、BUFSIZE、TEXT(“Windows Server 2008 R2”);
}
pGPI=(pGPI)GetProcAddress(
GetModuleHandle(文本(“kernel32.dll”),
“GetProductInfo”);
pGPI(osvi.dwmajorvision、osvi.dwMinorVersion、0、0和dwType);
开关(DW型)
{
case PRODUCT_ULTIMATE:
StringCchCat(pszOS,BUFSIZE,文本(“最终版”);
打破
case PRODUCT_PROFESSIONAL:
StringCchCat(pszOS、BUFSIZE、文本(“专业”);
打破
案例产品\家庭\高级:
StringCchCat(pszOS,BUFSIZE,文本(“家庭高级版”);
打破
case PRODUCT_HOME_BASIC:
StringCchCat(pszOS,BUFSIZE,文本(“家庭基本版”);
打破
企业案例产品:
StringCchCat(pszOS,BUFSIZE,文本(“企业版”);
打破
案例产品业务:
StringCchCat(pszOS,BUFSIZE,文本(“商业版”);
打破
案例产品(u启动器):
StringCchCat(pszOS,BUFSIZE,文本(“起始版”);
打破
案例产品集群服务器:
StringCchCat(pszOS,BUFSIZE,TEXT(“集群服务器版”);
打破
案例产品\数据中心\服务器:
StringCchCat(pszOS,BUFSIZE,文本(“数据中心版”);
打破
案例产品\数据中心\服务器\核心:
StringCchCat(pszOS,BUFSIZE,TEXT(“数据中心版(核心安装)”);
打破
案例产品\企业\服务器:
StringCchCat(pszOS,BUFSIZE,文本(“企业版”);
打破
案例产品\企业\服务器\核心:
StringCchCat(pszOS,BUFSIZE,TEXT(“企业版(核心安装)”);
打破
案例产品\企业\服务器\ IA64:
StringCchCat(pszOS,BUFSIZE,TEXT(“基于安腾系统的企业版”);
打破
案例产品\u小型企业\u服务器:
StringCchCat(pszOS、BUFSIZE、TEXT(“小型企业服务器”);
打破
案例产品\u小型企业\u服务器\u高级:
StringCchCat(pszOS,BUFSIZE,TEXT(“小型企业服务器高级版”);
打破
案例产品\u标准\u服务器:
StringCchCat(pszOS,BUFSIZE,文本(“标准版”);
打破
案例产品\u标准\u服务器\u核心:
StringCchCat(pszOS,BUFSIZE,TEXT(“标准版(核心安装)”);
打破
案例产品\网络\服务器:
StringCchCat(pszOS,BUFSIZE,TEXT(“Web服务器版”);
打破
}
}
if(osvi.dwMajorVersion==5&&osvi.dwMinorVersion==2)
{
if(GetSystemMetrics(SM_服务器R2))
StringCchCat(pszOS,BUFSIZE,TEXT(“Windows Server 2003 R2”);
else if(osvi.wSuiteMask&版本套件存储服务器)
StringCchCat(pszOS、BUFSIZE、TEXT(“Windows存储服务器2003”);
else if(osvi.wSuiteMas
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
#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 ) 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 "));
            else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 " ));
         }

         if ( osvi.dwMinorVersion == 1 || osvi.dwMinorVersion == 2 )
         {
            if ( osvi.wProductType == VER_NT_WORKSTATION && osvi.dwMinorVersion == 1)
                StringCchCat(pszOS, BUFSIZE, TEXT("Windows 7 "));
            else
            if ( osvi.wProductType == VER_NT_WORKSTATION && osvi.dwMinorVersion == 2)
                StringCchCat(pszOS, BUFSIZE, TEXT("Windows 8 "));
            else 
                StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 R2 " ));
         }

         pGPI = (PGPI) GetProcAddress(
            GetModuleHandle(TEXT("kernel32.dll")), 
            "GetProductInfo");

         pGPI( osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);

         switch( dwType )
         {
            case PRODUCT_ULTIMATE:
               StringCchCat(pszOS, BUFSIZE, TEXT("Ultimate Edition" ));
               break;
            case PRODUCT_PROFESSIONAL:
               StringCchCat(pszOS, BUFSIZE, TEXT("Professional" ));
               break;
            case PRODUCT_HOME_PREMIUM:
               StringCchCat(pszOS, BUFSIZE, TEXT("Home Premium Edition" ));
               break;
            case PRODUCT_HOME_BASIC:
               StringCchCat(pszOS, BUFSIZE, TEXT("Home Basic Edition" ));
               break;
            case PRODUCT_ENTERPRISE:
               StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition" ));
               break;
            case PRODUCT_BUSINESS:
               StringCchCat(pszOS, BUFSIZE, TEXT("Business Edition" ));
               break;
            case PRODUCT_STARTER:
               StringCchCat(pszOS, BUFSIZE, TEXT("Starter Edition" ));
               break;
            case PRODUCT_CLUSTER_SERVER:
               StringCchCat(pszOS, BUFSIZE, TEXT("Cluster Server Edition" ));
               break;
            case PRODUCT_DATACENTER_SERVER:
               StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition" ));
               break;
            case PRODUCT_DATACENTER_SERVER_CORE:
               StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition (core installation)" ));
               break;
            case PRODUCT_ENTERPRISE_SERVER:
               StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition" ));
               break;
            case PRODUCT_ENTERPRISE_SERVER_CORE:
               StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition (core installation)" ));
               break;
            case PRODUCT_ENTERPRISE_SERVER_IA64:
               StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems" ));
               break;
            case PRODUCT_SMALLBUSINESS_SERVER:
               StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server" ));
               break;
            case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
               StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server Premium Edition" ));
               break;
            case PRODUCT_STANDARD_SERVER:
               StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition" ));
               break;
            case PRODUCT_STANDARD_SERVER_CORE:
               StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition (core installation)" ));
               break;
            case PRODUCT_WEB_SERVER:
               StringCchCat(pszOS, BUFSIZE, TEXT("Web Server Edition" ));
               break;
         }
      }

      if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
      {
         if( GetSystemMetrics(SM_SERVERR2) )
            StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Server 2003 R2, "));
         else if ( osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER )
            StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Storage Server 2003"));
         else if ( osvi.wSuiteMask & VER_SUITE_WH_SERVER )
            StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Home Server"));
         else if( osvi.wProductType == VER_NT_WORKSTATION &&
                  si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
         {
            StringCchCat(pszOS, BUFSIZE, TEXT( "Windows XP Professional x64 Edition"));
         }
         else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2003, "));

         // Test for the server type.
         if ( osvi.wProductType != VER_NT_WORKSTATION )
         {
            if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64 )
            {
                if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition for Itanium-based Systems" ));
                else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition for Itanium-based Systems" ));
            }

            else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
            {
                if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter x64 Edition" ));
                else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise x64 Edition" ));
                else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard x64 Edition" ));
            }

            else
            {
                if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Compute Cluster Edition" ));
                else if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition" ));
                else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition" ));
                else if ( osvi.wSuiteMask & VER_SUITE_BLADE )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Web Edition" ));
                else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard Edition" ));
            }
         }
      }

      if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
      {
         StringCchCat(pszOS, BUFSIZE, TEXT("Windows XP "));
         if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
            StringCchCat(pszOS, BUFSIZE, TEXT( "Home Edition" ));
         else StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" ));
      }

      if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
      {
         StringCchCat(pszOS, BUFSIZE, TEXT("Windows 2000 "));

         if ( osvi.wProductType == VER_NT_WORKSTATION )
         {
            StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" ));
         }
         else 
         {
            if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
               StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Server" ));
            else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
               StringCchCat(pszOS, BUFSIZE, TEXT( "Advanced Server" ));
            else StringCchCat(pszOS, BUFSIZE, TEXT( "Server" ));
         }
      }

       // Include service pack (if any) and build number.

      if( _tcslen(osvi.szCSDVersion) > 0 )
      {
          StringCchCat(pszOS, BUFSIZE, TEXT(" ") );
          StringCchCat(pszOS, BUFSIZE, osvi.szCSDVersion);
      }

      TCHAR buf[80];

      StringCchPrintf( buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber);
      StringCchCat(pszOS, BUFSIZE, buf);

      if ( osvi.dwMajorVersion >= 6 )
      {
         if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
            StringCchCat(pszOS, BUFSIZE, TEXT( ", 64-bit" ));
         else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL )
            StringCchCat(pszOS, BUFSIZE, TEXT(", 32-bit"));
      }

      return TRUE; 
   }

   else
   {  
      printf( "This sample does not support this version of Windows.\n");
      return FALSE;
   }
}

int __cdecl _tmain()
{
    TCHAR szOS[BUFSIZE];

    if( GetOSDisplayString( szOS ) )
    {
        _tprintf( TEXT("\n%s\n"), szOS );
        cin.get();
    }
}