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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
; @  ]% p- B  _/ R5 s2 {0 w! [$ X% u2 ?4 i2 b
9 ~! R( p4 o7 [
--------------------------------------------------------------------------------# R4 S6 q3 R) v% V# u) q
WMI: Windows Management Instrumentation (Windows 管理工具)0 F3 t8 D/ F' K( ~2 H) H; m
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
9 U& B( H$ x* Z  D, ~5 O0 H   利用这个工具可以管理本地或客户端系统中几乎所有的信息。. g8 x9 k( ?5 w# |) e9 P# h2 |" ~" G
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 7 i) d- q* t) |8 d/ N2 `' _9 |

4 Q: r+ `" }3 R2 g--------------------------------------------------------------------------------+ c4 M/ A" b$ x
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
$ M! C* Y9 {$ ~3 j* u" G8 I. W" g  Z6 i. {" ]5 S
--------------------------------------------------------------------------------
. y; V: K- \4 l6 Z+ p① 初始化 COM 接口:# }7 u: N3 n+ c! w9 N$ r" Z
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
4 s. R: c- ]$ [/ K+ q5 s2 `( ]   这两个函数在 #include <comdef.h> 里面定义。' \* S9 \" u  B" C
+ p8 U. ?2 X: f6 Q% I8 p, \) c$ q
② 获取访问 WMI 权限:
9 f% Q/ N8 s9 N- v# ]$ `. D4 K   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);! J& x4 z. P. m
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。8 ]0 t! k8 r; O2 W
+ ~- P8 A" w3 s9 R6 k
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
1 D7 r7 P* R5 A: y* q   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
- h# v" D6 g- I0 q# h8 p/ X- l: b/ |  Q% [5 G
void GetWmiInfo(TStrings *lpList, WideString wsClass)
! U: B1 t) ~; r3 v. a6 d* Q{! l# c( y( L7 q) \% i+ j/ q8 _) g
  IWbemLocator *pWbemLocator = NULL;$ U" E: Q( r- `  m# c
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK): \  L) f# t+ ], o
  {9 p  Z. B6 W1 f5 U0 K* z
    IWbemServices *pWbemServices = NULL;
1 c+ Y/ N8 U/ K     WideString wsNamespace = (L"root\\cimv2");+ A) Y7 H2 Q0 c& f) L( W7 W
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)4 d8 ?' C  p: R8 [% h
     {8 d4 g0 K6 g5 A7 X8 K4 b
       IEnumWbemClassObject *pEnumClassObject = NULL;
9 G5 T/ s5 s+ a, ~0 O; b        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
6 s0 l) R/ W- V) }. n        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
) V  |- L: S7 ]0 d         {
, P) U9 J9 ]7 b" s           IWbemClassObject *pClassObject = NULL;
6 |' Z4 z4 }) \2 h* |& x           ULONG uCount = 1, uReturned;! _  }( F9 W6 ?5 ~
          if(pEnumClassObject->Reset() == S_OK)
- y. n1 t& v2 N' x1 i4 a            {5 N6 D0 U* o8 V# W% y, g
             int iEnumIdx = 0;
