experiment : 使用WMI取本地计算机信息

最近的任务中有个分支任务, 要求将客户本地计算机Windows操作系统中的软硬件信息记录下来, 给研发看, 辅助调试用.

看到WMI能做这事, codeproject上资料还没有csdn上多~

有人反映WMI比较慢, 这对于我的任务倒没啥影响~, 在线程中执行, 界面上给个等待进度提示就可以.

参考: 

Computer System Hardware Classes (Windows)

http://msdn.microsoft.com/en-us/library/windows/desktop/aa389273%28v=vs.85%29.aspx

http://www.codeproject.com/Articles/5853/Getting-Information-from-WMI-in-Visual-C

http://www.codeproject.com/Articles/18135/Getting-the-Network-Adaptor-MAC-Address-with-WMI

http://www.codeproject.com/Articles/37707/Process-Create-Notification-using-EnumProcess

http://www.codeproject.com/Articles/10539/Making-WMI-Queries-In-C

整理了一个WMI Data 类, 以后复用. CWmiDataHelper用来连续执行SQL查询WMI中的内容. 还挺好用的~

只需要执行一个方法:

WmiData.ExecComQuery

取数据还是弄得挺丑, 得再想想. 实际取数据的时候, 要取多个数据.  e.g. 取计算机逻辑分区信息 分区No.1(分区盘符, 分区大小, 分区格式, 所在物理硬盘序列号)

以后有时间, 再整理一个取WMI逻辑数据的类, 此类调用CWmiDataHelper来工作.

此数据主要给研发看, 比如我自己, 看到测试程序的结果, 我也能明白.

剩下的就是工作量, 整的好看些. 如果有时间的话, 要精益求精的去提高用户体验, 假设用户就是我一个人, 也是如此.

计算机硬件检测程序, 一定也调用了WMI功能. 只是人家做的非常精细.

测试程序:

// srcWmiData.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Helper/WmiData/WmiDataHelper.h"

