WIKI
+ -

WMI大全

2021-07-01 439 5

在wmi中,有一个函数CoInitializeSecurity,这个函数要慎重使用,因为一个进程中只能调用一次,以后再次调用会返回失败,即使调用CoUninitialize了之后。所以特别是在dll中,如果频繁地动态加载,则万万不能使用。

        //          bstr_t("SELECT * FROM Win32_SoundDevice"),                          // 音频设备  
        //          bstr_t("SELECT * FROM Win32_Product"),    
        //          bstr_t("SELECT * FROM Win32_Registry"),                             // 注册表  
        //          bstr_t("SELECT * FROM Win32_OnBoardDevice"),                        // 主板上的嵌入设备  
        //          bstr_t("SELECT * FROM Win32_MotherboardDevice"),                    // 母板  
        //          bstr_t("SELECT * FROM Win32_BaseBoard"),                            // 主板  
        //          bstr_t("SELECT * FROM Win32_DMAChannel"),                           // DMA通道  
        //          bstr_t("SELECT * FROM Win32_Bus"),                                  // 总线  
        //          bstr_t("SELECT * FROM Win32_BIOS"),                                 // BIOS系统  
        //          bstr_t("SELECT * FROM Win32_SystemBIOS"),  
        //          bstr_t("SELECT * FROM Win32_Processor"),                            // 处理器  
        //          bstr_t("SELECT * FROM Win32_SystemProcesses"),                      //   
        //          bstr_t("SELECT * FROM Win32_Process"),                              // 系统进程  
        //          bstr_t("SELECT * FROM Win32_Thread"),                               // 系统线程  
        //          bstr_t("SELECT * FROM Win32_AssociatedProcessorMemory"),            // CACHE  
        //          bstr_t("SELECT * FROM Win32_CacheMemory"),                          // 二级缓存内存  
        //          bstr_t("SELECT * FROM Win32_PhysicalMedia"),                        // 物理媒体信息 (硬盘)  
        //          bstr_t("SELECT * FROM Win32_LogicalDisk "),                         // 逻辑驱动器  
        //          bstr_t("SELECT * FROM Win32_DiskDrive"),                            // 磁盘驱动器  
        //          bstr_t("SELECT * FROM Win32_MemoryDevice"),                         // 内存设备  
        //          bstr_t("SELECT * FROM Win32_PhysicalMemoryArray"),                  // 物理内存数组  
        //          bstr_t("SELECT * FROM Win32_PhysicalMemoryLocation"),               // 物理内存位置  
        //          bstr_t("SELECT * FROM CIM_NumericSensor"),                          // 数字传感器  
        //          bstr_t("SELECT * FROM Win32_VoltageProbe"),                         // 数字传感器   
        //          bstr_t("SELECT * FROM Win32_TemperatureProbe"),                     // 温度传感器  
        //          bstr_t("SELECT * FROM Win32_CurrentProbe"),       
        //          bstr_t("SELECT * FROM Win32_OperatingSystem"),                      // 操作系统      
        //          bstr_t("SELECT * FROM Win32_UserAccount"),                          // 用户账号  
        //          bstr_t("SELECT * FROM Win32_SerialPort"),                           // 串行接口  
        //          bstr_t("SELECT * FROM Win32_ParallelPort"),                         // 并行接口  
        //          bstr_t("SELECT * FROM Win32_SCSIController"),                       // 小型计算机系统接口  
        //          bstr_t("SELECT * FROM Win32_PortResource"),                         // I/O 端口  
        //          bstr_t("SELECT * FROM Win32_PNPDevice"),                            // 即插即用设备  
        //          bstr_t("SELECT * FROM Win32_NetworkAdapter"),                       // 网络适配器  
        //          bstr_t("SELECT * FROM Win32_NetworkAdapterConfiguration"),  
        //          bstr_t("SELECT * FROM Win32_NetworkAdapterSetting"),  
        //          bstr_t("SELECT * FROM Win32_AssociatedBattery"),  
        //          bstr_t("SELECT * FROM Win32_Battery"),                              // 内部电池  
        //          bstr_t("SELECT * FROM Win32_PortableBattery"),  
        //          bstr_t("SELECT * FROM Win32_PowerManagementEvent"),  
        //          bstr_t("SELECT * FROM Win32_UninterruptiblePowerSupply"),  
        //          bstr_t("SELECT * FROM Win32_DriverForDevice"),  
        //          bstr_t("SELECT * FROM Win32_Printer"),                              // 打印机  
        //          bstr_t("SELECT * FROM Win32_TCPIPPrinterPort"),  
        //          bstr_t("SELECT * FROM Win32_POTSModem"),  
                 bstr_t("SELECT * FROM Win32_DesktopMonitor"),                       // 显示器  
    //    bstr_t("SELECT * FROM Win32_VideoController"),                      // 显卡  
        //          bstr_t("SELECT * FROM Win32_CDROMDrive"),  
       //       bstr_t("SELECT * FROM Win32_Keyboard"),                             // 键盘  
        //          bstr_t("SELECT * FROM Win32_AutochkSetting"),  
    //                  bstr_t("SELECT * FROM Win32_PointingDevice"),                       // 点击设备:鼠标、触摸板  
        //          bstr_t("SELECT * FROM Win32_Fan"),                                  // 风扇  
        //          bstr_t("SELECT * FROM Win32_WMISetting"),  
        //          bstr_t("SELECT * FROM Win32_TimeZone"),       
        //          bstr_t("SELECT * FROM Win32_Environment"),                          // 环境路径  
        //          bstr_t("SELECT * FROM Win32_QuotaSetting"),  
        //          bstr_t("SELECT * FROM Win32_NetworkProtocol"),                      // 己安装的网络协议

