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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)/ k! T, v+ e  u8 s/ [$ s

. q5 `* P. ], ^" N0 K
, b. e( A+ q2 Z" I  K! ]) M" x3 L--------------------------------------------------------------------------------
  b  s$ `) i( i4 eWMI: Windows Management Instrumentation (Windows 管理工具)) _$ J- ]# [7 ?: I
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。   c/ G9 `' Q" Z0 _
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。
5 K& E3 g6 l7 A& L, m2 m   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
' _, K7 o! A3 @  E/ D- |  t6 v* L0 C+ a! H8 F) B# J
--------------------------------------------------------------------------------
( j( a- g. M; o, g. FBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面. [" |/ Q7 x: X8 P+ D1 `
  M3 d8 R, C' F& `1 c
--------------------------------------------------------------------------------' T  S6 y; `* \, K: m) K# U. G
① 初始化 COM 接口:
; J- X5 y% J$ x- `2 p4 [   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
6 J$ E3 _" G6 r! @2 ?   这两个函数在 #include <comdef.h> 里面定义。! z7 D; k4 Z6 X) ?3 P
0 ^! K( d" p- `- B7 t
② 获取访问 WMI 权限:4 \$ K0 g: `  V2 m5 w, t
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
$ h4 P% z# ?. F4 C! J8 N2 V5 i   如果这个函数返回 S_OK 获取权限成功, 否则为失败。3 v$ a' l; i/ q4 [7 l) `

2 n  v9 ?& C1 [4 R3 O% s9 E; b③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:3 W; F" v8 M5 h' ^$ A- q- |6 E
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。3 @0 O/ h2 G) r0 D8 c3 m* C

