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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)# _: c5 C, h7 J! `' G/ M1 |# y
' @& \2 W+ W+ |* ?( }
& _# N2 w; X$ c7 C& O' V
--------------------------------------------------------------------------------4 ]8 ]0 t  V5 q8 H2 T/ i
WMI: Windows Management Instrumentation (Windows 管理工具)3 f% f+ o7 P+ `2 Z" R5 G
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 3 S& e7 Z% n- i6 @; z/ y7 d, g& _
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。
7 g% A$ r7 m  G' h5 M" v4 t4 _8 k3 B   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
  x2 o1 `0 l2 ?6 O+ @( R  t$ j/ N! Z
--------------------------------------------------------------------------------
- N' [0 C7 I* g6 MBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
3 |5 S0 G( ?7 G; o$ e4 @; B* c( D/ C& {6 J1 Q- r
--------------------------------------------------------------------------------+ \4 [. b! e* N4 N' H  x7 {
① 初始化 COM 接口:$ q) _% j  \# T8 N$ |
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
1 R* I+ G' j1 Y4 N, d- z2 @6 ?   这两个函数在 #include <comdef.h> 里面定义。  X$ I- ?/ O5 Y9 P7 y5 W) f0 a

. f. e2 @' O# X0 {② 获取访问 WMI 权限:
& s) `& g0 l3 H$ L& j   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
! z! q% z* C& D- }$ p. D! e- d   如果这个函数返回 S_OK 获取权限成功, 否则为失败。' p/ K* V7 C. w7 o: ?
( m0 X4 I5 Q5 j+ p4 \7 G
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:( X) o/ P+ I* w8 g2 A  y7 p
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。- i: ^3 s2 Z  m
. W/ F4 O) B3 f: a( `
void GetWmiInfo(TStrings *lpList, WideString wsClass)4 _& g; c7 E3 \+ i) R+ Q
{6 W+ \- _7 a( [9 k4 V# q# u3 B
  IWbemLocator *pWbemLocator = NULL;
1 ~8 Y9 [) S3 v" e% B7 Q$ h  E  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
: s0 f# {4 t6 ]) j* j4 C   {2 H) c* x# p/ m5 J% }% |) z
    IWbemServices *pWbemServices = NULL;8 ]! F. Y7 F/ @
    WideString wsNamespace = (L"root\\cimv2");
/ U7 I* l4 K" f$ f3 b! j     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)% ?( y9 Q  B/ n4 o: n) b# i* j$ A
     {
% T9 @6 W) `$ H3 {; w; S        IEnumWbemClassObject *pEnumClassObject = NULL;
6 p8 T& O/ F/ d2 X) R7 y        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
" h' D) S) N: E- Q8 e7 N        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
  u7 d" ^  B2 |& a- u( U0 N' K         {
  A$ G! s8 l' J1 M) h0 [" d/ D           IWbemClassObject *pClassObject = NULL;
+ W( h1 ^+ h) N+ e           ULONG uCount = 1, uReturned;  i3 R5 }9 z6 D6 c) N1 K* e$ Q
          if(pEnumClassObject->Reset() == S_OK)1 G) c% p; `5 t! U2 h
           {. ]# _! ?5 C4 Z
             int iEnumIdx = 0;
. t8 r( p+ j1 P  Y              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
$ e* ~1 C3 Z% |6 L% Z* l3 H               {
: O  ~2 P# P3 j5 x' c: [" a                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
4 q0 @/ [# p; p3 G4 }; @' j
* h/ `# t4 I$ ?2 s                 SAFEARRAY *pvNames = NULL;
2 G1 G: D7 u3 i% z6 ?) o: M+ k2 B                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)) I% V$ Q# T* i  m5 `& c' S
                 {7 B. {! i8 G" J5 O* b) h) I/ }. M) S
                   long vbl, vbu;' G  ~% N7 a( ?$ [; W3 y8 w$ X
                   SafeArrayGetLBound(pvNames, 1, &vbl);
. r$ A3 D* w  n/ B- S( t* Q                    SafeArrayGetUBound(pvNames, 1, &vbu);
/ h( \3 j+ g0 P3 L7 ^8 I                    for(long idx=vbl; idx<=vbu; idx++)) X- a6 D  ?, z; d/ D* y
                    {
4 Y: Z! o1 V' J  L: E, _                       long aidx = idx;
, O8 ~2 j% c2 Y                       wchar_t *wsName = 0;
  i; E# K" w& Z' e8 B                       VARIANT vValue;0 s$ K; V2 _* s* F
                      VariantInit(&vValue);$ x& L* E6 F) t4 q" Z- {+ f  }
                      SafeArrayGetElement(pvNames, &aidx, &wsName);$ b( p0 R# K# `2 n' }
. D* d' Z/ x# n; \- s" t& U6 w3 G8 |0 z
                      BSTR bs = SysAllocString(wsName);
0 J9 ]3 H& T# u0 Y& v$ o                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);, g0 E# U3 E1 G7 |, C
                      SysFreeString(bs);2 L" G2 n) O/ m* g& e, M  s0 o) T
: P# p2 d. q. N2 j9 @
                      if(hRes == S_OK)9 P  ^- w2 y# S. _2 b* q
                       {
5 p% P1 x: N/ V3 b& ~2 p                          AnsiString s;' L$ v  e7 s  l  }- e
                         Variant v = *(Variant*)&vValue;2 c* s& w, d  X9 ?/ C3 G, q
                         if(v.IsArray())/ F. ?+ ^0 b2 Q. d! ~) i
                          {
2 a$ |8 k9 r) Y, t                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)$ X5 X/ K1 s; M! U1 }
                             {
% J+ }; l1 v/ K8 K- c0 }! }2 _                                Variant a = v.GetElement(i);
2 m7 y' B' t5 R. B+ I2 ?, E  q; Y                                if(!s.IsEmpty()). D* p8 b! c: K' p  \" }: r8 {. v
                                 s+=", ";
. s6 l' G& M3 T0 }6 P                                s+=VarToStr(a);5 Y6 Q, Q- F3 g0 D7 x
                             }1 l# @: m8 n; n  y7 ^
                          }# R2 q! M5 H& H
                         else+ [2 S' |7 c& N; U2 N
                          {
# N/ ?, v; n4 m( l+ O                             s = VarToStr(v);( {. U2 g: n: B0 ^. Z, W
                          }
6 T$ K  l6 F; q5 w9 [/ p( c- P                          lpList->Add(AnsiString(wsName)+"="+s);
! ]5 }# W+ E! ?* k( ]7 n' J                        }
) K* U1 [4 |% o. V  ?8 j& D6 p' z1 o( \
                      VariantClear(&vValue);$ l* @% T; X/ a) G: ~
                      SysFreeString(wsName);4 i8 Q; g9 _2 E2 B2 n
                    }' Z* c0 @4 W. x2 ?0 D
                 }$ P* v0 @7 g/ W% D- U
                if(pvNames)SafeArrayDestroy(pvNames);$ s+ _% U' W& P* T3 S) t
                iEnumIdx++;
# r) F6 {. S! ^0 j6 ~7 Z               }0 Y- M% Q/ w/ q+ l
           }: U3 W/ z0 B1 b9 N7 F" P, _
          if(pClassObject)pClassObject->Release();$ d' A8 p0 p: h! U! D
        }
1 z: g3 G7 [( W% D        if(pEnumClassObject)pEnumClassObject->Release();) ?' j. S6 H( j! D6 P* b
     }5 W1 o- }' p  b
    if(pWbemServices)pWbemServices->Release();5 T& u  i6 y5 V. h8 W8 j
  }