#include "stdafx.h"

//
//int _tmain(int argc, _TCHAR* argv[])
//{
//    return 0;
//}
//
#define _WIN32_DCOM  
//#define _WIN32_WINNT   0X0500   

#include <iostream>  
#include <comdef.h>  
#include <conio.h>  
#include "wbemidl.h"  
//#include "wmi.h"  

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

using namespace std;

static IWbemLocator *pLoc = NULL;
static IWbemServices *pSvc = NULL;
static IEnumWbemClassObject* pEnumerator = NULL;
static IWbemClassObject* pclsObj = NULL;

// http://www.pudn.com/downloads44/sourcecode/windows/system/detail148813.html  
// http://www.qingfengju.com/article.asp?id=62  
// http://blog.csdn.net/shiwei0124/article/details/4985790  
// http://topic.csdn.net/u/20100623/15/ad7c592d-2548-4aae-8ca2-18065baf6566.html  
// http://msdn.microsoft.com/en-us/library/aa389273(VS.85).aspx  
int DisplayALL()
{
    HRESULT hres;

//    cout << "Step 1: --------------------------------------------------" << endl;
//    cout << "Initialize COM. ------------------------------------------" << endl;

    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.      
    }

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

    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.      
    }

    //cout << "Step 3: ---------------------------------------------------" << endl;
    //cout << "Obtain the initial locator to WMI -------------------------" << endl;

    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.      
    }

    //cout << "Step 4: -----------------------------------------------------" << endl;
    //cout << "Connect to WMI through the IWbemLocator::ConnectServer method" << endl;

    IWbemServices *pSvc = NULL;

    //cout << "Connect to the root\cimv2 namespace with" << endl;
    //cout << "the current user and obtain pointer pSvc" << endl;
    //cout << "to make IWbemServices calls." << endl;

    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 (e.g. 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;


    //cout << "Step 5: --------------------------------------------------" << endl;
    //cout << "Set security levels on the proxy -------------------------" << endl;

    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.      
    }

    //cout << "Step 6: --------------------------------------------------" << endl;
    //cout << "Use the IWbemServices pointer to make requests of WMI ----" << endl;

    // For example, get the name of the operating system      
    IEnumWbemClassObject* pEnumerator = NULL;

    hres = pSvc->ExecQuery(
        bstr_t("WQL"),
        //          bstr_t("SELECT * FROM Win32_SoundDevice"),                          // 音频设备  
        //          bstr_t("SELECT * FROM Win32_Product"),    
        //          bstr_t("SELECT * FROM Win32_Registry"),                             // 注册表  
        //          bstr_t("SELECT * FROM Win32_OnBoardDevice"),                        // 主板上的嵌入设备  
        //          bstr_t("SELECT * FROM Win32_MotherboardDevice"),                    // 母板  
        //          bstr_t("SELECT * FROM Win32_BaseBoard"),                            // 主板  
        //          bstr_t("SELECT * FROM Win32_DMAChannel"),                           // DMA通道  
        //          bstr_t("SELECT * FROM Win32_Bus"),                                  // 总线  
        //          bstr_t("SELECT * FROM Win32_BIOS"),                                 // BIOS系统  
        //          bstr_t("SELECT * FROM Win32_SystemBIOS"),  
        //          bstr_t("SELECT * FROM Win32_Processor"),                            // 处理器  
        //          bstr_t("SELECT * FROM Win32_SystemProcesses"),                      //   
        //          bstr_t("SELECT * FROM Win32_Process"),                              // 系统进程  
        //          bstr_t("SELECT * FROM Win32_Thread"),                               // 系统线程  
        //          bstr_t("SELECT * FROM Win32_AssociatedProcessorMemory"),            // CACHE  
        //          bstr_t("SELECT * FROM Win32_CacheMemory"),                          // 二级缓存内存  
        //          bstr_t("SELECT * FROM Win32_PhysicalMedia"),                        // 物理媒体信息 (硬盘)  
        //          bstr_t("SELECT * FROM Win32_LogicalDisk "),                         // 逻辑驱动器  
        //          bstr_t("SELECT * FROM Win32_DiskDrive"),                            // 磁盘驱动器  
        //          bstr_t("SELECT * FROM Win32_MemoryDevice"),                         // 内存设备  
        //          bstr_t("SELECT * FROM Win32_PhysicalMemoryArray"),                  // 物理内存数组  
        //          bstr_t("SELECT * FROM Win32_PhysicalMemoryLocation"),               // 物理内存位置  
        //          bstr_t("SELECT * FROM CIM_NumericSensor"),                          // 数字传感器  
        //          bstr_t("SELECT * FROM Win32_VoltageProbe"),                         // 数字传感器   
        //          bstr_t("SELECT * FROM Win32_TemperatureProbe"),                     // 温度传感器  
        //          bstr_t("SELECT * FROM Win32_CurrentProbe"),       
        //          bstr_t("SELECT * FROM Win32_OperatingSystem"),                      // 操作系统      
        //          bstr_t("SELECT * FROM Win32_UserAccount"),                          // 用户账号  
        //          bstr_t("SELECT * FROM Win32_SerialPort"),                           // 串行接口  
        //          bstr_t("SELECT * FROM Win32_ParallelPort"),                         // 并行接口  
        //          bstr_t("SELECT * FROM Win32_SCSIController"),                       // 小型计算机系统接口  
        //          bstr_t("SELECT * FROM Win32_PortResource"),                         // I/O 端口  
        //          bstr_t("SELECT * FROM Win32_PNPDevice"),                            // 即插即用设备  
        //          bstr_t("SELECT * FROM Win32_NetworkAdapter"),                       // 网络适配器  
        //          bstr_t("SELECT * FROM Win32_NetworkAdapterConfiguration"),  
        //          bstr_t("SELECT * FROM Win32_NetworkAdapterSetting"),  
        //          bstr_t("SELECT * FROM Win32_AssociatedBattery"),  
        //          bstr_t("SELECT * FROM Win32_Battery"),                              // 内部电池  
        //          bstr_t("SELECT * FROM Win32_PortableBattery"),  
        //          bstr_t("SELECT * FROM Win32_PowerManagementEvent"),  
        //          bstr_t("SELECT * FROM Win32_UninterruptiblePowerSupply"),  
        //          bstr_t("SELECT * FROM Win32_DriverForDevice"),  
        //          bstr_t("SELECT * FROM Win32_Printer"),                              // 打印机  
        //          bstr_t("SELECT * FROM Win32_TCPIPPrinterPort"),  
        //          bstr_t("SELECT * FROM Win32_POTSModem"),  
                 bstr_t("SELECT * FROM Win32_DesktopMonitor"),                       // 显示器  
    //    bstr_t("SELECT * FROM Win32_VideoController"),                      // 显卡  
        //          bstr_t("SELECT * FROM Win32_CDROMDrive"),  
       //       bstr_t("SELECT * FROM Win32_Keyboard"),                             // 键盘  
        //          bstr_t("SELECT * FROM Win32_AutochkSetting"),  
    //                  bstr_t("SELECT * FROM Win32_PointingDevice"),                       // 点击设备:鼠标、触摸板  
        //          bstr_t("SELECT * FROM Win32_Fan"),                                  // 风扇  
        //          bstr_t("SELECT * FROM Win32_WMISetting"),  
        //          bstr_t("SELECT * FROM Win32_TimeZone"),       
        //          bstr_t("SELECT * FROM Win32_Environment"),                          // 环境路径  
        //          bstr_t("SELECT * FROM Win32_QuotaSetting"),  
        //          bstr_t("SELECT * FROM Win32_NetworkProtocol"),                      // 己安装的网络协议  
        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.      
    }

    cout << "Step 7: -------------------------------------------------" << endl;
    cout << "Get the data from the query in step 6 -------------------" << endl;

    IWbemClassObject* pclsObj = NULL;
    ULONG uReturn = 0;

    while (pEnumerator)
    {
        cout << "pEnumerator==>" << pEnumerator << endl;
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
        cout << "pclsObj==>" << pclsObj << " NO. object found" << endl;
        if ((0 == uReturn) || (0 == pclsObj))
        {
            break;
        }

        VARIANT vtProp;

        // Get the value of the Name property      
        VariantInit(&vtProp);
        vtProp.bstrVal = 0;// NULL  
        // hr = pclsObj->Get(L"MACAddress", 0, &vtProp, 0, 0);     
        hr = pclsObj->GetObjectText(0, &vtProp.bstrVal);
        if (vtProp.bstrVal != 0)
        {
            wcout.imbue(locale(locale(""), "", LC_CTYPE));
            wcout << L"硬件信息: " << vtProp.bstrVal << endl;
        }
        else
        {
            cout << "硬件信息 " << "vtProp.bstrVal == NULL" << endl;
        }
        VariantClear(&vtProp);
    }

    cout << "Cleanup" << endl;
    // ========      

    pSvc->Release();
    pLoc->Release();
    if (pEnumerator)
    {
        pEnumerator->Release();
    }
    if (pclsObj)
    {
        pclsObj->Release();
    }
    CoUninitialize();

    cout << "end of program" << endl;

    return 0;   // Program successfully completed.      

}