5 I  Y6 g1 h7 v- z( `void GetWmiInfo(TStrings *lpList, WideString wsClass)
7 j( j& V# t8 z- V: \8 C{
' h8 W1 ]' P3 c  IWbemLocator *pWbemLocator = NULL;
( H0 w8 H5 \9 `" J  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)* V$ f7 i& O0 _% p# S, k! H
  {
0 \; u0 h7 {" G2 G4 A2 i     IWbemServices *pWbemServices = NULL;1 j* Z* Y; V% [$ ~- w
    WideString wsNamespace = (L"root\\cimv2");
: N8 Q2 D0 Z% H6 R( d  B4 x) T     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
; Y3 D" N" r4 m" \( G/ O      {) X& G. S/ Y4 f- ?/ t4 q) G1 v
       IEnumWbemClassObject *pEnumClassObject = NULL;
9 V/ B2 T, m5 I        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;# }2 p, }, A' K! |, F  E% z1 u
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)* W3 u- v! X. D! u
        {! j! H. l% |3 g$ u) k9 }& H
          IWbemClassObject *pClassObject = NULL;3 n) E( ?6 @: G/ b2 t/ p
          ULONG uCount = 1, uReturned;2 R4 ]$ q, y3 M& ]3 S% v
          if(pEnumClassObject->Reset() == S_OK)' I2 |8 n. d! N8 {0 u4 t) S+ e
           {
7 W' b' f; x2 ^! \              int iEnumIdx = 0;! k& ?& F' L9 H* z
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
! j: `3 V5 [# Y2 g+ P9 T               {
7 X0 ?  o8 n6 l* c                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");* b% X  j2 w6 O) l& t& ]/ I% G

0 S% V; m! m/ P& ^0 t/ G/ o& A& t; q                 SAFEARRAY *pvNames = NULL;
# t$ G0 ^8 v3 y                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)8 F8 k# F/ o! a# p1 s: U. U" z
                 {2 ?3 ^* f* V( w* A) [
                   long vbl, vbu;
5 h+ O6 ~7 Q, h& P& s. ^                    SafeArrayGetLBound(pvNames, 1, &vbl);
* u7 b# u' |0 ^9 K9 r                    SafeArrayGetUBound(pvNames, 1, &vbu);
' y4 B5 I! p$ e) j0 J                    for(long idx=vbl; idx<=vbu; idx++)
2 ?5 l% E+ R- r+ D$ \6 w& ?                     {
! w7 t: n9 [; G2 @; V                       long aidx = idx;7 j; Z4 r3 ]! ]. B  q: z
                      wchar_t *wsName = 0;" Q( S! O: w) i' r! p4 J
                      VARIANT vValue;) Q* o, Q; `5 m! O9 H% r
                      VariantInit(&vValue);, O' j2 k5 s+ j. X
                      SafeArrayGetElement(pvNames, &aidx, &wsName);6 @6 h/ _. Q: l

5 O5 z( S1 O2 g                       BSTR bs = SysAllocString(wsName);- c& f+ p2 W4 s0 x
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);, s  |* M$ p& r! Q
                      SysFreeString(bs);/ _8 ^( W3 M7 v0 d$ z5 M: q
% i+ `3 I2 J6 g, f3 F* I* S' l6 \+ h3 A
                      if(hRes == S_OK)
8 {* C) @  ?; {, I0 q4 f                        {
1 C, M/ I+ J8 |6 h. q' ~6 E                          AnsiString s;
9 \# S& {( E7 R: V                          Variant v = *(Variant*)&vValue;% s6 b8 U& |+ k- }8 M, j' a
                         if(v.IsArray())
2 Z; Z' Y0 m& b' ~5 J                           {
$ Y$ i7 B  A) S8 O. p                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)& P+ d+ v0 a# Y+ h+ `4 _
                             {
. c$ H$ d2 l/ r: D7 @                                Variant a = v.GetElement(i);
! q3 L( y8 L( t* D                                if(!s.IsEmpty())
+ ^9 f3 C* V' F; N* [                                  s+=", ";
" t, G4 M: s# s! D% R                                s+=VarToStr(a);# U( B; d6 U6 A0 S( c
                             }9 W: f; F0 i" {
                          }( V' Y7 g7 n) I- H
                         else
0 B# Z4 w2 D% \                           {
( O7 A, v! [! p4 E0 n2 n& q& r                             s = VarToStr(v);
4 |" n* \9 u7 U6 N                           }
7 Z$ m7 ]8 d6 `4 \1 ]+ m- z& O                          lpList->Add(AnsiString(wsName)+"="+s);: i* B% U2 R4 m) ?2 e# D4 f, z
                       }0 h( z: c9 x/ V
5 P8 v2 m2 @* a( G2 q7 p* r: s
                      VariantClear(&vValue);
0 q( m2 ^7 j. E  a  x                       SysFreeString(wsName);" B* @8 s2 }& V& H; N
                    }
$ a) N6 v* t3 h                  }
4 B! S4 d  p% o                 if(pvNames)SafeArrayDestroy(pvNames);- T! K: D. z+ ~! {- l' x  }$ b
                iEnumIdx++;
0 m* c8 F) h2 X+ f* u( w2 I, F               }
  }. y! \# D9 T8 z- b            }1 l- B0 s7 a0 B: y
          if(pClassObject)pClassObject->Release();9 \4 a$ N1 i: s. w
        }
+ y  j2 l- N0 w        if(pEnumClassObject)pEnumClassObject->Release();
( N+ a3 Q0 ^/ m: C. R. ~      }* E* ~) h# D6 f5 F
    if(pWbemServices)pWbemServices->Release();( Y1 j3 ]/ L  C
  }" X1 S: q/ s1 L4 E4 ?
  if(pWbemLocator)pWbemLocator->Release();
& L- h+ \# u$ H; \/ I4 {  K; z}% c  L( Y3 y. ?9 u7 f
//---------------------------------------------------------------------------
: e" t7 J& x7 {% r# D5 l0 {4 b1 O1 X9 j
// 通过 WIN32_bios 获取 BIOS 信息:) J, J" p) p3 `) m" D( M
void __fastcall TForm1::Button1Click(TObject *Sender)! C2 E& l! `$ f6 x: r: U
{
# O1 v/ w$ m+ A% @1 @/ z2 l. Q$ Q    Memo1->Lines->Add("================== [WIN32_bios] =================");9 J( t& n, B4 U* M. Q; z6 X
   GetWmiInfo(Memo1->Lines, "WIN32_bios");
: M- b4 p* w+ C2 p0 g) T4 F    Memo1->Lines->Add("");
# A3 B. T. I: f) t& K. r/ {5 f- F, O}
- C4 ?5 ~) E2 U% c/ x- J2 N/ h: u/ }. u+ P$ e# Y% T: S1 G4 C
--------------------------------------------------------------------------------
1 E! M* b7 w! v1 R; U- m# H9 ~
& t9 S2 h- P; z( I  `WMI 可以访问的信息类型有:" c  M1 u5 a+ g" x5 y
  Win32_1394Controller: E  }6 w$ v' @, R4 e+ n
  Win32_BaseBoard
% r- w! B2 h9 m5 \: T   Win32_Battery- c0 ~& D# r( L. x2 N7 F
  Win32_BIOS. G0 J* \9 o" O+ z/ V& _
  Win32_Bus9 G8 e4 F, p# ^
  Win32_CacheMemory
& R& e. q& J( t8 P' i   Win32_CDROMDrive1 O9 e; Z. ^8 g& m
  Win32_CurrentProbe1 ]+ [2 S( @9 P( Z1 j! J1 ?* y
  Win32_DesktopMonitor% o# H0 f7 v/ P6 M' x. q
  Win32_DeviceMemoryAddress
$ r' o! O1 S' `$ G/ t6 {5 p   Win32_DiskDrive
* o2 R( w1 Y$ @/ Z( L   Win32_DisplayConfiguration  Y2 J) V5 Q( T( |8 J+ N' Z0 c
  Win32_DisplayControllerConfiguration/ P- z0 B2 t% H
  Win32_DMAChannel3 ]$ f; U/ f8 }  C9 D: l5 C
  Win32_Fan
8 U' L7 c9 }6 Q/ D( O( O   Win32_FloppyController
2 d( h* N' B' \! E& `9 L, F/ V   Win32_FloppyDrive
( W+ l+ V+ a8 T, I) j2 b* v5 E2 W   Win32_HeatPipe9 ^, f4 e) N! h
  Win32_IDEController
' @" j) R3 \# |7 R# B( i   Win32_InfraredDevice& @, K# Z% x& v2 i( w/ e
  Win32_IRQResource' }) I1 h. M' p4 D
  Win32_Keyboard  y; T( m" [" U4 e! a8 c
  Win32_MemoryArray* |7 I6 G) t% E) t* t4 L& o1 U
  Win32_MemoryDevice
$ S5 S2 o* j$ l, X. V   Win32_MotherboardDevice
( n3 m9 Z( b; K+ u- F4 q( m   Win32_NetworkAdapter
# Q7 F$ _' u; M+ v3 p: k   Win32_NetworkAdapterConfiguration) w; u" ~2 \' x8 a/ W
  Win32_OnBoardDevice
6 a* u+ _2 p8 {   Win32_ParallelPort8 a' k6 h. t$ e) P
  Win32_PCMCIAController
5 W/ D( q; Y8 [9 ~5 i/ w   Win32_PhysicalMemory$ V& O2 Q$ c% p5 p8 O
  Win32_PhysicalMemoryArray$ ?* E6 ?# @% v$ P7 i0 r6 u
  Win32_PnPEntity
8 K2 }. q8 T6 ?2 t2 }   Win32_PointingDevice9 i% w6 i  C! Y% j$ t& J
  Win32_PortableBattery
1 n, i, S; B% B, X- B- G/ F   Win32_PortConnector$ V- V# Y1 A6 t0 b; x& e' \
  Win32_PortResource
; j- R0 B% A0 K+ V- G  J# ]2 ~% f5 N   Win32_POTSModem
9 R& H5 `. |% \0 Q6 Q9 ]1 D1 l   Win32_PowerManagementEvent
0 w# I9 p: }& v   Win32_Printer4 K9 h" U# H+ N2 Y2 b
  Win32_PrinterConfiguration
" o7 u# i: \- U1 j$ r5 `   Win32_PrintJob3 B; v, u5 i% e$ F( p
  Win32_Processor% r7 Z& }* E* K% c
  Win32_Refrigeration+ t4 w, o" V. _+ f% x! I' v
  Win32_SerialPort
3 ~7 e0 i" |0 u: c   Win32_SerialPortConfiguration8 [2 a# g' Y* g- o) N7 p0 |2 ~7 _
  Win32_SMBIOSMemory
  h6 E6 S+ j/ m) _- y% S4 Z4 g: y" m   Win32_SoundDevice. A( Z4 L) M/ K- f5 T3 l
  Win32_SystemEnclosure
9 M; \5 H! Y! t# h7 w7 u   Win32_SystemMemoryResource( u% L* l; Y. Y" P6 l: d7 |
  Win32_SystemSlot- L. }. g$ j: x
  Win32_TapeDrive; Q* M# T* D. ^& g4 x% [1 x; }9 r
  Win32_TemperatureProbe
: u1 Y9 O" u0 N   Win32_UninterruptiblePowerSupply  j( T# y# O8 L. a8 {
  Win32_USBController
! V0 E6 q, m! W0 v( N   Win32_VideoConfiguration
) q/ P) S! O: k2 w   Win32_VideoController5 V/ }; e8 |  O
  Win32_VoltageProbe
3 Q6 |& b+ a/ U% ^9 n( ^' B5 q" d. \$ e, s% E3 N2 _6 e
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-7 00:46 , Processed in 0.019344 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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