//---------------------------------------------------------------------------
#include <vcl.h>
#include <comdef.h>
#include <wbemidl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
CoInitialize(NULL);
if(CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0) != S_OK)
{
ShowMessage("无法获取权限!");
Application->Terminate();
}
Memo1->Clear();
//Memo1->Lines->Add("通过 WMI 获取系统信息 - Victor Chen, http://www.cppfans.com/");
Font->Name = "宋体";
Font->Charset = GB2312_CHARSET;
Font->Size = 9;
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
CoUninitialize();
}
//---------------------------------------------------------------------------
void GetWmiInfo(TStrings *lpList, WideString wsClass,String item)
{
IWbemLocator *pWbemLocator = NULL;
if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
{
IWbemServices *pWbemServices = NULL;
WideString wsNamespace = (L"root\\cimv2");
if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
{
IEnumWbemClassObject *pEnumClassObject = NULL;
WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
{
IWbemClassObject *pClassObject = NULL;
ULONG uCount = 1, uReturned;
if(pEnumClassObject->Reset() == S_OK)
{
int iEnumIdx = 0;
while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
{
//lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
SAFEARRAY *pvNames = NULL;
if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
{
long vbl, vbu;
SafeArrayGetLBound(pvNames, 1, &vbl);
SafeArrayGetUBound(pvNames, 1, &vbu);
for(long idx=vbl; idx<=vbu; idx++)
{
long aidx = idx;
wchar_t *wsName = 0;
String temp;
VARIANT vValue;
VariantInit(&vValue);
SafeArrayGetElement(pvNames, &aidx, &wsName);
temp=AnsiString(wsName);
if(temp!=item) continue;
BSTR bs = SysAllocString(wsName);
HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
SysFreeString(bs);
if(hRes == S_OK)
{
AnsiString s;
Variant v = *(Variant*)&vValue;
if(v.IsArray())
{
for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
{
Variant a = v.GetElement(i);
if(!s.IsEmpty())
s+=", ";
s+=VarToStr(a);
}
}
else
{
s = VarToStr(v);
}
lpList->Add(s);
if(AnsiString(wsClass)!="Win32_PhysicalMemory")
return;
}
VariantClear(&vValue);
SysFreeString(wsName);
}
}
if(pvNames)SafeArrayDestroy(pvNames);
iEnumIdx++;
}
}
if(pClassObject)pClassObject->Release();
}
if(pEnumClassObject)pEnumClassObject->Release();
}
if(pWbemServices)pWbemServices->Release();
}
if(pWbemLocator)pWbemLocator->Release();
}
//---------------------------------------------------------------------------
char *AllWmiClasses[] =
{
"Win32_BaseBoard" ,
"Win32_CDROMDrive" ,
"Win32_DesktopMonitor" ,
"Win32_DiskDrive" ,
"Win32_FloppyController" ,
"Win32_Keyboard" ,
"Win32_NetworkAdapter" ,
"Win32_NetworkAdapterConfiguration" ,
"Win32_PhysicalMemory" ,
"Win32_PointingDevice" ,
"Win32_PrinterConfiguration" ,
"Win32_Processor" ,
"Win32_SoundDevice" ,
"Win32_VideoController" ,
NULL
};
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Memo1->Clear();
Memo1->Lines->Add("计算机名称");//计算机名称
GetWmiInfo(Memo1->Lines, AllWmiClasses[2],"SystemName");
Label4->Caption=Memo1->Lines->operator [](1);
Memo1->Lines->Add("主板序列号");//主板
GetWmiInfo(Memo1->Lines, AllWmiClasses[0],"SerialNumber");
if (Memo1->Lines->operator [](3)=="")
Memo1->Lines->Add("无法辨认的主板序列号");
Label2->Caption=Memo1->Lines->operator [](3);
Memo1->Lines->Add("光驱");//CDROM
GetWmiInfo(Memo1->Lines, AllWmiClasses[1],"Caption");
if (Memo1->Lines->operator [](5)=="")
Memo1->Lines->Add("无光驱");
Label6->Caption=Memo1->Lines->operator [](5);
Memo1->Lines->Add("显示器"); //显示器
GetWmiInfo(Memo1->Lines, AllWmiClasses[2],"Caption");
Edit1->Text=Memo1->Lines->operator [](7);
Memo1->Lines->Add("硬盘型号"); //硬盘型号
GetWmiInfo(Memo1->Lines, AllWmiClasses[3],"Model");
Memo1->Lines->Add("硬盘大小");//硬盘大小
GetWmiInfo(Memo1->Lines, AllWmiClasses[3],"Size");
Label14->Caption=Memo1->Lines->operator [](9)+ " "+AnsiString(StrToInt64(Memo1->Lines->operator [](11))/1024/1000000)+"G";
Memo1->Lines->Add("软盘"); //软盘
GetWmiInfo(Memo1->Lines, AllWmiClasses[4],"Manufacturer");
Label7->Caption=Memo1->Lines->operator [](13);
Memo1->Lines->Add("键盘"); //键盘
GetWmiInfo(Memo1->Lines, AllWmiClasses[5],"Caption");
Label15->Caption=Memo1->Lines->operator [](15);
Memo1->Lines->Add("网卡"); //网卡,
GetWmiInfo(Memo1->Lines, AllWmiClasses[6],"Caption");
Label12->Caption=Memo1->Lines->operator [](17);
Memo1->Lines->Add("MAC地址");//MAC地址
GetWmiInfo(Memo1->Lines, AllWmiClasses[6],"MACAddress");
Label17->Caption=Memo1->Lines->operator [](19);
Memo1->Lines->Add("IP 地址"); //IP 地址
GetWmiInfo(Memo1->Lines, AllWmiClasses[7],"IPAddress");
Label16->Caption=Memo1->Lines->operator [](21);
Memo1->Lines->Add("鼠标");//鼠标
GetWmiInfo(Memo1->Lines, AllWmiClasses[9],"Caption");
Label11->Caption=Memo1->Lines->operator [](23);
Memo1->Lines->Add("打印机"); //打印机
GetWmiInfo(Memo1->Lines, AllWmiClasses[10],"Caption");
Label13->Caption=Memo1->Lines->operator [](25);
Memo1->Lines->Add("CPU"); //CPU
GetWmiInfo(Memo1->Lines, AllWmiClasses[11],"Name");
Label1->Caption=Memo1->Lines->operator [](27).Trim();
Memo1->Lines-