int _tmain(int argc, _TCHAR* argv[])
{
    int                                 iIndex          = 0;
    INT64                               int64Size       = 0;
    INT64                               int64SizeCur    = 0;
    size_t                              nSize           = 0;
    size_t                              nSizeCur        = 0;
    std::wstring                        strTmpW;
    std::vector<std::wstring>           vecResult;
    std::vector<std::wstring>::iterator it;
    CWmiDataHelper                      WmiData;

    /// 安装程序列表 Win32_Product
    if(WmiData.ExecComQuery(L"select * from  Win32_Product", L"Name", vecResult))
    {
        int64Size = 0;
        _tprintf(L"Win32_Product Name = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
            _tprintf(L"%s\r\n", (*it).c_str());
    }

    /// Win32_NetworkAdapterConfiguration Ip地址 IPAddress
    if(WmiData.ExecComQuery(L"select * from  Win32_NetworkAdapterConfiguration", L"IPAddress", vecResult))
    {
        int64Size = 0;
        _tprintf(L"Win32_NetworkAdapterConfiguration IPXAddress = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
            _tprintf(L"%s\r\n", (*it).c_str());
    }

    /// 网卡名称 Win32_NetworkAdapter Name
    if(WmiData.ExecComQuery(L"select * from  Win32_NetworkAdapter", L"Name", vecResult))
    {
        int64Size = 0;
        _tprintf(L"Win32_NetworkAdapter Name = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
            _tprintf(L"%s\r\n", (*it).c_str());
    }

    /// 网卡Mac地址 Win32_NetworkAdapter MACAddress
    if(WmiData.ExecComQuery(L"select * from  Win32_NetworkAdapter", L"MACAddress", vecResult))
    {
        int64Size = 0;
        _tprintf(L"Win32_NetworkAdapter MACAddress = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
            _tprintf(L"%s\r\n", (*it).c_str());
    }

    /// 磁盘分区 Win32_DiskPartition
    if(WmiData.ExecComQuery(L"select * from  Win32_DiskPartition", L"DeviceID", vecResult))
    {
        int64Size = 0;
        _tprintf(L"Win32_DiskDrive Capacity = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
            _tprintf(L"%s\r\n", (*it).c_str());
    }

    // 分区类型
    if(WmiData.ExecComQuery(L"select * from  Win32_DiskPartition", L"Type", vecResult))
    {
        int64Size = 0;
        _tprintf(L"Win32_DiskDrive Capacity = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
            _tprintf(L"%s\r\n", (*it).c_str());
    }

    if(WmiData.ExecComQuery(L"select * from  Win32_DiskPartition", L"Size", vecResult))
    {
        int64Size = 0;
        _tprintf(L"Win32_DiskDrive Capacity = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
        {
            strTmpW = (*it).c_str();
            int64SizeCur = _ttoi64(strTmpW.c_str());
            int64Size = int64SizeCur / 1024 / 1024 / 1024;
            _tprintf(L"%dGB\r\n", int64Size);
        }
    }

    /// 硬盘 Win32_DiskDrive
    if(WmiData.ExecComQuery(L"select * from  Win32_DiskDrive", L"Capabilities", vecResult))
    {
        int64Size = 0;
        _tprintf(L"Win32_DiskDrive Capacity = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
        {
            strTmpW = (*it).c_str();
            int64SizeCur = _ttoi64(strTmpW.c_str());
            int64Size += int64SizeCur;
        }

        _tprintf(L"%dGB\r\n", int64Size);
    }

    /// 内存 Win32_PhysicalMemory, 数据是int64!
    if(WmiData.ExecComQuery(L"select * from  Win32_PhysicalMemory", L"Capacity", vecResult))
    {
        int64Size = 0;
        _tprintf(L"Win32_PhysicalMemory Capacity = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
        {
            strTmpW = (*it).c_str();
            int64SizeCur = _ttoi64(strTmpW.c_str());
            int64Size += int64SizeCur;
        }

        _tprintf(L"%dMB\r\n", int64Size / 1024 / 1024);
    }

    /// 可用内存 Win32_PerfFormattedData_PerfOS_Memory
    if(WmiData.ExecComQuery(L"select * from Win32_PerfFormattedData_PerfOS_Memory", L"AvailableMBytes", vecResult))
    {
        nSize = 0;
        _tprintf(L"Win32_PerfFormattedData_PerfOS_Memory Capacity = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
        {
            nSizeCur = _ttol((*it).c_str());
            nSize += nSizeCur;
        }

        _tprintf(L"%dMB\r\n", nSize);
    }

    /// 得处理器信息
    /// CPU 厂家 Manufacturer
    if(WmiData.ExecComQuery(L"select * from win32_Processor", L"Manufacturer", vecResult))
    {
        _tprintf(L"cpu Manufacturer = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
        {
            _tprintf(L"%s\r\n", (*it).c_str());
        }
    }
    
    /// CPU Name
    if(WmiData.ExecComQuery(L"select * from win32_Processor", L"Name", vecResult))
    {
        _tprintf(L"Name = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
        {
            _tprintf(L"%s\r\n", (*it).c_str());
        }
    }

    /// CPU占用率
    if(WmiData.ExecComQuery(L"select * from win32_Processor", L"LoadPercentage", vecResult))
    {
        _tprintf(L"cpu LoadPercentage = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
        {
            _tprintf(L"%s\r\n", (*it).c_str());
        }
    }

    /// 得主板信息
    /// 生产厂家 Manufacturer
    if(WmiData.ExecComQuery(L"select * from Win32_BaseBoard", L"Manufacturer", vecResult))
    {
        _tprintf(L"MainBoard = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
        {
            _tprintf(L"%s\r\n", (*it).c_str());
        }
    }

    /// 主板型号
    if(WmiData.ExecComQuery(L"select * from Win32_BaseBoard", L"Product", vecResult))
    {
        _tprintf(L"MainBoard = :\r\n");
        for(it = vecResult.begin(); it != vecResult.end(); it++)
        {
            _tprintf(L"%s\r\n", (*it).c_str());
        }
    }

    getchar();
    return 0;
}

/** run result
Windows Mobile 5.0 SDK R2 for Pocket PC
Catalyst Control Center - Branding
wpdtools_x64fre
Microsoft Visual Studio 2010 IntelliTrace Collection (x64)
Intel?Trusted Connect Service Client
smartcardsamples
Device Simulation Framework 1.0.1
eventsample
bussamples
CCC Help Hungarian
Microsoft Visual Studio 2005 64bit Prerequisites (x64) - ENU
Visual Studio .NET Enterprise Architect 2003 - CHS
biometrictools_x86fre
swtuner
oacr_x86fre
CCC Help Thai
wpdsamples
avstreamsamples
HydraVision
Microsoft Visual Studio 2008 Professional Edition - ENU
Catalyst Control Center InstallProxy
Windows Management Instrumentation (WMI) SDK (3790.0)
usbsamples
Visual Studio.NET Baseline - CHS
pfd_ia64fre
sensorsamples
buildtools_x86fre
Microsoft Document Explorer 2005
wdtfbinaries_ia64fre
setuptools_x86fre
CCC Help Chinese Standard
wnetlibs_x86fre
setupsamples
drvtools_x86fre
CCC Help Finnish
Microsoft Team Foundation Server 2010 Object Model - ENU
ccc-utility64
Debugging Tools for Windows (3790.0)
bluetoothsamples
O&O Defrag Professional
Microsoft .NET Framework 2.0 SDK (x64) - ENU
avstreamtools_ia64fre
Snagit 10.0.1
Microsoft Visual C++ 2008 Redistributable - x86 9.0.30729
avstreamtools_x64fre
wpdtools_x86fre
AnkhSVN 2.3.10838.1211
Microsoft FrontPage Client - CHS
tools_ia64fre
Adobe Reader 9.5.3 - Chinese Simplified
fireflysample
MSDN Library for Visual Studio 2005
Microsoft Visual Studio 2008 Remote Debugger - ENU
vistalibs_ia64fre
CCC Help Spanish
libs_x64fre
portiosample
Microsoft Visual C++ 2008 Redistributable - x86 9.0.21022
CCC Help Greek
modemtools
toastermetadatapackagesample
Tablet PC SDK (3790.0)
networksamples
wdtfbinaries_x86fre
CCC Help German
wsdtool_ia64fre
SlickEdit 2012 x64 (17.0.1)
Visual Studio .NET Prerequisites - English
Microsoft Visual Studio 2010 Office Developer Tools (x64)
dfx_x64fre
tools-winPre2k
drvtools_ia64fre
tools-netware
drvtools_x64fre
pcidrvsample
Microsoft Visual Studio 2005 SDK February 2007
MSDN Library for Visual Studio .NET 2003 - ????
wpdtools_ia64fre
Microsoft Visual Studio 2008 Performance Collection Tools - ENU
printtools_x64fre
infsample_x64fre
CCC Help Chinese Traditional
generaltools_x86fre
installhelp
Microsoft Visual C++ 2005 Redistributable (x64)
Microsoft Windows Driver Kit Documentation 7600.091201
wnetlibs_x64fre
ifssamples
Microsoft Data Access Components (MDAC) SDK (Ver 2.7) (3790.0)
Renesas Electronics USB 3.0 Host Controller Driver
offreg_ia64fre
CCC Help Swedish
Microsoft Visual C++ 2005 Redistributable (x64)
imagingtools_ia64fre
wsdtool_x64fre
CCC Help Danish
AMD Media Foundation Decoders
ATI AVIVO64 Codecs
generaltools_x64fre
setuptools_x64fre
cancelsample
headers
DSF-KitSetup
wxplibs_x86fre
printtools_ia64fre
networklibraries_ia64fre
hidsampleinput
debugfiles_win7
setuptools_ia64fre
tools-linux
AMD Catalyst Install Manager
tracingtool_x86fre
libs_ia64fre
VMware Workstation
Microsoft Help Viewer 1.0
VC Runtimes MSI
toolindex
bluetoothtools_x64fre
pnptools_x64fre
sideshowsamples
Microsoft Windows SDK for Visual Studio 2008 SP1 Win32 Tools
hidsamples
buildtools_ia64fre
hid_inputsamples
Microsoft Visual Studio 2010 Ultimate - ENU
imagingtools_x86fre
Microsoft Visual C++ 2005 Redistributable
sdv
wdftools_x86fre
Catalyst Control Center Graphics Previews Common
evntdrvsample
biometrictools_x64fre
chkinftool_x86fre
CCC Help Norwegian
tracingtool_x64fre
umdfsamples
generaltools_ia64fre
bluetoothtools_ia64fre
CCC Help Turkish
CCC Help Russian
pfd_x86fre
tools-solaris
Windows Media Services SDK (3790.0)
imagingtools_x64fre
Catalyst Control Center Localization All
Microsoft Document Explorer 2008
Microsoft Visual C++ 2010  x64 Redistributable - 10.0.30319
Microsoft Visual C++ 2005 Redistributable
tools-windows
networklibraries_x64fre
pnpportssample
printtools_x86fre
pnptools_ia64fre
Microsoft Windows SDK for Visual Studio 2008 SDK Reference Assemblies and Intell
iSense
dsfsamples
Windows Mobile 5.0 SDK R2 for Smartphone
wmisamples
Internet Information Server (IIS) SDK (Version 5.1) (3790.0)
vistalibs_x64fre
wcoinstallers
readme
Microsoft Visual C++ 2008 Redistributable - x86 9.0.30729.4148
Microsoft Visual C++ 2010  x86 Redistributable - 10.0.30319
Internet Development SDK (Version 6.0) (3790.0)
Microsoft Visual C++ 2008 Redistributable - x86 9.0.30729.17
displaysamples
AMD APP SDK Runtime
Visual C++ 2008 x86 Runtime - (v9.0.30729)
pfd_x64fre
ASUS VGA Driver
dfx_x86fre
storagesamples
tools-freebsd
Microsoft Visual Studio 2010 Performance Collection Tools - ENU
Microsoft Visual Studio Team System 2008 Team Suite - ENU
tools_x86fre
Microsoft .NET Framework 1.1
wnetlibs_ia64fre
CCC Help Italian
Windows Installer SDK (Version 2.0) (3790.0)
Microsoft Device Emulator (64 bit) version 3.0 - ENU
offreg_x64fre
powermanagement_ia64fre
Microsoft Visual C++ 2008 ATL Update kb973924 - x86 9.0.30729.4148
vistalibs_x86fre
Microsoft Visual Studio Macro Tools
bluetoothtools_x86fre
Core SDK (Windows Server 2003) (3790.0)
MSDN Library for Visual Studio 2008 SP1 - ENU
Debugging Tools for Windows (x64)
buildsamples
VisualSVN Server 2.5.4
powermanagement_x64fre
ioctlsample
CCC Help English
Catalyst Control Center InstallProxy
TortoiseSVN 1.7.6.22632 (64 bit)
wdtfbinaries_x64fre
tools_x64fre
Utility
RAMDisk
WSC Real 09
CCC Help Czech
tracingtool_ia64fre
irsamples
audiosamples
CCC Help Korean
wsdtool_x86fre
Microsoft Windows SDK for Visual Studio 2008 Headers and Libraries
infsample_x86fre
offreg_x86fre
wdftools_x64fre
???? 0.46
Win32_NetworkAdapterConfiguration IPXAddress = :







192.168.70.60






192.168.132.1
192.168.70.1


Win32_NetworkAdapter Name = :
WAN Miniport (SSTP)
WAN Miniport (IKEv2)
WAN Miniport (L2TP)
WAN Miniport (PPTP)
WAN Miniport (PPPOE)
WAN Miniport (IPv6)
WAN Miniport (Network Monitor)
Realtek PCIe GBE Family Controller
WAN Miniport (IP)
Microsoft ISATAP Adapter
RAS Async Adapter
Microsoft ISATAP Adapter #2
Microsoft 6to4 Adapter
Microsoft Teredo Tunneling Adapter
VMware Virtual Ethernet Adapter for VMnet1
VMware Virtual Ethernet Adapter for VMnet8
Microsoft ISATAP Adapter #3
Microsoft ISATAP Adapter #4
Win32_NetworkAdapter MACAddress = :







8C:89:A5:67:BC:C6






00:50:56:C0:00:01
00:50:56:C0:00:08


Win32_DiskDrive Capacity = :
Disk #1, Partition #0
Disk #0, Partition #0
Disk #2, Partition #0
Win32_DiskDrive Capacity = :
Installable File System
Installable File System
Unknown
Win32_DiskDrive Capacity = :
111GB
1863GB
3GB
Win32_DiskDrive Capacity = :
630GB
Win32_PhysicalMemory Capacity = :
16384MB
Win32_PerfFormattedData_PerfOS_Memory Capacity = :
9738MB
cpu Manufacturer = :
GenuineIntel
Name = :
Intel(R) Core(TM) i7-2600K CPU @ 3.40GHz
cpu LoadPercentage = :
0
MainBoard = :
MSI
MainBoard = :
Z68A-G43 (G3) (MS-7750)

*/

WMI数据查询类封装如下:

/// @file           WmiDataConst.h
/// @brief          ...

#ifndef __WMI_DATA_CONST_H__
#define __WMI_DATA_CONST_H__



#endif

/// @file           WmiDataHelper.h
/// @brief          通过WMI取操作系统数据

#ifndef __WMI_DATA_HELPER_H__
#define __WMI_DATA_HELPER_H__

#include <vector>
#include <wbemcli.h>
#include "WmiDataConst.h"

class CWmiDataHelper
{
public:
    CWmiDataHelper();
    virtual ~CWmiDataHelper();

    /// @fn        ExecComQuery
    /// @brief    按照SQL语句, 进行COM查询, 查询的多条结果, 压入vecResult返回
    /// @param  IN const wchar_t * pcSqlW, 负荷COM查询规范的SQL语句
    /// @param  OUT vector<std::wstring> vecResult, 返回的结果集
    /// @return  boolen, true = 查询成功, false = 查询失败
    bool ExecComQuery(IN const wchar_t * pcSqlW, IN const wchar_t * pcPropertyValue, OUT std::vector<std::wstring> & vecResult);

private:
    /// 设置,取得 标记_COM是否有效
    void SetComValid(bool bValid) {m_bIsComValid = bValid;}
    bool GetComValid() {return m_bIsComValid;}

    bool SetComSecurity();
    bool CreateComInstance();
    bool ConnectComServer();

    bool ProcessWbemClassObject(IN ULONG ulRc, 
                                IN IWbemClassObject ** ppClassObject, 
                                IN const wchar_t * pcPropertyValue,
                                OUT std::vector<std::wstring> & vecResult);

    void VariantToString(const LPVARIANT pVar, std::wstring & strRc);

    void DataInit();
    void DataUnInit();

private:
    bool             m_bIsComValid;     ///< COM环境是否有效
    IWbemLocator *   m_pIWbemLocator;
    IWbemServices *  m_pWbemServices;
};

#endif

/// @file           WmiDataHelper.cpp
/// @brief         ...

#include "stdafx.h"
#include "wbemidl.h"
#include <comdef.h>
#include <string>
#include <strsafe.h>

#include "WmiDataHelper.h"
#pragma comment(lib, "wbemuuid.lib")

CWmiDataHelper::CWmiDataHelper()
{
    DataInit();

    SetComValid(S_OK == CoInitialize(NULL));
    SetComValid(SetComSecurity());
    SetComValid(CreateComInstance());
    SetComValid(ConnectComServer());
}

CWmiDataHelper::~CWmiDataHelper()
{
    DataUnInit();
    CoUninitialize();
}

bool CWmiDataHelper::SetComSecurity()
{
    HRESULT	hRc	=	S_OK;

    if(!GetComValid())
        return false;

    /// Security needs to be initialized in XP first and this was the major problem 
    /// why it was not working in XP.

    hRc = CoInitializeSecurity( NULL,
                                -1,
                                NULL,
                                NULL,
                                RPC_C_AUTHN_LEVEL_PKT,
                                RPC_C_IMP_LEVEL_IMPERSONATE,
                                NULL,
                                EOAC_NONE,
                                0);
    return ((S_OK != hRc ) && (RPC_E_TOO_LATE != hRc)) ? false : true;
}

bool CWmiDataHelper::CreateComInstance()
{
    HRESULT  hRc   = S_OK;

    if(!GetComValid())
        return false;

    hRc = CoCreateInstance( CLSID_WbemAdministrativeLocator,
                            NULL ,
                            CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER , 
                            IID_IUnknown ,
                            (void **) &m_pIWbemLocator);

    return (S_OK == hRc);
}

bool CWmiDataHelper::ConnectComServer()
{
    HRESULT  hRc                   = S_OK;
    BSTR        bstrNamespace = (L"root\\cimv2");

    if(!GetComValid())
        return false;
    hRc = m_pIWbemLocator->ConnectServer(   bstrNamespace,  // Namespace
                                            NULL,           // Userid
                                            NULL,           // PW
                                            NULL,           // Locale
                                            0,              // flags
                                            NULL,           // Authority
                                            NULL,           // Context
                                            &m_pWbemServices);
    return (S_OK == hRc);
}

bool CWmiDataHelper::ExecComQuery(  IN const wchar_t * pcSqlW, 
                                    IN const wchar_t * pcPropertyValue, 
                                    OUT std::vector<std::wstring> & vecResult)
{
    bool                    bRc         =  true;
    HRESULT                 hRc         =  S_OK;
    BSTR                    strQuery    =  _bstr_t(pcSqlW);
    BSTR                    strQL       =  (L"WQL");
    IEnumWbemClassObject *  pEnumObject =  NULL;

    const size_t            nClassObject    =   1;
    const ULONG             ulCount         =   1;  ///< pClassObject 的数量是1
    IWbemClassObject *      pClassObject[nClassObject];

    ULONG                   ulRc    = 0;
    ULONG                   ulIndex = 0;

    vecResult.clear();
    for (ulIndex = 0; ulIndex < nClassObject; ulIndex++)
        pClassObject[ulIndex] = NULL;

    if(!GetComValid())
        return false;

    hRc = m_pWbemServices->ExecQuery(strQL, strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject);
    if (S_OK != hRc)
        return false;

    hRc = pEnumObject->Reset();
    if (S_OK != hRc)
    {
        bRc = false;
        goto ExecComQuery_END;
    }

    do
    {
        hRc = pEnumObject->Next(WBEM_INFINITE, ulCount, &pClassObject[0], &ulRc);
        if (S_OK != hRc)
            goto ExecComQuery_END;

        ProcessWbemClassObject(ulRc, &pClassObject[0], pcPropertyValue, vecResult);
        for (ulIndex = 0; ulIndex < nClassObject; ulIndex++)
        {
            if(NULL != pClassObject[ulIndex])
            {
                pClassObject[ulIndex]->Release();
                pClassObject[ulIndex] = NULL;
            }
        }
    } while (WBEM_S_NO_ERROR == hRc);

ExecComQuery_END:
    for (ulIndex = 0; ulIndex < nClassObject; ulIndex++)
    {
        if (NULL != pClassObject[ulIndex])
        {
            pClassObject[ulIndex]->Release();
            pClassObject[ulIndex] = NULL;
        }
    }

    if (NULL != pEnumObject)
        pEnumObject->Release();

    return bRc;
}

void CWmiDataHelper::VariantToString(const LPVARIANT pVar, std::wstring & strRc)
{  
    USES_CONVERSION;  

    CComBSTR HUGEP *    pBstr   = NULL;
    BYTE HUGEP *        pBuf    = NULL;
    LONG                llow    = 0;
    LONG                lhigh   = 0;
    LONG                lIndex  = 0;
    HRESULT             hRc     = S_OK;  
    wchar_t             cBufW[_MAX_PATH];

    switch(pVar->vt)  
    {  
    case VT_BSTR:  
        {  
            strRc=(wchar_t *)_bstr_t(pVar->bstrVal);  
        }  
        break;  
    case VT_BOOL:  
        {  
            if(VARIANT_TRUE==pVar->boolVal)   
                strRc = L"TRUE";  
            else  
                strRc=  L"FALSE";  
        }  
        break;  
    case VT_I4:  
        {  
            StringCchPrintf( cBufW, sizeof(cBufW) / sizeof(wchar_t), TEXT("%d"), pVar->lVal);
            strRc = cBufW;
        }  
        break;  
    case VT_UI1:  
        {  
            StringCchPrintf( cBufW, sizeof(cBufW) / sizeof(wchar_t), TEXT("%d"), pVar->bVal);
            strRc = cBufW;
        }  
        break;  
    case VT_UI4:  
        {  
            StringCchPrintf( cBufW, sizeof(cBufW) / sizeof(wchar_t), TEXT("%d"), pVar->ulVal);
            strRc = cBufW;
        }  
        break;  

    case (VT_BSTR | VT_ARRAY):  
        {  
            hRc=SafeArrayAccessData(pVar->parray,(void HUGEP**)&pBstr);  
            hRc=SafeArrayUnaccessData(pVar->parray);  
            strRc = (wchar_t *)_bstr_t(pBstr->m_str);  
        }  
        break;  

    case (VT_I4 | VT_ARRAY):  
        {  
            SafeArrayGetLBound(pVar->parray, 1, &llow);   
            SafeArrayGetUBound(pVar->parray, 1, &lhigh);  

            hRc = SafeArrayAccessData(pVar->parray,(void HUGEP**)&pBuf);  
            hRc = SafeArrayUnaccessData(pVar->parray);  
            strRc = L"";
            for(lIndex=llow; lIndex<=lhigh; ++lIndex)  
            {  
                StringCchPrintf( cBufW, sizeof(cBufW) / sizeof(wchar_t), TEXT("%d"), pBuf[lIndex]);
                strRc += cBufW;
            }  
        }  
        break;  
    default:  
        break;  
    }  
}

bool CWmiDataHelper::ProcessWbemClassObject(IN ULONG ulRc, 
                                            IN IWbemClassObject ** ppClassObject, 
                                            IN const wchar_t * pcPropertyValue,
                                            OUT std::vector<std::wstring> & vecResult)
{
    VARIANT         v;
    HRESULT         hRc     = S_OK;
    ULONG           ulIndex =  0;
    std::wstring    strW    = L"";
    wchar_t         cBufW[_MAX_PATH];

    if ((NULL == ppClassObject) || (NULL == *ppClassObject))
        return false;

    VariantInit(&v);
    for (ulIndex = 0; ulIndex < ulRc; ulIndex++)
    {
        hRc = (*(ppClassObject + ulIndex))->Get(pcPropertyValue, 0, &v, 0, 0);
        if(S_OK == hRc)
        {
            VariantToString(&v, strW);
            vecResult.push_back(strW);
        }
    }

    VariantClear(&v);
    return true;
}

void CWmiDataHelper::DataInit()
{
    m_bIsComValid = false;
    m_pIWbemLocator = NULL;
    m_pWbemServices = NULL;
}

void CWmiDataHelper::DataUnInit()
{
    if (NULL != m_pWbemServices)
        m_pWbemServices->Release();

    if (NULL != m_pIWbemLocator)
        m_pIWbemLocator->Release();
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值