前言:随着IT管理的不断成熟,IT资产管理方面逐渐被人们认识和加于应用,比如关注CUP使用率、内存使用率等,作为调配资源和购买新设备的依据;以前是手动查看,现在有HostMonitor等软件产品。如何编写适合自己或企业习惯的产品呢?当然最好是使用WMI(Windows Management Instrumentation )。
本文介绍获取CPU和硬盘系列号的方法大部份源码均来源于网上和MSDN,在2000/XP/2003调试通过。
实现的原理都是采用WMI,故思路和框架相似;虽然只列出CPU和硬盘两种设备,但对其它设备亦适用。更多的参数信息(WQL和设备参数)等可查MSDN。
下述代码已经应用于个人开发的一个软件:针式背单词,可供参考:
a.简介:http://fjwuyongzhi.cnblogs.com/archive/2005/12/19/300126.html
b.下载1:https://files.cnblogs.com/fjwuyongzhi/EnglishWord.rar
下载2:华军http://xz.newhua.com/down/englishword.rar
下面就具体的代码作一说明:[C#/C++/VB6]
1、C#
public string CpuID
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{//要引用“using System.Management;”
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string strProcessorId;
ManagementObjectSearcher opSearch;
ManagementObjectCollection mocSystem;
strProcessorId="";
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
opSearch = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
mocSystem=opSearch.Get();//Win32_Processor:在MSDN查找
foreach( ManagementObject opInfo in mocSystem)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//返回所有CPU的ID,分别返回可修改下述语句
strProcessorId+= opInfo["ProcessorId"].ToString().Trim() ;
//ProcessorId:在MSDN查找更多
}
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//TO DO:添加异常处理
}
return strProcessorId;
}
}
public string DiskDrive
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{//硬盘厂商
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string strDiskDrive;
ManagementObjectSearcher opSearch;
ManagementObjectCollection mocSystem;
strDiskDrive="";
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
opSearch = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
mocSystem=opSearch.Get();
foreach( ManagementObject opInfo in mocSystem)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strDiskDrive+= opInfo["PNPDeviceID"].ToString().Trim() ;
}
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//TO DO:添加异常处理
}
return strDiskDrive;
}
}
2、C++
代码框架,你所要做的仅是修改WQL语句,具体可看MSDN。以下代码为简介起见,一次返回所有的CPUID或硬盘厂商系列号。
#include "StdAfx.h"
#include ".\puiddisk.h"
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
# pragma comment(lib, "wbemuuid.lib")
CpuIDDisk::CpuIDDisk(void)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
GetInfomation();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
CpuIDDisk::~CpuIDDisk(void)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
int CpuIDDisk::GetInfomation(void)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
HRESULT hres;
//步骤1:不是必须的,COM只须也只能初始化一次
hres = CoInitializeEx(0, COINIT_MULTITHREADED );
if (FAILED(hres))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return 1; //初始化COM异常:注意,COM只须也只能初始化一次
}
//步骤2:不是必须的,COM只须也只能设置一次
//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))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CoUninitialize();
return 1; // Program has failed.
}
//以上不是必须的,若已有“::COMInit();”,则要跳过
//步骤3: Obtain the initial locator to WMI
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CoUninitialize();
return 1;//Failed to create IWbemLocator object
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//步骤4:Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
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))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// 步骤5: 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
);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (FAILED(hres))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 步骤6:Use the IWbemServices pointer to make requests of WMI ----
IEnumWbemClassObject* pEnumerator = NULL;
//计算CPUID
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_Processor"),//Win32_OperatingSystem
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
// 步骤7:Get the data from the query
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(0 == uReturn)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
break;
}
VARIANT vtProp;
VariantInit(&vtProp);
hr = pclsObj->Get(L"ProcessorId", 0, &vtProp, 0, 0);
strProcessID=_com_util::ConvertBSTRToString(vtProp.bstrVal);//strProcessID:类级变量
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//计算硬盘系列号
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_DiskDrive"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (FAILED(hres))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
while (pEnumerator)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(0 == uReturn)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
break;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
VARIANT vtProp;
VariantInit(&vtProp);
hr = pclsObj->Get(L"PNPDeviceID", 0, &vtProp, 0, 0);
strDisk=_com_util::ConvertBSTRToString(vtProp.bstrVal);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
pclsObj->Release();
CoUninitialize();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3.VB6:要引用Microsoft VMI
Function getDriveSerialsNumer() As String
Dim DriveSerialsNumer$, d As Object, DiskDriveSet As Object
getDriveSerialsNumer = ""
Set DiskDriveSet = GetObject("winmgmts:{impersonationLevel=impersonate}"). _
InstancesOf("Win32_DiskDrive")
For Each d In DiskDriveSet
DriveSerialsNumer$ = d.PNPDeviceID
getDriveSerialsNumer = DriveSerialsNumer$
Exit For
Next
Set d = Nothing
Set DiskDriveSet = Nothing
End Function
网址:http://www.pinstudy.com/ws/eq/Default.aspx
“针式背单词”Web化产品:英语单词查找-返回表格式结果
英语单词查询返回表格式结果,是英语单词学习、研究分析,很好的辅助系统