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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
' C4 j" o) {% o0 t" T. k, V( y4 S5 j

! H2 D9 [$ m7 R" X--------------------------------------------------------------------------------) `  ^& \2 X( d% u! Z
WMI: Windows Management Instrumentation (Windows 管理工具)
: V# |2 T* [$ T   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 / E  g( I, y' d& I" b1 [
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。" K# Q0 o0 l" p1 i
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 & }2 |: @4 @  ]" y1 x% g

4 x2 \- U- |1 e7 t, y" e# v  I--------------------------------------------------------------------------------
0 {6 i/ a  I+ f) HBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面6 q; B8 S; I) V# _0 `. r# j
. t$ B* C% I2 c7 R# T) P8 J3 {
--------------------------------------------------------------------------------( C& I3 `7 f2 m2 D/ I" P5 W. H, X
① 初始化 COM 接口:
1 P, Z0 V8 f9 c. S8 H# E8 d   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。& u" \2 `8 G# R  ]" N
  这两个函数在 #include <comdef.h> 里面定义。
( ^0 J: S) C& h3 m! [& _7 c0 t8 G% v& }5 h3 ^  v
② 获取访问 WMI 权限:
8 s3 L2 o3 c7 S8 e' C" `, J   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
3 r7 U; q# W1 u7 F   如果这个函数返回 S_OK 获取权限成功, 否则为失败。
( M. m" S6 ]! H0 G3 h! \6 o; H1 Y6 f: ]# G" y/ h
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
3 s" P0 p+ F' a* D8 a8 r   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
7 |* [$ t8 D% x3 l" F
$ U1 j9 e* q7 k5 Y' G; dvoid GetWmiInfo(TStrings *lpList, WideString wsClass)7 ~4 W- J6 ~4 w/ X+ h" r: H  b; k
{
) z( v0 B7 Z, Q: c6 ~) H  IWbemLocator *pWbemLocator = NULL;: h7 k2 G. ~3 J
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)0 x; [( e8 e  c1 B+ D6 Q
  {
+ w; l" n: q5 E/ ~. o; \     IWbemServices *pWbemServices = NULL;% J& p; |3 b- H/ F: [5 q& U7 z3 j
    WideString wsNamespace = (L"root\\cimv2");+ s1 N  E8 `' L; _. \4 j6 l* T
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
2 d, F  D: @9 `6 F) ^1 S      {: ~3 @$ r5 r# W
       IEnumWbemClassObject *pEnumClassObject = NULL;! g: v3 O1 e6 r# b
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;$ R2 N' v8 w( M# S8 [3 N* {
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)6 \, p6 c! c& N2 b2 t
        {
5 f% |: G3 j) Y; p1 A( u$ i           IWbemClassObject *pClassObject = NULL;
4 r8 o* J. K) p& ^6 i0 P/ ]           ULONG uCount = 1, uReturned;
! m3 i. `9 G: \1 {! c           if(pEnumClassObject->Reset() == S_OK)) \2 L2 Y* J  M1 x6 K; A. C: t
           {/ p2 {+ q7 I0 {& K
             int iEnumIdx = 0;
* B4 [5 s: w) [9 D              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
& O! J1 Q  z- L- r: a( u% I               {
+ E  c" m1 b6 R; N/ B6 T                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
( X; T& k7 V, I& y% G8 ]
+ P' U+ s& R/ v                 SAFEARRAY *pvNames = NULL;6 w! K6 u/ b( R  N- m
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
3 |" g: J. V; ~1 G- a1 M% J                  {
  u- L! a* g$ O4 o7 e                    long vbl, vbu;
1 m( ]" ~/ W) s. N2 U                    SafeArrayGetLBound(pvNames, 1, &vbl);: A! v$ B  n) Y& X& d
                   SafeArrayGetUBound(pvNames, 1, &vbu);
4 A& M$ @* W( D2 u7 U; N                    for(long idx=vbl; idx<=vbu; idx++)6 t! ^0 y; N) u$ O( `
                    {
+ \8 _6 G5 _, W' b* i+ @                       long aidx = idx;. D+ l9 L2 ^8 v# K+ P( k
                      wchar_t *wsName = 0;! P* M# Y' S8 l+ K+ i( _/ W
                      VARIANT vValue;5 X$ E% i5 X( S' U
                      VariantInit(&vValue);- R8 p, L' V) N. g, L* v! T+ n
                      SafeArrayGetElement(pvNames, &aidx, &wsName);% w4 A( l/ C, ~' x, K4 n
/ `5 I6 h6 o0 z0 I1 n# ~
                      BSTR bs = SysAllocString(wsName);
7 D8 Z& Y/ q7 c8 y$ m+ B; {& s* U                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);! a5 {6 Z- y  |* A$ P7 J
                      SysFreeString(bs);) r# P& a: `- j+ \

' \4 @1 Q/ x* ~* j% P; }                       if(hRes == S_OK)! g, E' j5 n" t0 K6 E
                       {
# [  A" I' W; L1 J                          AnsiString s;, P" H! A5 S" `% q# I
                         Variant v = *(Variant*)&vValue;. ]2 c6 T, q: J* Y( `$ f
                         if(v.IsArray())) L1 h, V  |0 f5 p: g1 d3 v
                          {" O, r9 b! N# u% |' \4 t+ L' O* A8 D
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
1 n. l4 U- G4 D3 v                              {
* M" G7 w7 m( m: w                                Variant a = v.GetElement(i);: e$ S/ d' H; B% p9 k7 d
                               if(!s.IsEmpty())) c1 ?+ G& a$ C9 F8 N
                                 s+=", ";
4 O, P0 |1 u- V7 R& g, o7 |# @3 l                                s+=VarToStr(a);
0 C1 h- n- X4 I6 K, L                              }% a, ^7 ^% ?3 p# ~/ n9 q, L+ O
                          }
" ]# G* q9 i4 ]) s                          else0 E1 Y+ U; @% g' s. E
                          {, q" `/ h5 l; R( X, D! {, h
                            s = VarToStr(v);* m3 o0 I# F1 O  l: f
                          }
3 [2 s: l, L  }8 M9 a  l                          lpList->Add(AnsiString(wsName)+"="+s);
' N6 N* F& f; Q# i# r6 ]3 z) d                        }$ o3 J6 c' k, v4 M; U& t( A# W
: ^; v: S* P6 r- M( X, ^0 V- e
                      VariantClear(&vValue);( O& o2 J% ~7 i. }- \0 ^
                      SysFreeString(wsName);1 k, P+ t7 M8 t
                    }; u; a1 E( _+ P% K3 \- j. H, D
                 }0 H* x7 j) ^3 s* I6 k
                if(pvNames)SafeArrayDestroy(pvNames);' R* v! y4 H3 L8 }1 Z
                iEnumIdx++;
1 F; [, L, e3 i# L3 {$ Q               }4 h1 o8 x- |# s* d. e9 Q8 X; B6 E
           }
6 H2 T! m$ Y$ b           if(pClassObject)pClassObject->Release();3 [7 M0 f, `( o5 `# e: e/ F* L( u
        }1 u4 C4 }; C! h5 @
       if(pEnumClassObject)pEnumClassObject->Release();
' t' s" v/ I- k- L3 M1 g# z8 ~      }. ^3 G+ [( c  l' E( `" C
    if(pWbemServices)pWbemServices->Release();
3 s' v) P1 |6 t0 y& W5 k   }0 u! ]- J, v; Q( d
  if(pWbemLocator)pWbemLocator->Release();
) [& C5 l7 p  h}# t3 x. a: F- U" G+ [
//---------------------------------------------------------------------------
0 E+ x7 ?% c* h& Q* H$ i; d  s: B9 U
9 Y' a- b9 u7 a// 通过 WIN32_bios 获取 BIOS 信息:& a- {/ a7 I9 k" }  B
void __fastcall TForm1::Button1Click(TObject *Sender)
! @, m3 }- ?+ A- X  M& n- ^{* p# y$ C( U5 A8 b  s5 E4 U
   Memo1->Lines->Add("================== [WIN32_bios] =================");. Z) Z) a  Z$ t3 {# w' f9 ]3 T( {
   GetWmiInfo(Memo1->Lines, "WIN32_bios");4 X+ j+ Y) J6 ?3 I: O) F9 ^0 a
   Memo1->Lines->Add("");
5 K# _4 `. D3 `- x- |/ o- |}( H) Y9 y: l0 R0 V' Z: @9 P6 Z

! x- g% s' f6 ^) l, ]) q0 c--------------------------------------------------------------------------------5 U' f. F7 N* e
& H* C. r. j& A8 J' o+ v
WMI 可以访问的信息类型有:
' D  E3 `% D, d* H' X6 E+ z6 T; g- w   Win32_1394Controller9 a. e- ?+ N3 V2 g
  Win32_BaseBoard( D" Z4 u. [# ]! \! x" j: ]
  Win32_Battery1 C/ }; {( `1 [7 N) i" i
  Win32_BIOS, g. _2 O& E' ?9 v
  Win32_Bus
: i7 k( S, e6 K3 E  |) c: t   Win32_CacheMemory
2 f1 b6 B% r, h5 U   Win32_CDROMDrive" Q8 p3 P) e" ^; q5 Z: y
  Win32_CurrentProbe5 v, O  P# H& X$ a
  Win32_DesktopMonitor
+ n$ c% G7 {5 p$ I/ d! O2 N( O   Win32_DeviceMemoryAddress
! i- D& V- B, }2 s& I" K   Win32_DiskDrive, ^" i5 y) T0 s$ x# E% L! `
  Win32_DisplayConfiguration
# H, A. h' y1 @   Win32_DisplayControllerConfiguration
6 n: L7 q1 g) K& U* {   Win32_DMAChannel: c6 Q# G, \( m, ~. J
  Win32_Fan
  M1 @* x* |9 m2 j* m/ l- \   Win32_FloppyController% i! }/ p( \& i$ P' k
  Win32_FloppyDrive
* o6 c0 M& R7 z   Win32_HeatPipe6 R& u  B4 y" ~# Q" V6 S
  Win32_IDEController% S8 d( S1 }( d- z
  Win32_InfraredDevice0 L3 ?* ?6 C6 P0 q! L! s; m5 E0 h/ \
  Win32_IRQResource9 y1 j, X9 `, g5 Y4 [
  Win32_Keyboard- Q8 C1 ^7 Y6 B
  Win32_MemoryArray
; F; ?5 t5 b, t$ C! ^, Z- H: v2 r0 S$ w% D   Win32_MemoryDevice
8 s, _4 K0 _! m8 r  G3 o0 w# x   Win32_MotherboardDevice
" ^+ `0 R- \1 F9 _1 h   Win32_NetworkAdapter
* V; _: X, j$ L# y# w0 N   Win32_NetworkAdapterConfiguration
$ z9 e( D0 O1 A& ~4 z# Q7 }   Win32_OnBoardDevice
# U& b. S- _' a- r   Win32_ParallelPort
# |" }' J  \% y2 h   Win32_PCMCIAController
' |  B) \, a( T   Win32_PhysicalMemory" O) u% d. M* s8 z% j0 i+ c; o/ v
  Win32_PhysicalMemoryArray' l" S. g5 M7 P4 ]" E6 s
  Win32_PnPEntity" [' s1 Z9 n: F- C4 D( ^. R9 X( j$ T
  Win32_PointingDevice% `( V! G8 n' Y, D  q" o
  Win32_PortableBattery
- ~& p& G# t9 T# a# C6 \- a   Win32_PortConnector
7 W% t$ d  v7 \% _1 C   Win32_PortResource/ f/ Y* K; J8 \" y  h! Y' j
  Win32_POTSModem
# t* J5 y/ k, ~5 V( X. s; J, p( C   Win32_PowerManagementEvent
3 v# e. B0 k, h' E   Win32_Printer
' C6 w( F( c$ n  w# b2 d3 W* v   Win32_PrinterConfiguration+ {- B- Y9 N  g' E% M9 q1 [
  Win32_PrintJob; @! x8 Y3 X, Y& [. G
  Win32_Processor* T( D- [  V& B
  Win32_Refrigeration/ G4 |% _$ X; }9 g* G
  Win32_SerialPort1 X( h* c1 K+ q" T
  Win32_SerialPortConfiguration
( ~( K9 |9 |, v/ ^1 }6 ~/ I   Win32_SMBIOSMemory
; r1 Y9 k( C3 E* o# H9 c   Win32_SoundDevice
& P4 `. x. d' \# E0 n& Z   Win32_SystemEnclosure5 P, V2 [; {7 w4 ^" x+ k) t
  Win32_SystemMemoryResource1 O% n! w6 i% G# K2 Z
  Win32_SystemSlot) O( P3 ^1 O/ }6 c
  Win32_TapeDrive- F( @7 a6 c6 e4 Z  X5 w
  Win32_TemperatureProbe
6 }' |- ~- ]  g1 r+ [  g$ s   Win32_UninterruptiblePowerSupply
3 y! }$ L% m+ R" e, N9 w: `. `7 c   Win32_USBController( f8 z/ U: ?' z- a9 Z
  Win32_VideoConfiguration
  `$ i4 _1 K* }, N: k  p- t$ B   Win32_VideoController
) f; ]0 M& B+ K' ~   Win32_VoltageProbe
& I- ~' m8 |1 V
% `& y) ^- M5 `( j- B( I+ ?以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-1 17:31 , Processed in 0.020641 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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