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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)0 y& z1 |0 g" i, N0 M
/ C$ _( p+ h  z! c8 G0 |/ |

) ]2 U& i" H" ~8 x8 r5 U--------------------------------------------------------------------------------
0 Q0 O( U0 @7 S/ W$ p' h& xWMI: Windows Management Instrumentation (Windows 管理工具)
/ E5 X. ?8 o$ N: H& I   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
3 O8 H4 b. Q' M   利用这个工具可以管理本地或客户端系统中几乎所有的信息。
7 h! z; G2 T% O3 O" ]   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 # }5 W! ^4 {0 H1 k* e

, t# s. }: i% ^. w1 n' r9 q" M6 \--------------------------------------------------------------------------------$ F; H' s! M# |6 }, I
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
, t' |  @8 b5 b1 l- I$ _* B6 @: H
5 A, ~8 w% C( d: a, H; s# @--------------------------------------------------------------------------------: ]1 m4 j4 D2 r& t* e
① 初始化 COM 接口:1 n: G" k0 d! g5 h
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
( K# Z/ C8 j, P% _; O) {   这两个函数在 #include <comdef.h> 里面定义。
$ ]$ @2 {( p: e$ Y2 q: Y# v# l3 u1 j" O( h+ p3 p
② 获取访问 WMI 权限:
; ?0 A8 n' }/ ~% G% \! D1 E3 T   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);5 H9 ?3 I+ M( f2 A+ S4 n: W+ x6 e0 r
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。& p" p0 ?% g( m% B. B
- x) I5 Z( s! D. A1 Y/ _* w
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
. x9 o0 L, j3 ]. `6 ^   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
# W; y" E& s6 e- k9 y2 J0 D  A' ~/ O: U! H; Q3 b5 S
void GetWmiInfo(TStrings *lpList, WideString wsClass)3 _4 t+ l5 A% C2 X, E' Z
{
" B( K$ Y; \& G& W5 K  IWbemLocator *pWbemLocator = NULL;
; V1 y. C9 d, s, `3 V8 r2 r2 x2 ]  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
  q) z. g! [% i! w) a   {
" l) P2 k+ Y6 }4 u1 a" d9 q     IWbemServices *pWbemServices = NULL;
8 f3 Y. b6 g+ n& T     WideString wsNamespace = (L"root\\cimv2");
% Y5 \) K5 q9 a2 a8 |' A& i     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
- A/ U# g( [) g1 c6 P      {
, f% M+ }( ?* T. U/ M( E        IEnumWbemClassObject *pEnumClassObject = NULL;" @: Q7 T& y1 I' N" C
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;$ p) y9 t# q, e
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
% f2 }1 q5 I4 ~3 S         {" U9 I* u/ d) Z5 ~
          IWbemClassObject *pClassObject = NULL;
. `) d, m- v: L& `! R! _: d) W1 \           ULONG uCount = 1, uReturned;
; J3 m1 i# n6 L2 W; @/ z           if(pEnumClassObject->Reset() == S_OK)
& D* Z- C6 K- W- {0 n! @  Z            {/ h1 V$ U- J: `& O
             int iEnumIdx = 0;8 D  A6 p6 z9 n# W# {
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
, ~$ @0 }4 _! y9 H/ K. g7 C               {
( ^' H, F/ j/ T* Y8 }                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");5 }: x" g- h* i1 {$ V

% x5 e/ I( W. ^5 O' g. f9 e                 SAFEARRAY *pvNames = NULL;
+ E  [) q4 N& _' w$ y# S8 q# T  h8 E: l                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
2 B4 L2 E! K+ L                  {
' p$ j- P' M/ o- I                    long vbl, vbu;) M. G, F( j4 o' g% f; I
                   SafeArrayGetLBound(pvNames, 1, &vbl);
3 p1 s& w( P" T" y                    SafeArrayGetUBound(pvNames, 1, &vbu);
1 N3 R7 ?4 z' x4 {3 v0 C9 T  ~                    for(long idx=vbl; idx<=vbu; idx++)
+ \2 O1 C% A( A1 r. Q  t+ p                     {
% u8 i7 h1 B4 [( T                       long aidx = idx;" c7 x  l; v0 d! s
                      wchar_t *wsName = 0;
7 ^9 @! q; F( L6 o                       VARIANT vValue;7 L0 T8 Z! j6 m7 f- D  K
                      VariantInit(&vValue);
8 v% r3 t( ~. i' }6 q                       SafeArrayGetElement(pvNames, &aidx, &wsName);
# I) d+ o; q# A7 W7 ?% G. p/ Z& M
                      BSTR bs = SysAllocString(wsName);
3 h- D4 \0 l; t) }9 r' k$ ]6 b                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
" O+ u5 r' Y& o: e. l                       SysFreeString(bs);
- a/ S4 Q0 s& e. u4 W: O. A- \
$ b) M( @3 Q, _* _" ~& o) Q  w                       if(hRes == S_OK)
9 `! _9 P3 g: K& b# H+ E                        {
& r/ n( I, D  y7 ]# S. B4 L( s! a                          AnsiString s;" O" Y4 ^' `; E& }
                         Variant v = *(Variant*)&vValue;5 J6 l% c* L2 y6 V  s$ b
                         if(v.IsArray())3 ?/ ?: Y6 _: p& F" p$ A; V
                          {
" ?; V; g6 e# C% q0 k8 O  n" o( I" z                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
( t: C3 i- f' u9 R                              {* p4 q4 P7 I  @6 H" a3 V" z
                               Variant a = v.GetElement(i);
' H+ v; O- f- f3 e5 P& V                                if(!s.IsEmpty())2 G, k- s& V! \+ G6 V7 Z* W
                                 s+=", ";
- O( M4 X: `& h                                s+=VarToStr(a);! S" Y! m( E* l$ y0 G) B+ v
                             }
1 m+ M3 T# e- r1 t0 w. E3 d! s( a/ ?0 W                           }
5 B* R5 Q7 F1 L0 V/ \                          else
3 z1 F3 d6 A- c0 ?5 X                           {1 p4 G3 n7 [; |% b7 M. y# [, |/ z6 @
                            s = VarToStr(v);
% s6 b2 ^' G) _# g                           }. |0 k$ X" t2 p
                         lpList->Add(AnsiString(wsName)+"="+s);
% b6 o1 k! r% R: E                        }' [$ u3 O/ h6 y7 `1 g
6 y; c- @, X5 V+ K
                      VariantClear(&vValue);. q! K% r1 U: `/ }0 r2 C$ a# R" X
                      SysFreeString(wsName);
- ?- L( [# F9 ?4 T, `# A                     }
! O. V( H$ i8 }% R; m4 e; `                  }3 \7 P& W4 h; h+ Y. V
                if(pvNames)SafeArrayDestroy(pvNames);. j1 U  m! K+ H& ]! {2 p- n3 a' U
                iEnumIdx++;) X) M( T7 G* e9 @7 Z
              }
2 O+ O" H" G( S) x0 d            }6 J  a- B3 e8 ?
          if(pClassObject)pClassObject->Release();
8 P  v- y1 B& {6 X6 ]$ q% r, z         }
; {& j& w; ?7 j: @/ E9 [. v: B( z        if(pEnumClassObject)pEnumClassObject->Release();
1 f7 k8 Q/ \: s! Z7 i      }, g; Q3 W* |' G# y5 ^  Y$ K
    if(pWbemServices)pWbemServices->Release();
; j! B% z, u0 l/ M2 v   }4 X7 J6 C+ F* Y4 C) [/ M# p" @
  if(pWbemLocator)pWbemLocator->Release();0 e" k  a% P6 o! k2 V1 K+ x
}. B* W5 B, a* X  \
//---------------------------------------------------------------------------$ R5 ^* N; i6 q8 w
# o/ |9 Y+ r3 ~9 c4 `
// 通过 WIN32_bios 获取 BIOS 信息:
3 b6 [  t! L. M# j5 M: S( _void __fastcall TForm1::Button1Click(TObject *Sender)
% i: L3 \; J4 z4 I5 f{
( F; ^4 O! t  V% I3 _  @    Memo1->Lines->Add("================== [WIN32_bios] =================");0 K' h, O* P: y5 a" \
   GetWmiInfo(Memo1->Lines, "WIN32_bios");4 l' W+ ]  V' m  j: A8 B, }
   Memo1->Lines->Add("");
( q; t3 m% B( C, E' U9 I}. q$ U+ X0 O0 @" e) {7 ^
: }% G0 K4 G) V
--------------------------------------------------------------------------------7 z% M; j, O' L: H
! a1 ]% X& g6 L) c
WMI 可以访问的信息类型有:
3 K4 Z- g* G! E   Win32_1394Controller' ~$ d4 L* ^6 Q! ^7 G; z6 q  w
  Win32_BaseBoard
5 V: W3 T& w0 A, v6 X& [2 ^1 I+ P   Win32_Battery
' T4 }5 T3 a1 C3 F   Win32_BIOS
1 n6 M; P& b% s- V7 J2 T9 ~* `   Win32_Bus9 G7 B- j- X1 @! K; Y) H
  Win32_CacheMemory; _) v1 {$ A8 `' \: M4 }3 u
  Win32_CDROMDrive+ k( w( r( G9 J: m, p' @& C8 @1 @% Y
  Win32_CurrentProbe1 A* M- I! W: G4 ^1 v  `- n4 A
  Win32_DesktopMonitor
2 |2 R- B% g9 t/ L7 W# S- s   Win32_DeviceMemoryAddress% E# b8 A0 |, t
  Win32_DiskDrive& j# S7 ~1 M( c% w( ]
  Win32_DisplayConfiguration, A: Q9 f( E9 W: F- T& ^9 M8 B3 }, r  R
  Win32_DisplayControllerConfiguration
9 K! h  S9 o5 B1 e; q   Win32_DMAChannel" a# F, l; E  D; w- g. n
  Win32_Fan8 J" ?- ]7 g. o! q9 @9 k
  Win32_FloppyController6 Z: |. {& E& |& f- w- i
  Win32_FloppyDrive
4 M4 v& V2 l  D9 U. N  w8 o   Win32_HeatPipe: R: w" A  b" T3 m
  Win32_IDEController
# G9 ]. \9 E) i) Y' C! M; L   Win32_InfraredDevice
7 B2 P$ V: M: g6 g) b4 q   Win32_IRQResource& _' O7 F. Z) X) v
  Win32_Keyboard  \$ }  _3 k; D" M. O
  Win32_MemoryArray/ U  v+ {& V( N; |$ \
  Win32_MemoryDevice3 M) ~. _- g# _( J" l6 A$ [- C
  Win32_MotherboardDevice
% p6 C# ]2 g1 {6 @   Win32_NetworkAdapter
8 u/ l2 c" `$ C2 }/ C9 g   Win32_NetworkAdapterConfiguration
% W  @$ O- ^7 K$ S9 F4 n" r* d   Win32_OnBoardDevice
! z. q, U. i- G7 j  @3 x8 O   Win32_ParallelPort3 E+ o! `: ^2 P9 A8 g% @
  Win32_PCMCIAController
0 G) ?4 h6 K; G$ t) M4 w) u+ a3 S   Win32_PhysicalMemory
7 i3 q  t* `1 ^. J% d   Win32_PhysicalMemoryArray
$ O, C1 N4 \( |1 e, }' q: P% E; O   Win32_PnPEntity
6 V( F0 h$ `% k) f8 J   Win32_PointingDevice
) S$ P# F8 C7 u# g   Win32_PortableBattery
8 M. H# C# m* ^. A, j/ U' V1 h   Win32_PortConnector
  {( p/ i6 b, w9 i3 n! y   Win32_PortResource
- V! s, b  F: n) B, x% X# l7 v# [   Win32_POTSModem0 \" i7 d7 S. E  e8 g3 e! W0 t" ?
  Win32_PowerManagementEvent9 |& u% g. B$ a- s; B; Z
  Win32_Printer
- c( m3 p& g* ^* ]. h   Win32_PrinterConfiguration
; F2 r  [! u9 T9 ~( v- h( f   Win32_PrintJob
1 C4 k- p* [7 W& p3 Y- X   Win32_Processor; t+ M3 r5 L- U& r6 E! q1 \' S( Z+ n8 ]
  Win32_Refrigeration
; ^0 A+ |7 @. l/ x3 m   Win32_SerialPort# M9 z, p, c/ B* k  N) y! k9 d
  Win32_SerialPortConfiguration
  l/ @! m7 p' d$ e   Win32_SMBIOSMemory2 g: y. Q  A) B5 [( I
  Win32_SoundDevice# ]" n3 M4 U& O3 v
  Win32_SystemEnclosure
2 e2 U  V2 w2 J6 Z6 @   Win32_SystemMemoryResource
3 N6 ^9 I/ [% O9 K( i7 c% J   Win32_SystemSlot
1 A% ?. A. q9 r; H8 f/ D9 c   Win32_TapeDrive
0 c" a; _+ f+ F3 @) v* s   Win32_TemperatureProbe
/ D/ I" k9 L$ N. m$ E   Win32_UninterruptiblePowerSupply
8 h3 {/ f0 O/ v3 ~+ a   Win32_USBController5 [3 i) i. h- l7 a+ x! }4 B: `
  Win32_VideoConfiguration
& @( {$ M* X/ T* I9 r   Win32_VideoController
) J8 `& A; a  t8 F  Y   Win32_VoltageProbe: Y( Y" F1 V5 `# E. d4 B2 Q
8 ]; i4 Y5 W# {( |6 w: D# t
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-19 07:27 , Processed in 0.021234 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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