; c* ^/ }( }5 ]& C/ N              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)6 H, s! f! k& @* b
              {
- W: ^- Y4 W/ G! L0 y4 V                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
% F! u# z3 K) U' t& p2 J" t& Y9 _+ s2 M) ?
                SAFEARRAY *pvNames = NULL;- u3 l9 H5 O! N+ Z5 S% z1 a
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
. o6 z! B: g0 x# {  y, Y                  {
, k6 @1 l5 y3 A& Z- t/ _! d; K; J& N                    long vbl, vbu;' {+ X1 ]" b/ W* {8 y5 j
                   SafeArrayGetLBound(pvNames, 1, &vbl);' M$ l$ H, d+ [; M- H
                   SafeArrayGetUBound(pvNames, 1, &vbu);" l& X+ q0 y/ X# U1 X! f8 \
                   for(long idx=vbl; idx<=vbu; idx++)
5 ^, h& o2 l1 {8 F( Q+ D                     {3 _, w! }* k% P, T/ y
                      long aidx = idx;) [, j8 G. R7 ~) ^
                      wchar_t *wsName = 0;% I/ Y1 q7 G& m
                      VARIANT vValue;' h- \% ?+ O0 J9 o8 n: c, @" o
                      VariantInit(&vValue);
" ~6 y% L* G" s2 |                       SafeArrayGetElement(pvNames, &aidx, &wsName);
- n1 c" x4 o' Z1 T
" m! J( b& D/ G& C                       BSTR bs = SysAllocString(wsName);2 r, M  ]7 u" `6 F9 Y; `* I$ T
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
- \. C+ e9 w5 H2 q4 Q5 T* W3 w                       SysFreeString(bs);
. v7 q. g: R# i! m. m$ h6 b6 H% H2 T- W; n) W( q1 A% N' A4 J
                      if(hRes == S_OK)
4 V$ x4 w2 [% s! Y( v, F' m                        {
4 Y5 D/ O' S- g$ t8 v0 @                          AnsiString s;  o, d8 N: V* d2 a  q8 R. a
                         Variant v = *(Variant*)&vValue;% }0 N7 C& O5 J; @
                         if(v.IsArray())
# ?. m2 F/ s9 u9 v                           {' G( L  A! W* H# T4 d
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)( |4 V4 A; s/ Y/ a) F2 Q8 T
                             {
3 Z- L  g* s( I) j" O. B                                Variant a = v.GetElement(i);- C/ H; n# c, ^: I# {  i' U7 P
                               if(!s.IsEmpty())
. e! r2 L6 A/ F5 S. K* r                                  s+=", ";
) i  Y2 r4 p; _- j! x2 `) n% z                                s+=VarToStr(a);) \6 ?* D9 Z6 P
                             }' }; Z7 f" O" ?" b8 s+ \
                          }
- m' V7 T( e9 X! v& l3 s( d                          else- g9 a' P1 C" h5 p
                          {
& o, l4 q1 B' w0 r1 V- t5 }                             s = VarToStr(v);7 g1 M- X& `1 ~) b* c# D, `
                          }) H& J+ i& d# P; u4 g
                         lpList->Add(AnsiString(wsName)+"="+s);
0 G9 s4 x3 u: q; F) X                        }: J7 X# {6 P7 n  u; O
2 J; c. ~1 W2 r
                      VariantClear(&vValue);
; M: ~6 C: i9 ]% S: m                       SysFreeString(wsName);- |, {" @: D% h  B) J
                    }) G1 X5 W+ a+ q  g2 h% K
                 }
4 e- e- e3 g, ^% O                 if(pvNames)SafeArrayDestroy(pvNames);; r$ O3 w; P" j2 S
                iEnumIdx++;* }( n2 K$ v* v, i* i
              }5 J( X5 K% I5 M% m- X1 h8 Z6 |
           }! j& A& W7 i) x$ b! {0 H& \+ A) L* O' N
          if(pClassObject)pClassObject->Release();
4 ~5 d# D4 q: e5 K0 q; n/ F' b5 a         }
" p% p3 P9 X8 H, C) B        if(pEnumClassObject)pEnumClassObject->Release();- t1 v4 C' u% V( d
     }. W3 J3 S& o, K: h% l8 E
    if(pWbemServices)pWbemServices->Release();