int Step1()
{
    HRESULT hres;

    //   cout << "Step 1: --------------------------------------------------" << endl;     
    //   cout << "Initialize COM. ------------------------------------------" << endl;     

    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.      
    }
    return 0;
}
int Step2()
{
    //  cout << "Step 2: --------------------------------------------------" << endl;     
    //    cout << "Set general COM security levels --------------------------" << endl;     
    //    cout << "Note: If you are using Windows 2000, you need to specify -" << endl;     
    //    cout << "the default authentication credentials for a user by using" << endl;     
    //    cout << "a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----" << endl;     
    //   cout << "parameter of CoInitializeSecurity ------------------------" << endl;     
    HRESULT hres;

    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.      
    }
    return 0;
}
int Step3()
{
    //  cout << "Step 3: ---------------------------------------------------" << endl;     
    //   cout << "Obtain the initial locator to WMI -------------------------" << endl;     

    //   IWbemLocator *pLoc = NULL;     
    HRESULT hres;

    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.      
    }
    return 0;
}
int Step4()
{
    //  cout << "Step 4: -----------------------------------------------------" << endl;     
    //  cout << "Connect to WMI through the IWbemLocator::ConnectServer method" << endl;     

    //   IWbemServices *pSvc = NULL;     

    //   cout << "Connect to the root\cimv2 namespace with" << endl;     
    //   cout << "the current user and obtain pointer pSvc" << endl;     
    //  cout << "to make IWbemServices calls." << endl;     
    HRESULT hres;

    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 (e.g. 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;     
    return 0;
}
int Step5()
{
    //  cout << "Step 5: --------------------------------------------------" << endl;     
    //  cout << "Set security levels on the proxy -------------------------" << endl;     
    HRESULT hres;

    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.      
    }
    return 0;
}
int Step6(char *CmdString)
{
    //  cout << "Step 6: --------------------------------------------------" << endl;     
    //  cout << "Use the IWbemServices pointer to make requests of WMI ----" << endl;     

    // For example, get the name of the operating system      
    //  IEnumWbemClassObject* pEnumerator = NULL;     
    HRESULT hres;

    hres = pSvc->ExecQuery(
        bstr_t("WQL"),
        bstr_t(CmdString),
        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.      
    }
    return 0;
}
int Step7(unsigned short *Item1, unsigned short *content1,
    unsigned short *Item2, unsigned short *content2)
{
    //  cout << "Step 7: -------------------------------------------------" << endl;     
    //   cout << "Get the data from the query in step 6 -------------------" << endl;     

    //  IWbemClassObject* pclsObj = NULL;     
    ULONG uReturn = 0;

    while (pEnumerator)
    {
        //        cout << "pEnumerator==>" << pEnumerator << endl;     
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
        //        cout << "pclsObj==>" << pclsObj << " NO. object found" << endl;     
        if ((0 == uReturn) || (0 == pclsObj))
        {
            break;
        }

        VARIANT vtProp;

        // Get the value of the Name property      
        // 名称  
        VariantInit(&vtProp);
        vtProp.bstrVal = 0;// NULL  
        hr = pclsObj->Get(Item1, 0, &vtProp, 0, 0);

        if (hr != WBEM_S_NO_ERROR || vtProp.bstrVal == 0 || wcscmp(vtProp.bstrVal, content1) != 0)
        {
            VariantClear(&vtProp);
            continue;
        }
        VariantClear(&vtProp);

        VariantInit(&vtProp);
        vtProp.bstrVal = 0;// NULL  
        hr = pclsObj->Get(Item2, 0, &vtProp, 0, 0);
        // hr = pclsObj->GetObjectText(0, &vtProp.bstrVal);  
        if (vtProp.bstrVal != 0)
        {
            //          wcout.imbue(locale(locale(""),"",LC_CTYPE));  
            //          wcout << L"硬件信息: " << vtProp.bstrVal << endl;     
            wcscpy(content2, vtProp.bstrVal);
        }
        else
        {
            cout << "硬件信息 " << "vtProp.bstrVal == NULL" << endl;
        }
        VariantClear(&vtProp);
    }
    return 0;
}
int Step7(unsigned short *Item, unsigned short *content)
{
    //  cout << "Step 7: -------------------------------------------------" << endl;     
    //   cout << "Get the data from the query in step 6 -------------------" << endl;     

    //  IWbemClassObject* pclsObj = NULL;     
    ULONG uReturn = 0;

    while (pEnumerator)
    {
        //      cout << "pEnumerator==>" << pEnumerator << endl;     
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
        //        cout << "pclsObj==>" << pclsObj << " NO. object found" << endl;     
        if ((0 == uReturn) || (0 == pclsObj))
        {
            break;
        }

        VARIANT vtProp;

        // Get the value of the Name property      
        // 名称  
        VariantInit(&vtProp);
        vtProp.bstrVal = 0;// NULL  
        hr = pclsObj->Get(Item, 0, &vtProp, 0, 0);
        // hr = pclsObj->GetObjectText(0, &vtProp.bstrVal);  
        if (vtProp.bstrVal != 0)
        {
            //          wcout.imbue(locale(locale(""),"",LC_CTYPE));  
            //          wcout << L"硬件信息: " << vtProp.bstrVal << endl;     
            wcscpy(content, vtProp.bstrVal);
        }
        else
        {
            cout << "硬件信息 " << "vtProp.bstrVal == NULL" << endl;
        }
        VariantClear(&vtProp);
    }
    return 0;
}
int Step8()
{
    //  cout << "Cleanup" << endl;     
    // ========      
    if (pEnumerator)
    {
        pEnumerator->Release();
    }
    if (pclsObj)
    {
        pclsObj->Release();
    }
    CoUninitialize();

    //  cout << "end of program" << endl;  
    return 0;
}

