转载:http://www.cnblogs.com/renyuan/archive/2012/12/29/2838716.html
转载:http://blog.csdn.net/jhqin/article/details/5548656
转载:http://blog.csdn.net/dongdan_002/article/details/42460343
头文件WMIInfo.h
/*************************************************************************************************
*
* File Name : WMIInof.h
*
* Created : 2016/10/10
*
* Author : weidongdong
*
* Model :
*
* Description : [[CN]] 获取系统硬件配置信息 [[CN]]
*
**************************************************************************************************/
#ifndef _WMIINFO_H_
#define _WMIINFO_H_ #include <WbemIdl.h>
#pragma comment(lib,"WbemUuid.lib") class CWmiInfo
{
public:
CWmiInfo();
~CWmiInfo(); public:
HRESULT InitWmi(); //初始化WMI
HRESULT ReleaseWmi(); //释放 /*获取一个类成员
*@param [in ] ClassName Example: "Win32_Processor"
*@param [in ] ClassMember Example: "SerialNumber"
*@param [out] chRetValue
*@param return TRUE success; false fail Example:
CString strRetValue;
GetSingleItemInfo(_T("Win32_Processor"),_T("Caption"),strRetValue);
*/
BOOL GetSingleItemInfo(CString ClassName,CString ClassMember,CString &chRetValue); /*获取一个类的多个成员
*@param [in ] ClassName Example: "Win32_Processor"
*@param [in ] ClassMember Example: "SerialNumber"
*@param [in ] n 成员个数
*@param [out] chRetValue
*@param return TRUE success; false fail Example:
CString strRetValue;CString [] strClassMem = {_T("Caption"),_T("CurrentClockSpeed"),_T("DeviceID"),_T("Manufacturer"),_T("Manufacturer")};
GetGroupItemInfo(_T("Win32_Processor"),strClassMem,5,strRetValue);
*/
BOOL GetGroupItemInfo(CString ClassName,CString ClassMember[],int n,CString &chRetValue); private:
void VariantToString(const LPVARIANT,CString &) const;//将Variant类型的变量转换为CString private:
IEnumWbemClassObject* m_pEnumClsObj;
IWbemClassObject* m_pWbemClsObj;
IWbemServices* m_pWbemSvc;
IWbemLocator* m_pWbemLoc;
};
#endif
实现文件WMIInfo.cpp
#include "stdafx.h"
#include "WmiInfo.h" CWmiInfo::CWmiInfo(void)
{
m_pWbemSvc=NULL;
m_pWbemLoc=NULL;
m_pEnumClsObj = NULL;
} CWmiInfo::~CWmiInfo(void)
{
m_pWbemSvc=NULL;
m_pWbemLoc=NULL;
m_pEnumClsObj = NULL;
} HRESULT CWmiInfo::InitWmi()
{
HRESULT hr; //一、初始化COM组件
//初始化COM
hr=::CoInitializeEx(,COINIT_MULTITHREADED);
if (SUCCEEDED(hr) || RPC_E_CHANGED_MODE == hr)
{
//设置进程的安全级别,(调用COM组件时在初始化COM之后要调用CoInitializeSecurity设置进程安全级别,否则会被系统识别为病毒)
hr=CoInitializeSecurity(NULL,
-,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL);
//VERIFY(SUCCEEDED(hr)); //二、创建一个WMI命名空间连接
//创建一个CLSID_WbemLocator对象
hr=CoCreateInstance(CLSID_WbemLocator,
,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID*)&m_pWbemLoc);
// VERIFY(SUCCEEDED(hr)); //使用m_pWbemLoc连接到"root\cimv2"并设置m_pWbemSvc的指针
hr=m_pWbemLoc->ConnectServer(CComBSTR(L"ROOT\\CIMV2"),
NULL,
NULL,
,
NULL,
,
,
&m_pWbemSvc);
// VERIFY(SUCCEEDED(hr)); //三、设置WMI连接的安全性
hr=CoSetProxyBlanket(m_pWbemSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE);
// VERIFY(SUCCEEDED(hr)); }
return(hr);
} HRESULT CWmiInfo::ReleaseWmi()
{
HRESULT hr; if (NULL != m_pWbemSvc)
{
hr=m_pWbemSvc->Release();
}
if (NULL != m_pWbemLoc)
{
hr=m_pWbemLoc->Release();
}
if (NULL != m_pEnumClsObj)
{
hr=m_pEnumClsObj->Release();
} ::CoUninitialize(); return(hr);
} BOOL CWmiInfo::GetSingleItemInfo(CString ClassName,CString ClassMember,CString &chRetValue)
{
USES_CONVERSION; CComBSTR query("SELECT * FROM ");
VARIANT vtProp;
ULONG uReturn;
HRESULT hr;
BOOL bRet = FALSE; if (NULL != m_pWbemSvc)
{
//查询类ClassName中的所有字段,保存到m_pEnumClsObj中
query+=CComBSTR(ClassName);
hr=m_pWbemSvc->ExecQuery(CComBSTR("WQL"),query,WBEM_FLAG_FORWARD_ONLY|WBEM_FLAG_RETURN_IMMEDIATELY,
,&m_pEnumClsObj);
if (SUCCEEDED(hr))
{
//初始化vtProp值
VariantInit(&vtProp);
uReturn=; //返回从当前位置起的第一个对象到m_pWbemClsObj中
hr=m_pEnumClsObj->Next(WBEM_INFINITE,,&m_pWbemClsObj,&uReturn);
if(SUCCEEDED(hr)&&uReturn>)
{
//从m_pWbemClsObj中找出ClassMember标识的成员属性值,并保存到vtProp变量中
hr=m_pWbemClsObj->Get(CComBSTR(ClassMember),,&vtProp,,);
if (SUCCEEDED(hr))
{
VariantToString(&vtProp,chRetValue);
VariantClear(&vtProp);//清空vtProp
bRet = TRUE;
}
}
}
}
if(NULL != m_pEnumClsObj)
{
hr=m_pEnumClsObj->Release();
m_pEnumClsObj = NULL;
}
if(NULL != m_pWbemClsObj)
{
hr=m_pWbemClsObj->Release();
m_pWbemClsObj = NULL;
}
return bRet;
} BOOL CWmiInfo::GetGroupItemInfo(CString ClassName,CString ClassMember[],int n,CString &chRetValue)
{
USES_CONVERSION; CComBSTR query("SELECT * FROM ");
CString result,info;
VARIANT vtProp;
ULONG uReturn;
HRESULT hr;
int i;
BOOL bRet = FALSE;
if (NULL != m_pWbemSvc)
{
query+=CComBSTR(ClassName);
hr=m_pWbemSvc->ExecQuery(CComBSTR("WQL"),query,WBEM_FLAG_FORWARD_ONLY|WBEM_FLAG_RETURN_IMMEDIATELY,,&m_pEnumClsObj);
if (SUCCEEDED(hr))
{
VariantInit(&vtProp); //初始化vtProp变量
if(m_pEnumClsObj)
{
Sleep();
uReturn=;
hr=m_pEnumClsObj->Next(WBEM_INFINITE,,&m_pWbemClsObj,&uReturn);
if (SUCCEEDED(hr) &&uReturn>)
{
for(i=;i<n;++i)
{
hr=m_pWbemClsObj->Get(CComBSTR(ClassMember[i]),,&vtProp,,);
if (SUCCEEDED(hr))
{
VariantToString(&vtProp,info);
chRetValue+=info+_T("\t");
VariantClear(&vtProp);
bRet = TRUE;
}
}
chRetValue+=_T("\r\n");
}
}
}
} if(NULL != m_pEnumClsObj)
{
hr=m_pEnumClsObj->Release();
m_pEnumClsObj=NULL;
}
if(NULL != m_pWbemClsObj)
{
hr=m_pWbemClsObj->Release();
m_pWbemClsObj=NULL;
}
return bRet;
} void CWmiInfo::VariantToString(const LPVARIANT pVar,CString &chRetValue) const
{
USES_CONVERSION; CComBSTR HUGEP* pBstr;
BYTE HUGEP* pBuf;
LONG low,high,i;
HRESULT hr; switch(pVar->vt)
{
case VT_BSTR:
{
chRetValue=W2T(pVar->bstrVal);
}
break;
case VT_BOOL:
{
if(VARIANT_TRUE==pVar->boolVal)
chRetValue="是";
else
chRetValue="否";
}
break;
case VT_I4:
{
chRetValue.Format(_T("%d"),pVar->lVal);
}
break;
case VT_UI1:
{
chRetValue.Format(_T("%d"),pVar->bVal);
}
break;
case VT_UI4:
{
chRetValue.Format(_T("%d"),pVar->ulVal);
}
break; case VT_BSTR|VT_ARRAY:
{
hr=SafeArrayAccessData(pVar->parray,(void HUGEP**)&pBstr);
hr=SafeArrayUnaccessData(pVar->parray);
chRetValue=W2T(pBstr->m_str);
}
break; case VT_I4|VT_ARRAY:
{
SafeArrayGetLBound(pVar->parray,,&low);
SafeArrayGetUBound(pVar->parray,,&high); hr=SafeArrayAccessData(pVar->parray,(void HUGEP**)&pBuf);
hr=SafeArrayUnaccessData(pVar->parray);
CString strTmp;
high=min(high,MAX_PATH*-);
for(i=low;i<=high;++i)
{
strTmp.Format(_T("%02X"),pBuf[i]);
chRetValue+=strTmp;
}
}
break;
default:
break;
}
}
调用
#include "stdafx.h"
#include <iostream>
#include "WMIInfo.h" int main()
{
CWmiInfo WMI;
WMI.InitWmi(); // 网卡原生MAC地址
CString strNetwork;
WMI.GetSingleItemInfo(L"Win32_NetworkAdapter WHERE (MACAddress IS NOT NULL) AND (NOT (PNPDeviceID LIKE 'ROOT%'))",L"PNPDeviceID",strNetwork);
if (!strNetwork.IsEmpty())
{
wcout << "网卡原生MAC地址: "<< strNetwork.GetBuffer() << endl;
} // 硬盘序列号
CString strDiskDrive;
WMI.GetSingleItemInfo(L"Win32_DiskDrive WHERE (SerialNumber IS NOT NULL) AND (MediaType LIKE 'Fixed hard disk%')",L"SerialNumber",strDiskDrive); if (!strDiskDrive.IsEmpty())
{
wcout << "硬盘序列号 : " << strDiskDrive.GetBuffer() << endl;
} // 主板序列号
CString strBaseBoard;
WMI.GetSingleItemInfo(L"Win32_BaseBoard WHERE (SerialNumber IS NOT NULL)",L"SerialNumber",strBaseBoard); if (!strBaseBoard.IsEmpty())
{
wcout << "主板序列号 : " << strBaseBoard.GetBuffer() << endl;
} // 处理器ID
CString strProcessorID;
WMI.GetSingleItemInfo(L"Win32_Processor WHERE (ProcessorId IS NOT NULL)",L"ProcessorId",strProcessorID); if (!strProcessorID.IsEmpty())
{
wcout << "处理器ID : " << strProcessorID.GetBuffer() << endl;
} // BIOS序列号
CString strBIOS;
WMI.GetSingleItemInfo(L"Win32_BIOS WHERE (SerialNumber IS NOT NULL)",L"SerialNumber",strBIOS); if (!strBIOS.IsEmpty())
{
wcout << "BIOS序列号 : " << strBIOS.GetBuffer() << endl;
} // 主板型号
CString strBaseBoardType;
WMI.GetSingleItemInfo(L"Win32_BaseBoard WHERE (Product IS NOT NULL)",L"Product",strBaseBoardType); if (!strBaseBoardType.IsEmpty())
{
wcout << "主板型号 : " << strBaseBoardType.GetBuffer() << endl;
} // 网卡当前MAC地址
CString strCurrentNetwork;
WMI.GetSingleItemInfo(L"Win32_NetworkAdapter WHERE (MACAddress IS NOT NULL) AND (NOT (PNPDeviceID LIKE 'ROOT%'))",L"MACAddress",strCurrentNetwork); if (!strCurrentNetwork.IsEmpty())
{
wcout << "网卡当前MAC地址: " << strCurrentNetwork.GetBuffer() << endl;
}
WMI.ReleaseWmi(); getchar();
return ;
}