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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
: G1 n2 Z2 m: Y& y) X# z$ R3 B% z2 S% T& v# f; v
% ?9 E% E# b3 D& q9 F" i
--------------------------------------------------------------------------------
! L6 ]' _  S) u2 j3 e8 C& rWMI: Windows Management Instrumentation (Windows 管理工具)$ m& I7 L& y; E  [# c1 x
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 : H  O; }! f5 A* _' Q* Q* R
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。9 `% u; P0 @9 h! s
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
! l% }  x6 Y$ A, Y6 o. ?4 V4 P+ W# `0 |' P
--------------------------------------------------------------------------------
2 G9 v6 L# g# g  z+ UBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
$ A- p$ V3 w' Y; D! u2 M
. b# ?+ l; z% s--------------------------------------------------------------------------------
7 }- k# [4 s5 }# S( V① 初始化 COM 接口:& ~+ h, ^! R! I) [
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。: l5 ~( C1 J  E  B* J  \  _) ]' u. f
  这两个函数在 #include <comdef.h> 里面定义。' R5 E/ Q$ L  v8 q) X

" H1 g( V+ ~( r/ Z, I* N6 ^② 获取访问 WMI 权限:  G2 Z+ w! q7 ?0 v
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);- J: ?/ N7 ^* V" B; O8 u
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。
4 [2 g6 s9 z9 z9 S$ `6 H( G) Y2 b, G8 S  O1 k! t, x9 ?
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
& Z% ?: P. e  \* ~" \- w8 @1 l' d   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。) n" M( J7 r4 Q3 i' C: W* N

' p- g7 v1 M9 L' fvoid GetWmiInfo(TStrings *lpList, WideString wsClass)
' b- Z+ {/ g( R: B5 G# a{
) O+ Q* i3 B2 Q+ B; A  IWbemLocator *pWbemLocator = NULL;0 {) X, t4 }- ^/ s
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)/ y  X3 M$ B& o4 N2 G2 C& @
  {
4 f+ I& q+ m- q$ y& D+ G     IWbemServices *pWbemServices = NULL;
# o; y/ i; E, ?. V" T! s     WideString wsNamespace = (L"root\\cimv2");* @8 n; w: g. V" n: L. G( x
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
. u! \. {6 O3 a      {
9 W6 v+ c; {( r& U( G) f2 [        IEnumWbemClassObject *pEnumClassObject = NULL;
# [& B  r7 S+ N0 i4 k2 p        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
' m) I3 s( }1 c        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
9 N) R# ~* |; e' {+ s$ _- ~         {' O$ d/ T+ T8 g, `' |  m( z* q
          IWbemClassObject *pClassObject = NULL;! y0 |  H; z5 i: E- o
          ULONG uCount = 1, uReturned;! A+ x& A8 ~  V  t8 F9 {. V
          if(pEnumClassObject->Reset() == S_OK)$ p3 Q% H4 E' W! a4 g6 n
           {2 [) M9 {$ J$ r; c0 `0 [# U
             int iEnumIdx = 0;# R) u" B! G* b2 U5 U" s
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
& ~2 r- Y3 }# s; Z               {2 V* y4 e/ m8 v4 c; B
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");1 w1 F( e; V6 I; z; {& }7 K& Y

2 }# @, r% G: p+ }! c& n2 |9 Q# c                 SAFEARRAY *pvNames = NULL;* W5 j5 e. u9 }
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
1 y/ D9 \/ G$ H3 h                  {  [/ u; g$ V# W9 i6 l
                   long vbl, vbu;8 G$ l& l3 i" Q! g
                   SafeArrayGetLBound(pvNames, 1, &vbl);3 U% y; A2 U, A9 h; e8 _3 n* ~% B
                   SafeArrayGetUBound(pvNames, 1, &vbu);* Z+ N+ T- ]: x4 Y! ~  c2 L
                   for(long idx=vbl; idx<=vbu; idx++)1 Z' H0 s4 r9 A' n" q
                    {5 {  q+ e( V8 o: Q) T9 M  i) d6 M
                      long aidx = idx;4 c* E6 X5 t1 h/ f( y
                      wchar_t *wsName = 0;' f/ K& [6 w* f9 n$ y
                      VARIANT vValue;
- c6 X: |! w2 i, {/ j                       VariantInit(&vValue);
+ E! ~  R8 S: A8 ^# s# t                       SafeArrayGetElement(pvNames, &aidx, &wsName);
8 @/ ^; {6 h/ U8 ?% A8 [4 G  g6 _" e1 M- |, u+ u$ z, ]" X
                      BSTR bs = SysAllocString(wsName);/ x0 L9 x/ A0 ?0 q
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);; u2 J9 `0 G& a, ^9 @+ O
                      SysFreeString(bs);5 I) W5 p! p: ]% t3 c

, g, Y7 B! j  |                       if(hRes == S_OK)
4 h0 U* e" J5 l" a" `, J: w                        {
& e5 O1 U6 \0 F: |' O                          AnsiString s;
! N8 k/ h" d3 P5 y; z) N% k                          Variant v = *(Variant*)&vValue;
/ \% b7 B/ w  O; x( d; q- ~                          if(v.IsArray())
3 j4 u& n+ \2 E1 N3 |1 ^                           {
  m9 t3 n0 g* F: h+ _                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)* W9 H! Z% N$ u/ v, k
                             {7 _& }9 m! J8 z, D7 l- M- K
                               Variant a = v.GetElement(i);* a8 |6 ?1 ~5 u
                               if(!s.IsEmpty())
! s  o9 b9 f7 n( {' `4 n9 [                                  s+=", ";
9 s- r; J: f: B# Y6 b9 U1 \' ]                                s+=VarToStr(a);
. B0 w. j8 z  d9 i+ ~4 H8 Y3 k" v6 F                              }
& [: p3 V/ ]& R# x6 o: I% l                           }) x  e; @: f8 t+ W- @9 h
                         else+ J# M2 q  g% X3 ^
                          {1 N( N% x: V) k$ T7 [: c  E! h
                            s = VarToStr(v);
# U- y9 ]$ Z8 v8 f) ~4 z( {                           }
0 m8 c1 Z  k3 G. J* z( ^* o+ y                          lpList->Add(AnsiString(wsName)+"="+s);6 ]; M8 b% A; ~$ W; J  `: z8 K
                       }
2 d/ |' ^. V, o# k) e1 u# H" D' g$ f7 P
                      VariantClear(&vValue);
2 K0 d# T% j8 L; l                       SysFreeString(wsName);* S7 n6 K+ d1 D( b6 u( S
                    }4 k, A  h+ @* C! x- O
                 }4 O. R. U$ o! K5 h, X! _
                if(pvNames)SafeArrayDestroy(pvNames);! A/ i& `6 I$ b# R6 V6 P+ }( z
                iEnumIdx++;& s' N+ L7 P- m
              }
+ e0 r; ~/ S3 k2 r            }
% a9 P8 O/ c/ B& c/ e) v5 N4 W           if(pClassObject)pClassObject->Release();
7 K' a* C4 s7 l" Y* a$ f0 w& ?6 D% F         }
6 g( |$ Q" }! h9 T        if(pEnumClassObject)pEnumClassObject->Release();# x2 v, x3 ?7 K' |" [6 n
     }4 ]5 z; `8 F, z( N
    if(pWbemServices)pWbemServices->Release();
8 M4 j  V+ ~& O9 \) y5 m9 L   }
" C0 ]% b+ C1 ?% b; ?7 C$ g2 W  if(pWbemLocator)pWbemLocator->Release();
0 a9 w& V; ?) o% [; h7 M9 J}( O5 s' V9 P( n5 x/ L
//---------------------------------------------------------------------------# T9 ^8 Y9 f$ o7 W( z

, L! W" s9 ?: Y' |// 通过 WIN32_bios 获取 BIOS 信息:
, x8 R( F& {$ B% y  X* xvoid __fastcall TForm1::Button1Click(TObject *Sender)
* i, D" y0 }. n8 o{
" F1 ]9 d3 N6 h+ M    Memo1->Lines->Add("================== [WIN32_bios] =================");$ c" o% r9 Q: t" L4 G# h
   GetWmiInfo(Memo1->Lines, "WIN32_bios");2 m% H0 ^8 D( k$ ]! e2 {  ]1 p0 v
   Memo1->Lines->Add("");# ^" A, n3 y+ }3 n# e: i. d! m  u* {3 d
}2 {1 H/ j0 u$ B3 \
8 }  l% S. X+ d' d5 I
--------------------------------------------------------------------------------+ g8 J" f8 L7 h- O

3 H4 L7 |4 ^; _2 o# g, T! iWMI 可以访问的信息类型有:# I8 `4 C: P( p" w
  Win32_1394Controller
4 i& m1 l# f! K; d5 ^$ H$ |   Win32_BaseBoard0 Z! ^& J7 u6 W$ P
  Win32_Battery
- H- Z' W) P8 j& z4 H5 q   Win32_BIOS
" J3 F! B- A* D( X& ^  B   Win32_Bus
% A  [# P. \. F& H* [! v   Win32_CacheMemory
3 x1 o: D) a8 m7 T# J3 Z' ?8 b' [% K   Win32_CDROMDrive0 T' x$ V" `- X6 b: V3 p
  Win32_CurrentProbe
3 h! i- V; H) L   Win32_DesktopMonitor
% ?4 i# a' }, a0 F9 L, x, \4 n   Win32_DeviceMemoryAddress0 z7 @5 }' }) l  U0 h  t
  Win32_DiskDrive
, X# _, F2 K5 `3 d2 _   Win32_DisplayConfiguration* L3 \! q( z/ k2 C1 n- i
  Win32_DisplayControllerConfiguration" ?5 m  w7 l& J: _9 K7 r
  Win32_DMAChannel
! M  D9 B9 P' w2 a   Win32_Fan( T0 d; w8 q; b
  Win32_FloppyController& ]! C# ?# K6 w8 @3 o
  Win32_FloppyDrive2 \$ W; v6 M" S2 _1 }
  Win32_HeatPipe$ o" U5 T7 e2 U1 e
  Win32_IDEController" n9 _# Q' }+ W
  Win32_InfraredDevice  K& h3 q6 @+ l/ L1 Y
  Win32_IRQResource4 W5 N( J& C# o$ j3 A
  Win32_Keyboard' r7 }$ `. Y) T" n) m* Z. ~
  Win32_MemoryArray) M  C. [1 K5 F; ^- u
  Win32_MemoryDevice- E0 a: B, E) q5 F
  Win32_MotherboardDevice% O1 Z. ~/ i- W; M+ t
  Win32_NetworkAdapter- k1 N. t$ }( q6 m3 y" {: {
  Win32_NetworkAdapterConfiguration* w3 V* a7 c) P6 S6 b
  Win32_OnBoardDevice
, B  ?5 L3 ~+ a- N   Win32_ParallelPort7 w7 F! e5 D9 G
  Win32_PCMCIAController& J! B5 e# B4 r
  Win32_PhysicalMemory
7 }$ e0 u: _7 S; M9 u6 S1 L1 m   Win32_PhysicalMemoryArray
6 o2 S3 P1 R4 n   Win32_PnPEntity" A9 y5 E9 Q5 s. \; C. b0 F
  Win32_PointingDevice( X# C+ b; F& z5 f' k5 Q; q
  Win32_PortableBattery: m9 u+ i" _; d# w# k( F, u
  Win32_PortConnector
' T# |( Z5 _; w) D9 {; L* x+ ~   Win32_PortResource
' A1 T! N& _/ K& i+ D% m# p, {- R   Win32_POTSModem) d4 e% c" K( k6 N
  Win32_PowerManagementEvent
  a- L4 R4 ~4 x0 r. d1 G: L   Win32_Printer8 p! x$ q/ t4 e
  Win32_PrinterConfiguration
6 X# q6 z8 l( J. t, ?# M   Win32_PrintJob: o4 ^3 \  D- k& X, r
  Win32_Processor
4 D2 x& S" X* p. E$ Y8 z   Win32_Refrigeration" @! J! {+ E' u% ^4 ?5 I9 `6 }# w( D3 L
  Win32_SerialPort* D" g4 r  e- F' @- B# M! A! \4 O
  Win32_SerialPortConfiguration
+ {) @4 r1 G& O+ j! b( a$ X   Win32_SMBIOSMemory4 }; k4 L2 C, O. F" U" x$ L
  Win32_SoundDevice
+ c+ k2 n, ~% l0 I! p: a   Win32_SystemEnclosure
& B6 V0 ]) x. t3 T$ F; Y   Win32_SystemMemoryResource' |" Q7 T* F1 A+ T
  Win32_SystemSlot; `1 D6 ]( |$ J0 \. N
  Win32_TapeDrive
8 ^" q5 |( ^- q  d( s   Win32_TemperatureProbe- S/ X" ]+ _; y. Q& E
  Win32_UninterruptiblePowerSupply
4 _. A" ]- x, z7 W$ j* i( B5 C   Win32_USBController
8 n8 [5 H* h" ^3 ]- T   Win32_VideoConfiguration
0 q- g6 Z6 D( L4 m- Y, s0 H   Win32_VideoController3 N* ]2 G; t  w4 }% q5 V; Q
  Win32_VoltageProbe* |6 P6 F$ F* |

$ |4 F7 x6 D* _* V) r以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-14 09:15 , Processed in 0.018901 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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