int main(int argc, char **argv)
{
    while (1)
    {
        DisplayALL();
        Sleep(3000);

    }
      _getch();
      return 0;  

    int p = Step1() ? 1 : Step2() ? 1 : Step3() ? 1 : Step4() ? 1 : Step5();
    // 下面可能错误,编译器优化,先算其中某一步为0,不再算其它  
    //  int p = Step1() & Step2() & Step3() & Step4() & Step5();   
    if (p) goto ExitProgram;

    // 网卡  
    p = Step6("SELECT * FROM Win32_NetworkAdapter");
    if (p) goto ExitProgram;
    {
        unsigned short MACAddress[MAX_PATH] = { 0 };
        p = Step7(L"NetConnectionID", L"本地连接", L"MACAddress", MACAddress);
        if (MACAddress[0] != 0)
        {
            wcout.imbue(locale(locale(""), "", LC_CTYPE));
            wcout << L"网卡地址: " << MACAddress << endl;
        }
    }

    // CPU  
    p = Step6("SELECT * FROM Win32_Processor");
    if (p) goto ExitProgram;
    {
        unsigned short ProcessorId[MAX_PATH] = { 0 };
        p = Step7(L"DeviceID", L"CPU0", L"ProcessorId", ProcessorId);
        if (ProcessorId[0] != 0)
        {
            wcout.imbue(locale(locale(""), "", LC_CTYPE));
            wcout << L"处理器ID: " << ProcessorId << endl;
        }
    }

    // 硬盘  
    p = Step6("SELECT * FROM Win32_PhysicalMedia");
    if (p) goto ExitProgram;
    {
        unsigned short SerialNumber[MAX_PATH] = { 0 };
        p = Step7(L"SerialNumber", SerialNumber);
        if (SerialNumber[0] != 0)
        {
            wcout.imbue(locale(locale(""), "", LC_CTYPE));
            wcout << L"硬盘序列号: " << SerialNumber << endl;
        }
    }

    // 显卡  
    p = Step6("SELECT * FROM Win32_VideoController");
    if (p) goto ExitProgram;
    {
        unsigned short PNPDeviceID[MAX_PATH] = { 0 };
        p = Step7(L"PNPDeviceID", PNPDeviceID);
        if (PNPDeviceID[0] != 0)
        {
            wcout.imbue(locale(locale(""), "", LC_CTYPE));
            wcout << L"显卡序列号: " << PNPDeviceID << endl;
        }
    }

    // 操作系统  
    p = Step6("SELECT * FROM Win32_OperatingSystem");
    if (p) goto ExitProgram;
    {
        unsigned short SerialNumber[MAX_PATH] = { 0 };
        p = Step7(L"SerialNumber", SerialNumber);
        if (SerialNumber[0] != 0)
        {
            wcout.imbue(locale(locale(""), "", LC_CTYPE));
            wcout << L"操作系统ID: " << SerialNumber << endl;
        }
    }

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

ExitProgram:
    Step8();
    _getch();
    return 0;
}

0 篇笔记 写笔记

WMI大全
在wmi中,有一个函数CoInitializeSecurity,这个函数要慎重使用,因为一个进程中只能调用一次,以后再次调用会返回失败,即使调用CoUninitialize了之后。所以特别是在dll中,如果频繁地动态加载,则万万不能使用。 // bstr_t(&qu......
WDF WMI请求(仅限内核模式驱动程序)
WMI(WindowsManagementInstrumentation)请求触发驱动程序为当前所有WMI事件所注册的回调。在WMI回调中,驱动程序可能会调用设备对象的WIM方法来创建和操纵WMI实例,以WMI提供程序的形式改变它的状态。在WIM回调返回之后,框架代替驱动程序根据具体情况完成或转发请......
作者信息
我爱内核
Windows驱动开发,网站开发
好好学习,天天向上。
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!