c++ - 在 C++ 中列出 WMI 类的所有属性

标签 c++ wmi wmi-query

谁知道在 C++ 中获取给定 WMI 类中每个可用属性的完整列表的方法?我在 SO 上发现了一些类似的问题,但它们都使用 Powershell、VB 或 C#。

最佳答案

您可以使用 GetObject方法获取 WMI 类的实例并使用 GetNames方法,您可以检索属性名称。

试试这个示例。

#include "stdafx.h"
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")

#pragma argsused
int main(int argc, char* argv[])
{
    BSTR strNetworkResource;
    //To use a WMI remote connection set localconn to false and configure the values of the pszName, pszPwd and the name of the remote machine in strNetworkResource
    bool localconn = true;  
    strNetworkResource = localconn ?  L"\\\\.\\root\\CIMV2" : L"\\\\remote--machine\\root\\CIMV2";

    // Initialize COM. ------------------------------------------

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

    // Set general COM security levels --------------------------


        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;
        cout << _com_error(hres).ErrorMessage() << endl;
        CoUninitialize();
        cout << "press enter to exit" << endl;
        cin.get();      
        return 1;                    // Program has failed.
    }

    // 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;
        cout << _com_error(hres).ErrorMessage() << endl;
        CoUninitialize();       
        cout << "press enter to exit" << endl;
        cin.get();      
        return 1;                 // Program has failed.
    }

    // Connect to WMI through the IWbemLocator::ConnectServer method

    IWbemServices *pSvc = NULL;


        hres = pLoc->ConnectServer(
             _bstr_t(strNetworkResource),      // 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 (e.g. Kerberos)
             0,                       // Context object
             &pSvc                    // pointer to IWbemServices proxy
             );

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

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

    // 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;
        cout << _com_error(hres).ErrorMessage() << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        cout << "press enter to exit" << endl;
        cin.get();      
        return 1;               // Program has failed.
    }

    // Use the IWbemServices pointer to make requests of WMI ----
    IWbemClassObject* pClass = NULL;
    hres = pSvc->GetObject(L"Win32_Process", 0, NULL, &pClass, NULL);

    if (FAILED(hres))
    {
        cout << "GetObject failed" << " Error code = 0x"    << hex << hres << endl;
        cout << _com_error(hres).ErrorMessage() << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        cout << "press enter to exit" << endl;
        cin.get();      
        return 1;               // Program has failed.
    }

    SAFEARRAY *psaNames = NULL;
    hres = pClass->GetNames(
        NULL, 
        WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY, 
        NULL, 
        &psaNames);


    if (FAILED(hres))
    {
        cout << "GetNames failed" << " Error code = 0x" << hex << hres << endl;
        cout << _com_error(hres).ErrorMessage() << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        cout << "press enter to exit" << endl;
        cin.get();      
        return 1;               // Program has failed.
    }


   // Get the number of properties.
   long lLower, lUpper; 
   BSTR PropName = NULL;
   SafeArrayGetLBound(psaNames, 1, &lLower);
   SafeArrayGetUBound(psaNames, 1, &lUpper);

        for (long i = lLower; i <= lUpper; i++) 
        {
            // Get this property.
            hres = SafeArrayGetElement(
                psaNames, 
                &i, 
                &PropName);

                wcout << PropName<< endl;
                SysFreeString(PropName);
        }

    SafeArrayDestroy(psaNames);
    ULONG uReturn = 0;


    pSvc->Release();
    pLoc->Release();

    CoUninitialize();
    cout << "press enter to exit" << endl;
    cin.get();
    return 0;   // Program successfully completed.
}

关于c++ - 在 C++ 中列出 WMI 类的所有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18992717/

相关文章:

c# - 无法弄清楚如何检查生物识别是否存在

powershell - 如何从已安装的更新窗口获取所有详细信息

c++ - 如何在 C++/OpenCv 中的一个数组中存储多个 RGB 图像

c++ - 在 C++11 中同步整个类

c++ - 在 C++11 中使用可变大小声明静态数组

python - 如何在 python 中访问 wmi?

windows - 安排一个任务来监视某个进程的启动

c# - Win32_MountPoint 和 Win32_Volume 在 Windows XP 上的可用性?

c++ - CreateProcess - lpApplicationName 与 lpCommandLine

.net - 通过 WMI 使用 VBS 检测 .Net Framework 3.5 或更高版本