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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
, ~% B$ t, T5 M/ _
" R8 j6 ~& s1 e$ f6 Y1 A! n! H* P* Y& l* @  [% U" W5 N
--------------------------------------------------------------------------------
$ r- z. }8 ^1 V$ o# SWMI: Windows Management Instrumentation (Windows 管理工具)3 [% P" I4 v2 }* D
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
2 W9 h) Y# X& {  O) y9 I; c   利用这个工具可以管理本地或客户端系统中几乎所有的信息。1 M8 E4 M  d4 m/ K
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 , X* V% _' l( w. `8 ], z, U1 O% ^  B

% w# @% R5 x' J% a$ u2 Q( d--------------------------------------------------------------------------------8 a2 y; R$ y0 N5 F
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面' S' v( u3 H' D
4 q& k3 v% L( |* |# l5 U* \" E
--------------------------------------------------------------------------------: a4 _# Q2 y; Z6 V: V( G& j
① 初始化 COM 接口:
5 ?& a9 u3 V; @5 z4 r' T   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。! Z2 f5 i" c& @' M& o2 E0 P
  这两个函数在 #include <comdef.h> 里面定义。
! Q' D, r) v+ C) \+ V5 s6 C3 d3 ?7 q( }* y7 |
② 获取访问 WMI 权限:7 ~1 y8 W/ {2 q% P
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);/ b+ Y0 @% K: s) `$ [' H
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。* Q7 z7 `( [3 |. S6 E/ v- o* o
  U* b+ p  @; |- W# W
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:- K& `  A( k+ }* A% K! g
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。$ N2 o: w3 M; T* A4 g4 ~

) K: q9 c3 E  Yvoid GetWmiInfo(TStrings *lpList, WideString wsClass)
; l# D3 p8 ?0 }; l- H) K4 x{$ z: S- |" j3 z
  IWbemLocator *pWbemLocator = NULL;: Y1 D( e- ]+ Z4 Q
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)5 b$ {+ e9 E+ ?( h/ A: {
  {) H6 t- F% M- j1 z
    IWbemServices *pWbemServices = NULL;/ N  x; P$ ~6 o. B7 U
    WideString wsNamespace = (L"root\\cimv2");! H3 @4 \4 g  {8 x7 Z
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)* e+ M' H- e9 @; ~& g+ p
     {
- m" m- q; F/ t        IEnumWbemClassObject *pEnumClassObject = NULL;* T$ h9 q& o$ |: Y4 h9 {8 f( G
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
7 l$ C8 t( n. {$ w, d        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
1 |$ `/ Q! g4 m# [, L         {/ E+ }4 X: O! ^* r/ u
          IWbemClassObject *pClassObject = NULL;
* {1 q0 d7 [  P% V           ULONG uCount = 1, uReturned;9 ?0 m3 ^2 R6 C
          if(pEnumClassObject->Reset() == S_OK)
$ g# _6 d: V' B8 T. G            {
! m1 Q% d, h, q& z4 f. B              int iEnumIdx = 0;& H3 E$ n5 Z5 U" L" ?
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
7 N) ]( T4 c, b% t- z3 R               {
, P3 u" g& k' O. I* d" X- d6 `                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");: E, c2 O8 |( e/ T) v( k  X

4 N8 d& T5 l0 |* V4 }5 Y0 K! V                 SAFEARRAY *pvNames = NULL;
8 s2 A: S# @" S% D                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)4 I; ~) A) x7 f/ B/ _+ G' y: E0 C
                 {
" n! U; f1 e% b2 Y& h                    long vbl, vbu;7 O8 Q* K2 v5 s' k5 i' ]* e/ x
                   SafeArrayGetLBound(pvNames, 1, &vbl);
3 `- u2 O/ d, K: y- Q                    SafeArrayGetUBound(pvNames, 1, &vbu);
( C* Z& T1 E0 _) ~6 n8 R9 p                    for(long idx=vbl; idx<=vbu; idx++)
0 K: q: I/ u+ b, z% s4 u                     {- a! Z( ^* o0 O0 x$ r4 J
                      long aidx = idx;  V* j# h! i9 }8 B6 i, \8 a% j
                      wchar_t *wsName = 0;4 D1 g3 _4 V& B% U- R
                      VARIANT vValue;  F2 b) F, J/ _+ ?. Q7 ^
                      VariantInit(&vValue);
+ C2 i! M$ M9 @7 E0 s& U                       SafeArrayGetElement(pvNames, &aidx, &wsName);4 g) X) D  B+ `0 _/ ?

# }. i7 r& p' @+ a4 A                       BSTR bs = SysAllocString(wsName);
/ {" Y/ u# p8 T3 [; R$ Q                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);2 g  Q2 [. y5 G6 l1 M
                      SysFreeString(bs);: U' E. `/ m/ Z
% b* Q2 p$ k. k( R8 g3 J0 z
                      if(hRes == S_OK); ?9 `  J! e( N3 a6 N
                       {; ]* h0 z8 j% d
                         AnsiString s;+ B% T; Y5 y, F* c) [6 o
                         Variant v = *(Variant*)&vValue;1 C/ _, {4 B4 I& k
                         if(v.IsArray())
5 ]( q+ c; C4 n8 A9 k                           {0 ]0 I8 b6 O- m$ K3 D( c7 V
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)2 W- j( ]4 Q7 ]& w' [
                             {8 y, E0 |' W9 T
                               Variant a = v.GetElement(i);' J/ p, D0 u' i4 K- i
                               if(!s.IsEmpty())
8 ?5 X; t& o3 E2 C0 F2 M                                  s+=", ";2 ?) _) H% V8 M
                               s+=VarToStr(a);
4 Q  R0 V6 S1 o; w8 c& H8 B                              }* g; w- b/ k) h! X
                          }
3 T4 a7 H8 d$ n                          else& ^+ b3 R  ]2 q% y
                          {
2 @1 N# h2 P! P5 {. O+ q2 ]                             s = VarToStr(v);
4 S* f8 a1 c  ?. d9 p# z                           }9 F* v& E' |  a5 _& \* w4 D
                         lpList->Add(AnsiString(wsName)+"="+s);
9 I9 g$ v( `! X' Z                        }
; f# L2 R7 Q5 q0 \3 D* h* `# r$ m; }6 M5 }0 {( ]- d% f
                      VariantClear(&vValue);
* u, I9 [: g5 I                       SysFreeString(wsName);
' r$ q; u2 I. i- F                     }
9 N) k- e: A" k9 t! H9 F6 u                  }( V# g% z* h' ~- ]0 z; Z
                if(pvNames)SafeArrayDestroy(pvNames);
! E/ ~4 C4 Y9 x' c% I. t2 k5 h                 iEnumIdx++;" R3 N4 g  y2 E6 R" N/ r
              }
) j$ i% J& {. g$ s5 T1 P3 L            }
  d- Y4 q/ e' c6 G, P           if(pClassObject)pClassObject->Release();
$ a+ `0 d/ |0 K         }5 f* d! |6 G! l& D6 J9 @
       if(pEnumClassObject)pEnumClassObject->Release();
# B1 P7 M! K) `; G/ O) ?      }
4 g3 |" g* T4 v# D0 Z1 c8 O' |* v1 L     if(pWbemServices)pWbemServices->Release();
5 {! l5 p2 Q) J9 W   }3 R: s! K8 u( C  A* |
  if(pWbemLocator)pWbemLocator->Release();
2 w/ X4 i' B8 X  B1 w" G' U}- C1 T7 ~5 j2 m' |' Q; v% z
//---------------------------------------------------------------------------
- k/ @1 `3 n5 Y9 H2 C8 e0 C. I  C& i4 N# Z% }4 s1 y! D9 _
// 通过 WIN32_bios 获取 BIOS 信息:
' w4 c  ]! d0 w9 ?# X* @void __fastcall TForm1::Button1Click(TObject *Sender)8 K1 I; B7 c+ B0 @! R. W2 s
{
: e3 L: r: n# m/ e% R) h4 }    Memo1->Lines->Add("================== [WIN32_bios] =================");) L' l# C) g# q/ S  Q0 m
   GetWmiInfo(Memo1->Lines, "WIN32_bios");
$ P9 |( H% N% U4 }6 [    Memo1->Lines->Add("");
! H' p6 f8 F. }8 T9 S# T}
* c( L; K8 f1 y* g. O
  B: Z% v! W) ^9 h+ _--------------------------------------------------------------------------------0 q/ I$ h( c8 t1 K, p( [% t
& P0 _! u" k7 P
WMI 可以访问的信息类型有:8 D& ~# Z% M4 U5 X  v/ h
  Win32_1394Controller
9 h" F6 i2 n- L   Win32_BaseBoard
6 @1 j: J9 p& T, ^8 l0 T   Win32_Battery
3 Q2 q$ `7 d0 Y5 S   Win32_BIOS; s5 q2 o9 j6 k) C( S0 Y
  Win32_Bus0 p4 @4 b9 f' R: L9 ]
  Win32_CacheMemory
9 l- M& M# M/ F; q1 E9 e   Win32_CDROMDrive
* R0 d& V1 h8 l* Z, S" F) P! |- [  X   Win32_CurrentProbe+ d  g! x: o- u& Z
  Win32_DesktopMonitor- D% W8 O8 [: F4 _8 Q2 _
  Win32_DeviceMemoryAddress
6 Z" T' x7 H1 S   Win32_DiskDrive
0 m! p$ x- y* e& r: C" O   Win32_DisplayConfiguration8 U; }( |; I# l. o! j
  Win32_DisplayControllerConfiguration
3 J/ t( A' v: C! [! e   Win32_DMAChannel
% x4 ~, u. j. ~' E   Win32_Fan. d7 w8 Z7 a9 `/ }/ [- k
  Win32_FloppyController
! g$ g8 z5 E/ ^5 M, `- f   Win32_FloppyDrive  j7 F% i& f# G1 C$ {6 @7 K
  Win32_HeatPipe
4 ]9 J0 ?) V# I% k" V( w0 c   Win32_IDEController& t  ?" U" \# n
  Win32_InfraredDevice7 |/ R' t4 ~) [9 \! x
  Win32_IRQResource
. ]5 N% F0 o* e' q3 t   Win32_Keyboard, r& `1 [+ H+ g2 ^8 U% r
  Win32_MemoryArray
1 b0 {6 n3 h6 |+ O- Z/ S   Win32_MemoryDevice
# `) l2 M' X8 r) R( h   Win32_MotherboardDevice
. Z" u% P, ^$ H* J* a   Win32_NetworkAdapter/ m) ?+ G3 k! O% V. v0 j
  Win32_NetworkAdapterConfiguration
( @/ H* a! m0 a1 A9 x+ {   Win32_OnBoardDevice' q: y  A  \) _
  Win32_ParallelPort
  _' ?; Y& q& h   Win32_PCMCIAController/ N7 s. v/ C" m- Z) Y
  Win32_PhysicalMemory) n4 Q1 X* Y/ G3 O
  Win32_PhysicalMemoryArray
5 U3 [* R  r2 A- V+ m/ [. e2 O   Win32_PnPEntity% [4 O# A2 w3 P  {* f
  Win32_PointingDevice7 s- B1 s" E; S  p6 K6 E
  Win32_PortableBattery
0 ~% G! s& ]& y9 d* u   Win32_PortConnector
) ?/ c$ E6 H* b& K, K& e4 f( ~# Q   Win32_PortResource
& c) q% r! _9 U. G/ q   Win32_POTSModem) `8 g9 z( j( w2 V
  Win32_PowerManagementEvent6 t; _' G. l- p! M% g4 e
  Win32_Printer
) A, G* G: |5 G. T+ S   Win32_PrinterConfiguration. z4 y/ C+ |( o. Z5 L
  Win32_PrintJob
1 ~9 q2 q5 R! @7 ]! K" b5 k   Win32_Processor
4 v/ ?" u8 O, C; A5 `   Win32_Refrigeration: ?( s7 ~: T4 `% U  k( o
  Win32_SerialPort* W6 H4 D3 c  J+ h) Z
  Win32_SerialPortConfiguration4 b" ]5 B6 p; c8 |
  Win32_SMBIOSMemory, Q) x1 L! o0 g7 a7 R& z3 m0 W
  Win32_SoundDevice8 z/ a  j$ S: l7 i. L
  Win32_SystemEnclosure- e8 n6 `! C9 H. Q2 g
  Win32_SystemMemoryResource
7 O' q1 s0 {& a   Win32_SystemSlot
/ W' c' }* j8 Q) }; B( `   Win32_TapeDrive
  D+ X/ q# \* ?6 T' p   Win32_TemperatureProbe
  h. j. X, }0 z% L: R$ F   Win32_UninterruptiblePowerSupply
6 V% @% R  B9 G3 U1 B5 h# Q& a   Win32_USBController
$ V& y4 Y7 d; G/ F/ |/ g, [, o! V5 ?9 c   Win32_VideoConfiguration$ R  X5 g* d4 n, ^- ?
  Win32_VideoController
0 t5 U* y0 m$ b( p' ^1 u# l   Win32_VoltageProbe
& _7 I& B! Z* y6 M3 A( I
+ o. t, u3 _& `& l- g: P2 u2 f以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-1 22:21 , Processed in 0.018771 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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