4 y5 {0 H% t6 L( G   }
! ]7 C# B& G9 b; [& w, E  if(pWbemLocator)pWbemLocator->Release();  k* k, R  D  B9 |& A: {
}! t" a; X- P3 k$ P) c
//---------------------------------------------------------------------------" F+ J0 T0 x3 p# d( d1 Y5 V

) K4 c- Q, ^6 F, G- g" b5 V// 通过 WIN32_bios 获取 BIOS 信息:2 y' x& m1 @, n6 _% K
void __fastcall TForm1::Button1Click(TObject *Sender)
3 u3 b) s3 \/ Y; o{
7 T& L; v) C4 {6 p; b( S    Memo1->Lines->Add("================== [WIN32_bios] =================");! M( w  L- K3 ~/ h
   GetWmiInfo(Memo1->Lines, "WIN32_bios");
( q: e; [* _$ V# w# ~4 {1 Q* ^; K    Memo1->Lines->Add("");
0 a! ^# d# w7 j3 G+ Y}; q  g, P7 f2 E: j4 m/ y+ A8 ^/ ]

& N' p. @" |, P--------------------------------------------------------------------------------: y* o) ?+ D' H3 O" @9 E$ q' z
+ A- E3 X7 |; ?1 \5 u  l% [" b0 O
WMI 可以访问的信息类型有:" z+ o. j  ?: N, E/ M
  Win32_1394Controller
( f6 E, b0 \% b, Q/ I8 q   Win32_BaseBoard
8 O& Z  F' x% w" o: N5 r. C   Win32_Battery/ k, \; c  t  T+ s6 H
  Win32_BIOS& _' q1 Q$ ~' I% ~1 o9 d
  Win32_Bus
( a9 C& Q. S9 b4 X   Win32_CacheMemory
% t* {% S6 I8 G1 h   Win32_CDROMDrive
6 {! u$ P. L2 s; r) e' }4 c8 Q   Win32_CurrentProbe
2 K8 n1 c( f/ I6 j! D   Win32_DesktopMonitor
6 ]4 l2 ?) Z& b  s   Win32_DeviceMemoryAddress
6 y1 s* o/ u' d8 ]   Win32_DiskDrive
4 E9 E, |: H, o5 I% W- Q- ^3 B   Win32_DisplayConfiguration
- K% W9 Q4 D! b1 J+ M   Win32_DisplayControllerConfiguration% H4 \5 V& l$ B+ u0 L. y4 C
  Win32_DMAChannel
, l5 e' A& ~' \   Win32_Fan
+ O( X- G- g8 ]8 [. Y- x   Win32_FloppyController
  f. U+ k0 r( o! b' c1 t* A   Win32_FloppyDrive
" \# C! a- J, E% D( g   Win32_HeatPipe& h' G/ S/ a. S
  Win32_IDEController5 P% X7 \8 O; h. l
  Win32_InfraredDevice0 q5 k4 N) F% y0 {! W6 r9 E
  Win32_IRQResource
' [' M* z; I/ |. G5 _* j8 D   Win32_Keyboard
1 d9 G8 i' f1 g: a5 B" Q2 G   Win32_MemoryArray  Q9 k9 d6 }" X+ u
  Win32_MemoryDevice
7 O) Y( ^! u3 ]2 z3 g   Win32_MotherboardDevice
$ }# W8 L/ y2 S   Win32_NetworkAdapter
! \9 C* F# v) @$ U! O% U# J7 M   Win32_NetworkAdapterConfiguration
' u" O4 [' O5 i   Win32_OnBoardDevice
8 Z' q# o2 C7 U   Win32_ParallelPort
6 A  c+ R; H1 x' |   Win32_PCMCIAController* k; f, c! D8 @/ }3 W
  Win32_PhysicalMemory+ k4 L1 I) y% H
  Win32_PhysicalMemoryArray& l4 y5 T  t8 I: P: i
  Win32_PnPEntity2 [  Y+ \( r8 Q9 t/ ]5 ~4 M2 V
  Win32_PointingDevice" F7 q, x. \0 b8 K2 p7 }1 {
  Win32_PortableBattery
3 G5 p* W+ v: l; r9 a* p% o: f5 [   Win32_PortConnector
& F+ V! `1 w" S% e. [   Win32_PortResource
$ y9 Y0 p' l( v9 ~3 ]; o" c   Win32_POTSModem9 ?  a3 D" Q' D& U& G9 B5 s
  Win32_PowerManagementEvent* I# b4 y+ d0 i& g4 v% R/ Z
  Win32_Printer4 ^$ ^- E$ z% u' G% n- z9 ]" T& T
  Win32_PrinterConfiguration/ Z& f3 }  e6 v" w2 ^
  Win32_PrintJob$ O4 p2 q6 J8 t, u0 ?$ s# c- G
  Win32_Processor
7 P. e, n0 p2 k9 F- M   Win32_Refrigeration
3 i2 C3 m. ?. e4 d   Win32_SerialPort/ l' d1 `0 o9 z, w' [/ p2 o
  Win32_SerialPortConfiguration
3 M1 i+ D6 C4 F   Win32_SMBIOSMemory5 g+ Y  ^5 {( P5 w0 G
  Win32_SoundDevice$ i. f9 u2 h! D  o# E
  Win32_SystemEnclosure  x# I& F# }5 w% z. d
  Win32_SystemMemoryResource6 Y! E$ A( L- N7 N
  Win32_SystemSlot
( i  v5 J' Z+ h6 a   Win32_TapeDrive3 U6 P( F  }1 f/ r9 L9 a1 s" k
  Win32_TemperatureProbe
' D$ a+ _! [8 d   Win32_UninterruptiblePowerSupply
. @$ ?# `* ^; ^' Z0 v# w   Win32_USBController
4 O' c' D8 ?( F* n   Win32_VideoConfiguration% ?, b: Z, u; [$ J$ x* Y& S
  Win32_VideoController$ T. x! E* v+ }% I+ S+ b
  Win32_VoltageProbe. M* |* T$ d. x+ [0 `0 o9 L

7 d( N3 Q! G6 [' |- K以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-22 13:31 , Processed in 0.023458 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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