# t! f% j9 @) s# ?& O! _$ c+ D  if(pWbemLocator)pWbemLocator->Release();
& S; N. ~' q' F! @" }}4 M! |0 d- }( W2 y* E$ j# w1 Z
//---------------------------------------------------------------------------+ {3 j5 C  n' i; i2 K+ ^# U# s

; {" k& ?5 ]( M* l* D/ z// 通过 WIN32_bios 获取 BIOS 信息:  b) |/ N, j  w7 q2 D9 W7 P
void __fastcall TForm1::Button1Click(TObject *Sender)
2 F7 q+ F$ R. j7 k* B% h{% m+ C( c# S6 m/ ]' n( J) [
   Memo1->Lines->Add("================== [WIN32_bios] =================");
: n( E# c8 s7 Y" d    GetWmiInfo(Memo1->Lines, "WIN32_bios");; H6 ?  E# c( p) F
   Memo1->Lines->Add("");2 p6 a2 @5 t) W. ]
}
/ |* q8 W* f" S3 v3 j% _! p! u+ {: g- ~% ~3 h% @
--------------------------------------------------------------------------------7 h  K7 ?$ ]* Y2 j, `! d

9 |* _9 m1 S: A, u- s9 KWMI 可以访问的信息类型有:& `' f: i6 b" K/ [7 q+ j
  Win32_1394Controller
