利用WMI获取本机基本信息
Posted T20140401
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用WMI获取本机基本信息相关的知识,希望对你有一定的参考价值。
一.WMI简介
WMI是一项核心的Windows管理技术,WMI作为一种规范和基础结构,通过它可以访问、配置、管理和监视几乎所有的Windows资源,比如用户可以在远程计算机器上启动一个进程;设定一个在特定日期和时间运行的进程;远程启动计算机;获得本地或远程计算机的已安装程序列表;查询本地或远程计算机的Windows事件日志等等。
二.利用WMI获取本机基本信息
在这里,主要是利用wmi提供的一些接口实现了一个简单的类,可以方便的调用,获取本机序列号信息。头文件如下:
#pragma once
#include<atlbase.h>
#include<objbase.h>
#include<WbemIdl.h>
#include<WbemCli.h>
#include<comdef.h>
#pragma comment(lib,"wbemuuid.lib")
#pragma comment(lib,"ole32.lib")
#define DEF_MAX_WBEMOBJ 20
#define DEF_MAX_PATH 260
class TClientDMI
public:
TClientDMI();
~TClientDMI();
public:
CString getDMIInformation(TCHAR *pWin32_ClassName, TCHAR *pUniqueCaption,TCHAR *pUniqueValue, TCHAR *pTargetCaption);
private:
HRESULT _initDMI();
private:
BSTR m_strNameSpace;
BOOL m_blDMIInit; //DMI init flag
private:
IWbemLocator* m_pIWbemLocator;
IWbemServices* m_pIWbemServices;
IEnumWbemClassObject* m_pIEnumWbem;
IWbemClassObject* m_szIWbemObject[DEF_MAX_WBEMOBJ];
BSTR m_strClassName;
;
#include"stdafx.h"
TClientDMI::TClientDMI()
m_blDMIInit = FALSE;
m_strNameSpace = NULL;
m_pIWbemLocator = NULL;
m_pIWbemServices= NULL;
m_pIEnumWbem = NULL;
m_strClassName = NULL;
for (int i=0;i<DEF_MAX_WBEMOBJ;i++)
m_szIWbemObject[i] = NULL;
TClientDMI::~TClientDMI()
if (m_pIEnumWbem != NULL)
m_pIEnumWbem->Release();
m_pIEnumWbem = NULL;
if (m_strClassName != NULL)
SysFreeString(m_strClassName);
m_strClassName = NULL;
if (m_strNameSpace != NULL)
SysFreeString(m_strNameSpace);
m_strNameSpace = NULL;
if (m_pIWbemServices != NULL)
m_pIWbemServices->Release();
m_pIWbemServices = NULL;
if (m_pIWbemLocator != NULL)
m_pIWbemLocator->Release();
m_pIWbemLocator = NULL;
CoUninitialize();
//
//
// init DMI interface
//
//
HRESULT TClientDMI::_initDMI()
HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr))
return hr;
hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_SECURE_REFS, NULL);
if (FAILED(hr))
return hr;
hr = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID*)&m_pIWbemLocator);
if (FAILED(hr))
return hr;
m_strNameSpace = SysAllocString(_T("\\\\\\\\.\\\\root\\\\cimv2"));
hr = m_pIWbemLocator->ConnectServer(m_strNameSpace, NULL, NULL, 0L, 0L, NULL, NULL, &m_pIWbemServices);
if (FAILED(hr))
return hr;
m_blDMIInit = TRUE;
return hr;
//
// common query process
//step1. Init DMI interface
//step2. Get ClassInformation
//step3. Get Instance
//step4. Get the property of Instance
//
CString TClientDMI::getDMIInformation(TCHAR *pWin32_ClassName, TCHAR *pUniqueCaption, TCHAR *pUniqueValue, TCHAR *pTargetCaption)
CString strRet = _T("");
// TCHAR szTarget[MAX_PATH] = 0 ;
HRESULT hr = S_OK;
if (!m_blDMIInit)
hr= _initDMI();
if (SUCCEEDED(hr))
VARIANT var;
ULONG uReturned;
HRESULT hr;
VariantInit(&var);
m_pIEnumWbem = NULL;
m_strClassName = NULL;
m_strClassName = SysAllocString(pWin32_ClassName);
if (!m_strClassName)
return strRet;
hr = m_pIWbemServices->CreateInstanceEnum(m_strClassName, 0, NULL, &m_pIEnumWbem);
if (FAILED(hr))
return strRet;
uReturned = 1;
hr = m_pIEnumWbem->Next(2000 * DEF_MAX_WBEMOBJ, DEF_MAX_WBEMOBJ, m_szIWbemObject, &uReturned);
if (FAILED(hr))
return strRet;
VariantClear(&var);
UINT i = 0;
for (i = 0; i < uReturned; i++)
if (m_szIWbemObject[i] != NULL)
VariantClear(&var);
var.bstrVal = NULL;
if (pUniqueCaption != NULL)
hr = m_szIWbemObject[i]->Get(pUniqueCaption, 0, &var, NULL, NULL);
if (hr != WBEM_S_NO_ERROR)
return strRet;
if (pUniqueCaption == NULL || var.bstrVal!=NULL && _tcsncmp((V_BSTR(&var)),
<span style="white-space:pre"> </span>pUniqueValue, _tcslen(pUniqueValue)) == 0)
VariantClear(&var);
var.bstrVal = NULL;
hr = m_szIWbemObject[i]->Get(pTargetCaption, 0, &var, NULL, NULL);
if (var.bstrVal!=NULL)
strRet = var.bstrVal;
// _tcscpy(szTarget, var.bstrVal);
break;
VariantClear(&var);
return strRet;
获取本机序列号,无线网卡设备ID,以及mac地址。
#include "stdafx.h"
int main(int argc, TCHAR* argv[])
int ret = 0;
<span style="white-space:pre"> TCHAR szTarget[DEF_MAX_PATH] = 0 ;</span>
CString str = _T("");
TClientDMI *pDMI = new TClientDMI;
str = pDMI->getDMIInformation(_T("Win32_Bios"), NULL, NULL, _T("SerialNumber"));
_tprintf(_T("%str\\n"), str);
_tcscpy(szTarget, pDMI->getDMIInformation(_T("Win32_NetworkAdapter"), _T("NetConnectionID"), _T("WLAN"), _T("PNPDeviceID")));
_tcscpy(szTarget, pDMI->getDMIInformation(_T("Win32_DiskDrive"), NULL, NULL, _T("InterfaceType")));
_tprintf(_T("%s\\n"), szTarget);
_tcscpy(szTarget, pDMI->getDMIInformation(_T("Win32_NetworkAdapter"), _T("NetConnectionID"), _T("Ethernet"), _T("MACAddress")));
_tprintf(_T("%s\\n"), szTarget);
if (pDMI)
delete pDMI;
pDMI = NULL;
return ret;
开发者涨薪指南 48位大咖的思考法则、工作方式、逻辑体系
以上是关于利用WMI获取本机基本信息的主要内容,如果未能解决你的问题,请参考以下文章
VC能否获取所有硬件信息,现只获得CPU的信息,其他的都不行