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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)4 K( W' `( E) e/ Y! G) p. V
8 i2 B  t/ U& u( l

7 p4 [' S7 K6 {* }8 C, G& S--------------------------------------------------------------------------------
8 W" D, C. x: j' d! ]WMI: Windows Management Instrumentation (Windows 管理工具)( K) u/ q9 g* j: F
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 4 \( N- w) f6 R+ \
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。
5 u- N2 w9 Z# M. H   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 # X; @" D' N# N

8 m. j5 s9 g* V! b7 ^--------------------------------------------------------------------------------
; z/ L0 G; g0 nBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面+ |% l5 Y7 `1 H% `2 {
8 |) O7 q. V: X
--------------------------------------------------------------------------------
1 G8 m, Z% s: W* f) K① 初始化 COM 接口:
% Y* |1 m# @7 _2 E3 h   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
/ z/ }# a1 H7 D( b. p$ v6 z   这两个函数在 #include <comdef.h> 里面定义。* a8 q0 O( H- A8 c

* x8 j3 S1 q1 @② 获取访问 WMI 权限:- {/ D/ m" Z" V( c  ?2 Y2 n: J
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);+ Z! q) j+ l/ u8 Z# o1 `5 H' P
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。
0 |2 T/ I. a4 _5 X
0 E0 M$ A; f- l+ c* F③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
8 h2 C+ [2 f# }6 p& e: L   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
) z+ u; D4 J8 ?$ w
. o. \/ N; R7 L# qvoid GetWmiInfo(TStrings *lpList, WideString wsClass)
: j1 ]# O  M3 i6 C& Q4 F; ~0 \{$ {* g$ m& Z/ ~" ?. ?, N1 Q
  IWbemLocator *pWbemLocator = NULL;- @* O5 ~7 [0 R
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
6 o1 G7 R- I" F+ N   {4 Y; e7 e/ C# J% J$ T$ G: k
    IWbemServices *pWbemServices = NULL;: T7 y* e9 d. s
    WideString wsNamespace = (L"root\\cimv2");
5 ]- D( m1 u! @* ]1 {" ]/ X5 k     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
5 I1 H7 M& s7 v3 o/ i* f$ K& }      {2 a' M" z0 E7 N3 |3 c9 ?0 B
       IEnumWbemClassObject *pEnumClassObject = NULL;
0 c0 M3 `+ s# u# T, F        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;; d- b- r; P: T3 J
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
8 ^7 W' I+ t! v" n- L3 L6 q         {
& j& k9 Q, e- e; }  \5 V2 y           IWbemClassObject *pClassObject = NULL;
3 j. [3 l1 S1 U- ]5 p* y. H           ULONG uCount = 1, uReturned;: h6 @4 ]' F0 p. R
          if(pEnumClassObject->Reset() == S_OK)6 C& h  }8 d3 Y' E2 z
           {
8 R) M2 d% ^, N5 [              int iEnumIdx = 0;/ Z6 a  u. S& W8 D( }% J% Y- X
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
1 e! X9 q/ F) W+ `  I/ V               {
4 E2 P! ~% I  c) Q% x+ ?1 W0 ?                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");9 i# c( v# |+ Q. T3 L7 ~5 o

" q) Y$ b& X3 j" L$ C                 SAFEARRAY *pvNames = NULL;9 A) I# Q% [& E9 n1 \
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK); w% X% E8 }+ H' \4 y2 B% C% b5 l
                 {
5 Z2 J2 A3 }6 m: p) }                    long vbl, vbu;$ X; N8 d: o+ e, _3 Q( J) }
                   SafeArrayGetLBound(pvNames, 1, &vbl);