, h0 q; C# @" K$ K   Win32_BaseBoard4 Y6 ]4 e5 d( i* c
  Win32_Battery, b6 }, Z) x* T4 Y
  Win32_BIOS/ \& E: o4 o! S+ x; m: x
  Win32_Bus0 @' \& ]( f* r' h
  Win32_CacheMemory
. T2 J; I+ E4 @, h   Win32_CDROMDrive3 U2 D' E* W$ x' [2 E
  Win32_CurrentProbe$ X2 K0 `, j% j5 `
  Win32_DesktopMonitor
" g# \; r9 o5 @# l- r7 k   Win32_DeviceMemoryAddress2 k3 v' @. ?7 j& s8 M3 a
  Win32_DiskDrive9 O, z8 T: `) H7 M) h6 X7 Z
  Win32_DisplayConfiguration
/ O6 X- z3 e) f4 [$ x   Win32_DisplayControllerConfiguration# a1 Q5 [( E. X# D2 @
  Win32_DMAChannel6 F/ u: l1 l2 E7 c, A- d/ A$ s
  Win32_Fan9 Q* O+ k# a  {2 R  O
  Win32_FloppyController
4 S* e; B) H& M! C9 h8 P& n   Win32_FloppyDrive
& \8 l  ]0 K  i, J* i* @   Win32_HeatPipe
) r! V4 P' n- O# L( S. O   Win32_IDEController' Q& k* X& A# r9 J* O
  Win32_InfraredDevice
& f0 ^' @* R$ W# Z+ j   Win32_IRQResource3 y- K( ^+ B& V( {2 H( K6 l7 y
  Win32_Keyboard, R# o; \8 d. H  O3 N
  Win32_MemoryArray
& j% z+ g- g3 S/ d6 \/ r' n   Win32_MemoryDevice# a& h7 I! T, `( d; ^- T/ X& f" U
  Win32_MotherboardDevice; B% m8 t! l9 F7 k8 o" E* U3 _
  Win32_NetworkAdapter6 `% E7 E" ~# N4 w0 g* Y
  Win32_NetworkAdapterConfiguration" A/ @+ V; ?7 _3 E
  Win32_OnBoardDevice
9 T2 ^* Y# L" q4 h   Win32_ParallelPort
) k: \% b. o1 r: v/ I   Win32_PCMCIAController1 T) o. M& Z# C) j7 r$ ]
  Win32_PhysicalMemory
/ `: L/ `" Q+ R   Win32_PhysicalMemoryArray3 m( x3 c9 [. x
  Win32_PnPEntity
3 X, Z8 x& |( ?; e   Win32_PointingDevice
; z: k" i) Y7 p8 c   Win32_PortableBattery
3 M2 I+ a+ w" `0 H   Win32_PortConnector$ D- ?' o: x5 J0 j* V2 R' g0 ^4 J+ r% F' M
  Win32_PortResource) H/ X, P6 k# }8 U$ }; v  P% V
  Win32_POTSModem
$ s# G" v" b# f. A2 e   Win32_PowerManagementEvent
( p$ m. H  s1 k* C   Win32_Printer
7 V" J, Z) G* P# a' g, H( ]   Win32_PrinterConfiguration+ I. Y2 J( i4 H  A+ u
  Win32_PrintJob
, z" o0 O# q! @  E' [! N: Y2 Z1 I   Win32_Processor' R$ \; S% Y& u' d8 n
  Win32_Refrigeration
$ U9 V) A4 E. ?. l   Win32_SerialPort3 L! a0 h" r: i7 h+ Q1 j
  Win32_SerialPortConfiguration9 f, Y3 s4 h% U8 p
  Win32_SMBIOSMemory2 k$ t) f! i4 _9 a) ?
  Win32_SoundDevice
6 Z! j6 \- c" b9 L5 K) C" j$ e   Win32_SystemEnclosure
7 a' b5 Y- c. c4 P2 s! _   Win32_SystemMemoryResource/ g0 p( @0 E0 T( o
  Win32_SystemSlot; c/ Q) Q7 y: x) Y( ~( N
  Win32_TapeDrive
: f1 Q  {# g" a   Win32_TemperatureProbe
7 X# @! O* u. x3 S% v7 P   Win32_UninterruptiblePowerSupply8 ~% c2 B, R5 e
  Win32_USBController
7 I& |% @7 E4 ]: ^7 l* h2 j   Win32_VideoConfiguration0 D3 N9 e* m' d! ~
  Win32_VideoController
% D$ z: x6 [3 p, i+ t, q' F   Win32_VoltageProbe
$ J% M# [* X' E- \  Z4 c8 c( M& c, \
4 _+ J, t/ y  u5 Z& Q- S4 A以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-3 23:22 , Processed in 0.018880 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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