找回密码
 注册
搜索
查看: 12447|回复: 0

[收藏]C++ Builder 通过 WMI 获取系统信息

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)" o' e5 [" h1 N# U5 {" b3 A

8 ]1 ^# Y- t3 @0 d& I! V9 q% N- I, k+ t, t
--------------------------------------------------------------------------------3 F4 i/ A; n7 S6 [7 Z/ c6 O
WMI: Windows Management Instrumentation (Windows 管理工具)' O! u( ~% W9 [$ _6 [! h
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 7 r/ o5 M' n/ Y: n
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。, q1 y/ L( r- I+ g4 A8 h
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 9 g& X% S! L% U# |, {

- s2 G7 r% G8 O--------------------------------------------------------------------------------
' n) L( R4 v& ]% dBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
: u0 t$ \: L5 n& s9 Z' F5 m& a
# P5 L* X4 n% z0 y6 n--------------------------------------------------------------------------------6 r& f5 B: O; T+ u+ H4 Z3 o; G
① 初始化 COM 接口:7 r! E9 j% `- ~) D
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。, `  S; V3 t* d
  这两个函数在 #include <comdef.h> 里面定义。
! }6 J( J% l( @1 D8 V) m& Y5 F, U$ O2 Y: f6 D( ^, {6 _5 ~
② 获取访问 WMI 权限:
8 k# W+ H- J3 z* L/ ]) m   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);; G6 \1 M( @3 E, S9 R3 f. Q' I
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。9 l$ }+ j4 k" {+ R& m  w

, S! D. U2 b3 z3 F0 ?* u" x③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
7 w: V$ j4 {. z' Z# g( U! w5 A   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
& v9 i: a2 A# r1 V) t# q5 E2 }" d) h
void GetWmiInfo(TStrings *lpList, WideString wsClass): L; d4 K- v0 A0 v; d9 p3 R
{; m5 ?: l+ G) {9 T( y1 Y
  IWbemLocator *pWbemLocator = NULL;
/ Y7 L: e- [: h1 c. C# w  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)* ^, C- L4 `- R) k" V0 L8 A
  {
# S5 f7 q' d; y9 D     IWbemServices *pWbemServices = NULL;
+ V9 q7 s# e7 Q2 n# K7 b% ^8 g* X     WideString wsNamespace = (L"root\\cimv2");: l9 {! W+ J4 F2 Y9 A+ m
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)' o3 h0 k7 I; Z
     {7 A, z' b& ~1 i  X
       IEnumWbemClassObject *pEnumClassObject = NULL;
1 i. t3 f6 H5 K* a8 b$ r! A% H! y# H: J        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
! K6 O* }4 {  [# O& R7 l        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)9 J- n, `% V% E& a& l9 p
        {
# O( l: Z9 {) B( U3 a* |           IWbemClassObject *pClassObject = NULL;
& [, P1 d( c: P           ULONG uCount = 1, uReturned;
; c- a2 h- L+ E: L3 q& v# r           if(pEnumClassObject->Reset() == S_OK)
. t; X, d" {* @8 r) u. ^  q            {6 ~  L( V- b) V. Q% {
             int iEnumIdx = 0;
* _' r! K! t- O              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)+ w  ?7 f5 d5 @8 l. h
              {9 _0 ?% R7 P" m# z0 L, [$ }: x7 g' t
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");3 W2 h/ G/ w1 u) F: b- }/ \

* A; M3 q6 }) }                 SAFEARRAY *pvNames = NULL;
/ k! F+ O8 _1 p+ j6 U                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
" O, E5 q- {0 g; i                  {6 L3 M6 D* O" d& n2 F$ c" k6 B6 u. B
                   long vbl, vbu;
, F6 l& L8 p* L! D" K                    SafeArrayGetLBound(pvNames, 1, &vbl);
3 Q# t% \: H, A" ]                    SafeArrayGetUBound(pvNames, 1, &vbu);$ I6 d# p8 W5 w1 @3 }$ u$ p3 ^, Z
                   for(long idx=vbl; idx<=vbu; idx++)
& B1 x' c+ o3 Y8 x( m                     {9 q% D: `  |3 O8 X8 t* u( D
                      long aidx = idx;
9 e# Y0 x% W/ x7 g                       wchar_t *wsName = 0;% n& m( p. h6 v. l7 ~
                      VARIANT vValue;- A2 L$ |% R  `/ Y5 w, |+ q+ @
                      VariantInit(&vValue);
& d! a+ T" |- z6 Z: o                       SafeArrayGetElement(pvNames, &aidx, &wsName);2 Y' e% F' c% |. D* ]* \

$ t! b3 `; J: O3 n+ |  S2 P& c8 ^                       BSTR bs = SysAllocString(wsName);2 a- [: Z- Q0 B  y, [
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
6 q, {8 |( I5 _9 V4 E8 a% ?+ W                       SysFreeString(bs);3 h$ V" r& I' k9 L, Z" f

5 E" s# \' ]7 k; C! e  w                       if(hRes == S_OK)# o# y% T! u& b. \
                       {
* ^' M( g6 p% T# B% \: F0 k! c                          AnsiString s;
! @" y  P* ?& L# ?5 z+ g* H: ~4 X                          Variant v = *(Variant*)&vValue;3 ^2 ^, Y+ L) \; I: \3 x
                         if(v.IsArray())
# ?0 o, J, z6 {3 e( j/ v' S/ }                           {
* L: N/ W: x: m( X2 ]% j7 d  V6 n                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
" D/ c# i. m, s" M4 y                              {# l+ X5 }# E! H& D6 L3 f
                               Variant a = v.GetElement(i);
2 @* d8 \/ H0 J2 Q! D+ A. \                                if(!s.IsEmpty())
# M6 D  \) k3 p* l; ]                                  s+=", ";
; l3 z# J( Y3 C                                s+=VarToStr(a);
: p8 ~, m  J3 S/ E) C  }! r% y                              }
5 l9 n7 l: _! [% e5 y; @0 o5 e: ^1 b                           }/ O: \5 @- X7 b$ p8 }4 w  L0 y
                         else& B6 l4 L) k  q$ _  a8 N6 f9 I, W3 q
                          {  e. P( o; z& G+ L
                            s = VarToStr(v);
" x( R7 @7 O! h% a                           }
" b6 R( L7 F( ~. Q9 \                          lpList->Add(AnsiString(wsName)+"="+s);
( s1 M% n- M( x, E6 H! ^# s                        }3 a$ Q# q  Z! h0 q- `1 ^% R! p
5 u' j8 T; U& y9 ]/ d
                      VariantClear(&vValue);
/ L+ _# y+ s/ O& F  E                       SysFreeString(wsName);
) G& B9 O2 q: ]/ g                     }
: K. s6 |1 V* U; ~4 b+ e- W9 d                  }# D) i7 g: z6 q: ]! ^7 c; M
                if(pvNames)SafeArrayDestroy(pvNames);
) j- q0 l1 g4 f; _! s3 ^                 iEnumIdx++;
. X  X: R! f2 l1 C( J               }- t5 Y; ~; p9 z8 V  w+ W
           }
* b' l: x" W: @# R' q           if(pClassObject)pClassObject->Release();( d; q+ u( p5 m) g: G* [- }+ M9 f
        }% R- `0 [+ O4 \6 v; u9 ~9 y
       if(pEnumClassObject)pEnumClassObject->Release();+ m% z+ C. ^: h
     }$ N) L3 P+ Z: S9 @7 {% _% k
    if(pWbemServices)pWbemServices->Release();1 B$ a( x& E8 U. G5 C* O
  }
3 A" u1 Q. ]0 C  if(pWbemLocator)pWbemLocator->Release();+ g7 h0 |# ]9 }1 Q" F
}
& l  v. L7 p1 a* j//---------------------------------------------------------------------------" r  c# U% R3 i5 z; p

, J2 G9 r! T( A$ K- d// 通过 WIN32_bios 获取 BIOS 信息:
6 {  `; M& ?+ S8 S/ y% g2 W9 nvoid __fastcall TForm1::Button1Click(TObject *Sender)
" I3 e" p, E% J& e0 W" v{; j/ j% U6 e$ w
   Memo1->Lines->Add("================== [WIN32_bios] =================");6 ?1 i  L7 L: W# b! X' j
   GetWmiInfo(Memo1->Lines, "WIN32_bios");3 @" S" k; {: W, Q+ |5 o
   Memo1->Lines->Add("");+ n* A3 ~# A- y5 Y$ Q
}
9 r/ Z' \: y1 l. E
& }0 t7 U# x* k--------------------------------------------------------------------------------0 M1 j# E; A0 O5 j* h
% ]2 M4 k& s, v# \7 v7 {0 p- V
WMI 可以访问的信息类型有:
3 h- W+ y, `/ Z7 l$ G   Win32_1394Controller
7 T8 \  u! N3 K, C4 r  t9 S7 x   Win32_BaseBoard
, Z! ]" K% ^% k) B3 s2 P   Win32_Battery+ ]& q+ [, [& |0 W" P0 B, E
  Win32_BIOS( q1 A$ D5 w* d6 S6 Y. V2 F
  Win32_Bus
# q- q1 ]/ p/ l7 [0 G   Win32_CacheMemory
/ Q# r5 l  d! _9 J- l   Win32_CDROMDrive
: N; x% T. E2 s; \( C  H   Win32_CurrentProbe
5 F( s4 c8 e3 l) V7 v3 a   Win32_DesktopMonitor
$ J! t- Y" b! q0 r" r: T   Win32_DeviceMemoryAddress/ Q+ O& e8 K( }6 R4 H, O- V
  Win32_DiskDrive
" D# f7 c! Q" G8 @2 `   Win32_DisplayConfiguration+ u3 [8 V/ U# E+ ?' f6 \3 n& O$ x- K
  Win32_DisplayControllerConfiguration
  n( y$ p- P. N+ w. D8 l   Win32_DMAChannel8 ^5 @' o. A: s  Y& K. b% W
  Win32_Fan1 W5 u, [" K# r$ k
  Win32_FloppyController4 P8 [4 n/ \, a0 ~
  Win32_FloppyDrive
2 m6 W9 M7 x  k( p; A6 {   Win32_HeatPipe! v" H9 O5 f4 e- P6 G
  Win32_IDEController1 [/ S* m( i* N5 u
  Win32_InfraredDevice+ k& x( h0 B: I5 @6 ?6 j
  Win32_IRQResource, r* |* K7 ?' x; U
  Win32_Keyboard4 z: g4 o6 i1 c9 `0 O: R7 l* ^
  Win32_MemoryArray$ c% O0 v; P+ {) `$ }  V) v
  Win32_MemoryDevice
5 ]2 C8 R5 [7 `9 y9 s# [% i" L   Win32_MotherboardDevice
% g; J" |7 p4 k7 V8 Z6 Y. C% N' _   Win32_NetworkAdapter& w  r  j* O3 j. {
  Win32_NetworkAdapterConfiguration9 e( a0 ^: ~6 b& a7 I0 Z# b9 H
  Win32_OnBoardDevice
2 q% d+ W) Y& B3 S0 M% L9 G* n   Win32_ParallelPort
$ L9 r* X1 |' F   Win32_PCMCIAController% `* p3 D- y* L# @( [) ^5 v8 A8 @2 `
  Win32_PhysicalMemory
( P! W2 |6 F  G1 |   Win32_PhysicalMemoryArray
" ~) x+ l# Z; u' Q, n( I   Win32_PnPEntity
$ P* ]3 A" `) P( _2 L   Win32_PointingDevice+ H' Y4 i" E- y# \/ v" V& D
  Win32_PortableBattery3 D* g6 O% ]+ z& _6 L/ d- Y' E2 X
  Win32_PortConnector& @" V+ ~( U# b" A! N! }# N
  Win32_PortResource
4 ^/ Q! x' P3 ^, L5 `   Win32_POTSModem9 j" b6 _; Z4 c- I( v1 @0 p' L3 I5 i
  Win32_PowerManagementEvent3 m" ]  n7 A- {4 ^
  Win32_Printer% _( Q- s1 y( t; P* a8 p' M
  Win32_PrinterConfiguration
9 R  ]: {0 N3 W   Win32_PrintJob9 `) I% w6 N! ?! ?. u; c9 ^; w+ j
  Win32_Processor
" [2 b% |: }4 ]6 h- f0 }, Z   Win32_Refrigeration
8 ^9 j4 a2 X5 E5 y' w. k   Win32_SerialPort
0 A5 B+ K& K$ e/ i( U   Win32_SerialPortConfiguration; ^! i! Q# N1 y# s. j6 i
  Win32_SMBIOSMemory4 \4 q. S0 v% B
  Win32_SoundDevice- L* j5 c0 u. k% A: t5 I1 {3 y
  Win32_SystemEnclosure
- F1 j/ R4 v' A   Win32_SystemMemoryResource; E& Y$ i7 Q( T0 w: g* R6 I; |2 P3 B
  Win32_SystemSlot
6 P# m6 {3 F$ K+ e3 X9 r   Win32_TapeDrive
9 o: V1 B4 O1 C4 {7 h/ W   Win32_TemperatureProbe
: ?# |8 i  V8 ~) d- U8 a3 h6 r   Win32_UninterruptiblePowerSupply
. E8 G* B2 X$ @. R4 z+ q( L8 T   Win32_USBController2 h  \3 t% r( k. L/ C2 P
  Win32_VideoConfiguration( J8 G3 G0 W# W0 `2 x2 B3 m
  Win32_VideoController+ J' ~6 h/ g: r+ Q9 g8 a8 z
  Win32_VoltageProbe& g3 y' y: \0 @! d& @; X
# k6 m; T6 \0 o$ M3 e2 {
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|宁德市腾云网络科技有限公司 ( 闽ICP备2022007940号-5|闽公网安备 35092202000206号 )

GMT+8, 2025-11-17 10:29 , Processed in 0.017988 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表