+ x0 L4 D, x+ Z/ G# `                    SafeArrayGetUBound(pvNames, 1, &vbu);
6 ~( F* [7 O! s" I' o- A                    for(long idx=vbl; idx<=vbu; idx++)
6 H' J: S4 Q) V                     {: U" r# W7 N8 R  i$ j8 W: o# J  T
                      long aidx = idx;
2 Z, j9 }  |, A' a  j$ s/ b0 n                       wchar_t *wsName = 0;- v" M# H: u! S, T; ~- O
                      VARIANT vValue;" x3 {" j% q4 S% t
                      VariantInit(&vValue);
0 N! S4 M2 `3 o                       SafeArrayGetElement(pvNames, &aidx, &wsName);- }7 ~) N% v" H5 G9 c

$ T& J; j, Z2 D% a6 x; v                       BSTR bs = SysAllocString(wsName);# Z. f* e% O# n
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
( C0 L/ o$ X  k( H- v+ }8 W: {                       SysFreeString(bs);4 f7 W/ ~) f1 V
) h" O$ N' N8 P7 A
                      if(hRes == S_OK)
9 [' v* h# {2 w                        {" q, {* G' H6 {) I
                         AnsiString s;
8 j$ h. j: `0 J4 V! V3 p. @0 r8 g- V                          Variant v = *(Variant*)&vValue;. _0 p% O. i1 r& s
                         if(v.IsArray())6 V; d3 B! O8 {/ J( U" E3 I, I
                          {4 d% b& v, R( D  b' ?5 p4 B
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
9 P8 Y! z  K4 F                              {
5 o) @  }  q" z* N" y1 B                                Variant a = v.GetElement(i);
5 |' y8 u$ b3 C                                if(!s.IsEmpty())
1 ^+ e8 a4 O. b6 u: }1 b0 Z                                  s+=", ";. ]8 j# Z# S5 F; i6 Z
                               s+=VarToStr(a);
: f) L( A  }$ {                              }
% ?. M% [$ A/ b, Y/ Z; n                           }+ w/ S- N( P! Q; o6 `8 J+ Z" L
                         else) F' z. P5 O2 H4 }2 z7 g
                          {. R/ P3 t- L/ T6 R3 L
                            s = VarToStr(v);* z- f8 C5 \$ q9 ^- d: @
                          }1 i) Y: v5 }' \  d' \
                         lpList->Add(AnsiString(wsName)+"="+s);
* c; P1 E; l4 h9 u/ I                        }
! R3 N# h$ G- m* i
0 k, B' f4 D( U7 ?/ W, O* I: D                       VariantClear(&vValue);. d9 x9 B# X2 [% c
                      SysFreeString(wsName);
4 l3 i* B# I4 l6 Q8 g: h3 K                     }8 I4 S* S  D* u4 ^. F4 e. w
                 }& m1 q# R. K: W1 T9 k% B) ?' o
                if(pvNames)SafeArrayDestroy(pvNames);
# K* ^7 m: i7 E# @                 iEnumIdx++;
% d9 f8 k0 q: Y6 D8 C               }
' H" ]. j9 q8 f  t$ z8 Z9 X6 F3 t            }: Q( b+ J5 g* _7 _
          if(pClassObject)pClassObject->Release();
4 O7 z4 O& t& G6 x' k" }! w6 y         }* h3 \0 P9 U( b% p
       if(pEnumClassObject)pEnumClassObject->Release();
  b! r7 }7 c" D1 [8 k  j. d      }, v6 t% G7 B& R( n9 Q1 i
    if(pWbemServices)pWbemServices->Release();( K3 ?, k; \/ O& H& R( w# ~
  }
7 c+ S# T, k/ A  R  ~% d  if(pWbemLocator)pWbemLocator->Release();/ S! I$ r+ W" s, H( _3 ^' G
}
6 ^, T+ I3 N! e' I2 h6 T8 \//---------------------------------------------------------------------------
; H* P9 o: Z: r$ V' n) b: [  Y) ^" ]2 {* h5 U
// 通过 WIN32_bios 获取 BIOS 信息:
. a" ~, s* X0 L5 g3 K  |1 zvoid __fastcall TForm1::Button1Click(TObject *Sender)3 `9 k+ t: h7 T3 K% ^
{* N. O+ o" I  @3 `
   Memo1->Lines->Add("================== [WIN32_bios] =================");
2 F+ B* b/ M6 J/ Y# n    GetWmiInfo(Memo1->Lines, "WIN32_bios");* H9 h6 ^9 s8 n; M) R$ w
   Memo1->Lines->Add("");
" I+ ]. V4 z- E}) b0 C% Q  h9 Q6 d$ Y6 c- d
1 K+ x  {- u, b( |
--------------------------------------------------------------------------------
* c# ]4 Z2 i4 h8 ?2 h/ Y" y2 a2 y$ h  x% r  N0 V" c
WMI 可以访问的信息类型有:
/ ]* m6 Z4 X3 w0 r! B( V3 J. V1 t   Win32_1394Controller
. r; i. X" E' p; s# p   Win32_BaseBoard
/ C6 Q) |, l( v$ O) V9 g) X0 ]   Win32_Battery. r( W& g  W, [# s# G
  Win32_BIOS
- R/ o/ h+ [2 Q6 r2 p! Y, q   Win32_Bus
- x/ Z( n8 \  H6 n: ^! O   Win32_CacheMemory" C8 ]' R, @1 j+ t" M
  Win32_CDROMDrive% s1 h6 D+ _+ p+ w
  Win32_CurrentProbe" R; F- W" K( n9 G: Q+ _5 I
  Win32_DesktopMonitor+ ~  T7 g7 k' t8 Z( j4 l) o
  Win32_DeviceMemoryAddress
8 h! F" P$ Q/ [- W   Win32_DiskDrive/ f/ E- A; b, z! H& e0 ?1 Z4 _
  Win32_DisplayConfiguration# f  y8 M% y1 r
  Win32_DisplayControllerConfiguration
3 n! M, S" N) d0 d1 v   Win32_DMAChannel0 i& W# u/ j7 c( p/ C  R
  Win32_Fan7 X7 R3 K, b; A. w" w. a
  Win32_FloppyController0 a5 \& v2 {8 r" `
  Win32_FloppyDrive- f' n0 L* p+ @* n
  Win32_HeatPipe
2 \- E, @+ a! k9 t& ~& m- Z   Win32_IDEController! q% D# o7 |) P% C! t
  Win32_InfraredDevice4 d) z7 a5 u7 |: i8 w
  Win32_IRQResource  ^- t5 v0 `+ z2 g
  Win32_Keyboard, R" {: T( w1 v2 h! O' p% t6 |# [
  Win32_MemoryArray3 n2 D5 k$ l' ~
  Win32_MemoryDevice1 k' t! I: h. E4 `% z# g" _
  Win32_MotherboardDevice1 v! X6 o$ F/ T$ s( Q
  Win32_NetworkAdapter
; t; l# o( p! [- _6 Z: x   Win32_NetworkAdapterConfiguration3 _7 w) l1 ]" Y
  Win32_OnBoardDevice
  @% \2 ^2 o" t. ]- g3 E4 l   Win32_ParallelPort+ T6 B' \- V3 b  N3 U& |. Z
  Win32_PCMCIAController
7 i% x6 L# _8 q8 v  f) ^" x   Win32_PhysicalMemory! M8 J2 r9 I  {/ w# F
  Win32_PhysicalMemoryArray
: J0 p; a, K2 O) i5 }2 ]1 P4 }7 y   Win32_PnPEntity* H0 l2 _' @9 l3 E+ @
  Win32_PointingDevice
0 q4 ~: f* m7 k  [# h1 W$ ~. M   Win32_PortableBattery
- r2 h' T( x2 S. u' `   Win32_PortConnector
7 N& t: y6 D$ a0 m$ _4 i5 d3 V5 E$ Q   Win32_PortResource
6 r7 B$ X' ~: C5 s# Q; j7 |   Win32_POTSModem7 _( B5 o. Z7 l6 e
  Win32_PowerManagementEvent9 {- T# s+ l+ S( Y- z* y, @; q( x
  Win32_Printer
2 J& _& C# Z4 y  H9 G4 P. b   Win32_PrinterConfiguration' C  s, m, A7 u4 t
  Win32_PrintJob
& r! \8 Z( ]% {! Y- s   Win32_Processor/ r$ i0 q4 d5 d6 S5 ?- T/ h# z
  Win32_Refrigeration
( a8 H/ ]6 v4 x  U/ a9 x9 R; [   Win32_SerialPort+ v0 p6 `2 d2 l- f% e
  Win32_SerialPortConfiguration
3 Z9 L  \/ T* A2 o7 b; `   Win32_SMBIOSMemory! o/ b0 ?6 z2 W: b
  Win32_SoundDevice6 [0 b% P0 u* h& T; |2 t
  Win32_SystemEnclosure
7 Y) Z9 R* O+ {' x* d+ R2 I   Win32_SystemMemoryResource
: H. M3 [: R' n" |' m# q) F   Win32_SystemSlot
' i6 B" p' g. P. i( X   Win32_TapeDrive4 X2 x+ k7 X9 U, b5 l
  Win32_TemperatureProbe
& p+ E) Q$ Y. _7 r; x   Win32_UninterruptiblePowerSupply. G" C" S. o  F% I3 A( Z( w
  Win32_USBController
6 m% T! e2 ]' b0 F+ D   Win32_VideoConfiguration# @$ E2 e7 U6 Y2 y+ T- O+ }
  Win32_VideoController
5 m8 U! U) w/ Z5 P9 |! U% j   Win32_VoltageProbe- |9 m. ?3 I2 N# X8 y, i8 b4 M

9 N; m8 \- P+ \1 _3 f! d2 U( a, K以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-10 15:08 , Processed in 0.020841 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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