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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
! S# p1 I# q; l* {3 }
$ `2 a6 s) a" ]4 |, b' Y% F$ Q0 F  {6 k
--------------------------------------------------------------------------------
3 N( O( X" `8 X# WWMI: Windows Management Instrumentation (Windows 管理工具); a& Q4 b) Y$ ~1 L  R
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
9 ~- A) U* L! W9 J3 w( s   利用这个工具可以管理本地或客户端系统中几乎所有的信息。/ j' f" A+ f& t( F0 t
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
, j$ Q! D3 B; _; A% b( l, V2 e. V: [* c4 {8 l  F& U5 B
--------------------------------------------------------------------------------5 v7 J1 Q' F: B/ B/ Q" N
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
0 @! W9 }; K. E. P# j( N
8 j" h4 _$ A+ b5 W# Q7 w--------------------------------------------------------------------------------
2 D8 Z9 T2 {  u0 K+ Y① 初始化 COM 接口:, Q; O. m$ e/ D! c9 l+ |
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
1 {( R8 r. m0 \$ k, z2 Z* K- w   这两个函数在 #include <comdef.h> 里面定义。
( s# v0 {2 s1 |, H& I9 Q5 w& ~2 H
② 获取访问 WMI 权限:
" C4 w0 Q: r- A. j0 u   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);1 X9 q7 j  H9 B$ j7 D' k, }
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。: e  [2 g. a0 Q; d7 d
* U- i4 s% a7 Y4 Q! F
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
5 M- g9 |- q/ t; r: \1 m4 a   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。; X: C, A  t! B

; d0 |- f3 D6 m; }2 ^  `void GetWmiInfo(TStrings *lpList, WideString wsClass)
& }3 p0 V1 `  Y) Z{& f' m# ?" F2 {5 h( J  P
  IWbemLocator *pWbemLocator = NULL;
" [0 I) Y+ Q# P+ R3 I4 @, u  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)' t0 M+ V  r( A/ L9 Z5 y
  {7 X( L/ f! Z' m0 ?2 _' s2 k
    IWbemServices *pWbemServices = NULL;, `: b1 \( J! P2 i8 g2 D
    WideString wsNamespace = (L"root\\cimv2");; Q. M' B$ k4 i
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
* a4 E* E6 }- L  l/ H: d% }      {. [7 q% M# A' }7 I$ X7 l
       IEnumWbemClassObject *pEnumClassObject = NULL;
3 w4 A; X: o5 T& Z& w) F        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
  j4 m1 c% }- ~6 G% L1 f0 [        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)8 b( Y# ?1 O3 G3 q% u: y" r) x
        {! H) `4 a8 A; d6 S1 R! i
          IWbemClassObject *pClassObject = NULL;* n' I$ S9 O! g+ D
          ULONG uCount = 1, uReturned;: J6 r4 W( [9 _5 r7 A  ]8 ~
          if(pEnumClassObject->Reset() == S_OK)* a+ M2 b  _3 Q5 h
           {1 ~, j8 d5 q" F& y2 a4 v
             int iEnumIdx = 0;; ~' [8 R6 |: ?5 @, O
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
$ I2 W+ F( N! T; T% ]0 |               {6 c5 s- f7 {. x3 e
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");" R  ?) e6 U& [; R+ I/ v

- H4 ~- Q% [% A' }$ j' O! N0 h                 SAFEARRAY *pvNames = NULL;
& o8 k7 B& v5 l, P0 j" U                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)- [5 \7 N( g# s9 e
                 {
- D% O' w' w$ k' Q+ b& x% p: a                    long vbl, vbu;
9 {1 i# v/ m! b; g4 T                    SafeArrayGetLBound(pvNames, 1, &vbl);
' n1 w) b0 U6 D                    SafeArrayGetUBound(pvNames, 1, &vbu);
% N! @- e" d+ y% r# ^" s9 L8 e                    for(long idx=vbl; idx<=vbu; idx++)
% ~& A0 {' F/ t/ V0 D  r                     {+ ?, i! m6 M9 Z3 C# ]0 I
                      long aidx = idx;
2 k- Z* U' v( i# K                       wchar_t *wsName = 0;' v! Q# i( d9 Q0 b; P
                      VARIANT vValue;0 H: l0 f) m  N7 m
                      VariantInit(&vValue);& v# g' {0 B) Q5 ~9 l- n
                      SafeArrayGetElement(pvNames, &aidx, &wsName);9 m5 L2 P1 O; p. K6 t# v

1 M3 g4 b: y. _$ F7 q. n! _& q. K                       BSTR bs = SysAllocString(wsName);
! b& ^) v! j) U" b6 s                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);$ N0 e( B4 @0 V# w# M; K% A6 y0 Y
                      SysFreeString(bs);
$ C3 j2 |# P: s+ C& D  g  z7 g
$ @/ s, W8 e3 s0 t# s0 e4 r  B- _2 m2 W                       if(hRes == S_OK)
, h% n6 Y: a& Y2 V0 J# R                        {
7 U- Y2 c( a! }9 W# Y; `1 c                          AnsiString s;
  Q0 s" a( }4 S( G                          Variant v = *(Variant*)&vValue;& h2 v4 H1 ?( a4 }6 B: f
                         if(v.IsArray())
0 [2 ~% B: v  N7 L' A3 y                           {
' d3 `8 {# m* M  t& e                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
1 p  u1 n' ^: `+ _                              {# E& K0 `! ?: @) u* p: Z
                               Variant a = v.GetElement(i);+ O. P* ^3 n/ F2 I
                               if(!s.IsEmpty())
2 x2 S. [: d+ c4 X# G                                  s+=", ";& @- c' p& B7 A0 |; l
                               s+=VarToStr(a);+ I4 B! \1 s4 W: Q
                             }. C3 A! w+ `  O5 g4 B; y
                          }! J( W* w# n( y; `7 h" S
                         else
7 l! z, E( ]% C                           {$ F0 \! O' C7 @5 S1 f$ [- K2 o
                            s = VarToStr(v);
& E! {+ ?1 G+ ^                           }
4 Q2 q* Y# o5 p  B% U/ Q  Z% Q                          lpList->Add(AnsiString(wsName)+"="+s);( P+ A; a% ]4 @) S  ~
                       }% Z4 O8 r3 W; @2 u
9 y7 \: Y4 \) F2 s
                      VariantClear(&vValue);. K& s7 k7 P4 o6 R0 z) f1 }
                      SysFreeString(wsName);! e9 q- e9 L  U! s; c# V
                    }
3 L! i4 K3 @, L$ x                  }5 X  ]* s3 t: z! ^
                if(pvNames)SafeArrayDestroy(pvNames);- ^. o  o/ n, y- T6 ]
                iEnumIdx++;
, Q6 d8 {0 s( k               }+ V4 [" j% ^- ^; j
           }
; d9 z+ X2 `  s! Z           if(pClassObject)pClassObject->Release();
! P: O2 k( S7 B: n2 a3 c! c. R" {         }
1 u: _* ~1 U: ?/ W# s        if(pEnumClassObject)pEnumClassObject->Release();: q  s, o$ V/ v8 T% F4 B; p& n4 i7 d2 k
     }
& j& Q# m/ r; _% F$ V5 v1 h; L     if(pWbemServices)pWbemServices->Release();
) z2 v0 |6 n/ N6 a6 ~   }/ {# s0 B( D9 G, A
  if(pWbemLocator)pWbemLocator->Release();
0 p; G1 J8 t0 L' i6 Z* @- I}! A4 N' Z- M2 ^9 U
//---------------------------------------------------------------------------+ T7 b  o" m% N
8 M/ G7 Y. S9 ?; @! y" f$ s
// 通过 WIN32_bios 获取 BIOS 信息:2 S+ g) R) s" J3 E# G
void __fastcall TForm1::Button1Click(TObject *Sender)5 H+ `+ q. S7 p! [* n$ ?: e
{3 V* J: S' H! V0 j9 J8 o% u; d
   Memo1->Lines->Add("================== [WIN32_bios] =================");7 x& M! a% m5 K- S5 ?4 M, e2 l
   GetWmiInfo(Memo1->Lines, "WIN32_bios");/ f+ M$ E# {6 R# Y+ S
   Memo1->Lines->Add("");2 @1 S6 b4 E5 Y9 j6 n
}
. p" C: G' E- l; D& k7 u0 n
+ G# A8 a* F# h9 B--------------------------------------------------------------------------------" @' H1 f' `/ v2 W7 m, Y% ]2 X

" s1 p; S9 q* R( [WMI 可以访问的信息类型有:
% b: j& I. u6 d( ?8 u2 J( ~   Win32_1394Controller
" m. x; d5 O2 \  F- Z   Win32_BaseBoard" D# W! o6 u' x- Y9 W: r# t, K
  Win32_Battery
9 |) G7 y6 r6 u8 {2 }   Win32_BIOS+ P) s- i( z5 t9 J* O
  Win32_Bus4 V  M* y3 s7 g( D5 [: Q$ [* M! ]" C
  Win32_CacheMemory1 G  A7 W4 {' w! f) j  l5 b
  Win32_CDROMDrive
3 l8 T) G% P0 Q% i' ^# y! f* @   Win32_CurrentProbe0 o7 L. Q) `" l/ x2 ~8 V, k
  Win32_DesktopMonitor
; l& o0 ~$ ^  x  O- Q   Win32_DeviceMemoryAddress6 f0 W# ?5 g" M, Q# l
  Win32_DiskDrive* `+ O! y2 V0 q: i9 ?: s
  Win32_DisplayConfiguration
3 d: B  g+ ]( f* P) Z   Win32_DisplayControllerConfiguration$ l6 w2 W4 i3 X2 v  V
  Win32_DMAChannel
  M/ Q/ R" c- E   Win32_Fan
. g2 W) i, N' A- T2 b   Win32_FloppyController
. ]) v% u1 f: a   Win32_FloppyDrive' _3 A& d' j& o  m3 |, @
  Win32_HeatPipe
# z  ^2 p  D" |0 o0 Y   Win32_IDEController
8 f2 [( K# q1 U+ q' \' x6 p7 Y   Win32_InfraredDevice
) [2 z6 A) M, W   Win32_IRQResource
) y" L: f, v* v8 c% F) V9 E* Y   Win32_Keyboard
- |. J* E4 b% g   Win32_MemoryArray( J, f9 q& ~- q' j4 P$ \
  Win32_MemoryDevice" h) _4 s( U7 w1 N
  Win32_MotherboardDevice
  f3 Y# ?  ]' k   Win32_NetworkAdapter  E0 ?" Q+ _7 M) M8 s( C' l. o4 \5 U
  Win32_NetworkAdapterConfiguration1 g7 P, H3 }  [
  Win32_OnBoardDevice% [4 L/ ~: e. d! z
  Win32_ParallelPort& d& d! P! B8 x
  Win32_PCMCIAController5 L+ h2 g/ ~! T. O! O
  Win32_PhysicalMemory9 k/ n4 i' O: z/ I$ t
  Win32_PhysicalMemoryArray- _5 i$ P' j/ z/ D9 L
  Win32_PnPEntity- f8 Q: k1 x3 b$ m
  Win32_PointingDevice
6 ~9 T. Y6 ?! B+ `   Win32_PortableBattery  @9 D5 U0 W* Y* [* k( T' z. q4 {
  Win32_PortConnector
9 P2 Z. Q. m1 j0 e$ K' f   Win32_PortResource
! F3 J6 `7 K2 ^0 e9 R  g   Win32_POTSModem
0 [7 g  R+ n2 W; U% G3 d1 b7 l9 O6 A- D   Win32_PowerManagementEvent+ |4 M0 ?- \+ W0 F, R: Q
  Win32_Printer
* I. F8 {7 k. h6 J   Win32_PrinterConfiguration& |) b! n3 m6 _# E* A
  Win32_PrintJob1 u/ u# b0 i8 ~/ k; _6 b9 i! w
  Win32_Processor
' C+ V; t5 V5 t  |, c. {. |. t5 q. i   Win32_Refrigeration
- W; Q/ d2 l/ u! Q$ i2 \4 l   Win32_SerialPort
% A- m" R" a$ ?2 b   Win32_SerialPortConfiguration
) b. i; l+ n4 }0 j4 D0 Z- ]   Win32_SMBIOSMemory. ]# }5 x: S; Y. I$ t
  Win32_SoundDevice
( c% H4 l! Z& B( d. Y   Win32_SystemEnclosure5 X2 Z% r6 G7 j3 d
  Win32_SystemMemoryResource
2 i0 V2 U: r3 c; X. g   Win32_SystemSlot% o+ H3 h8 T+ s4 K* m8 G& }* @
  Win32_TapeDrive% P  c# C. r/ k# e
  Win32_TemperatureProbe
2 M' H0 K! H' }/ s4 I) x( R   Win32_UninterruptiblePowerSupply$ |# {- d# ]9 g
  Win32_USBController+ T  B$ D$ \8 J
  Win32_VideoConfiguration
2 B$ L- j' [+ w; q/ [0 e   Win32_VideoController
+ e. S! V$ O5 V! ]' y# h0 s1 O7 v. k   Win32_VoltageProbe" R$ X" ^# g2 _2 ]: _
8 \$ L) Q2 p4 f
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-29 04:46 , Processed in 0.020473 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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