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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)7 z+ N/ J, t- J0 C4 @5 l0 E
1 V9 r* h+ p, c- [
( F' V4 R+ I  X, n& O; i4 S6 U
--------------------------------------------------------------------------------
5 ~) H2 n* A1 \. _; ?1 W& V! o0 h+ dWMI: Windows Management Instrumentation (Windows 管理工具)
+ l" @9 N! k- X. l# J% R  b/ C: U   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 ( h  R: [9 ?9 p8 j" O; T
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。
5 P" I' L7 Y5 |" g+ A2 L3 r   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 " w7 ]' R' @. n
) I2 w3 z5 I( c& B( I; v/ N
--------------------------------------------------------------------------------
) x  h5 T% W0 Y, a7 Y; \! UBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
' N& ]  {) S+ h# h9 p( o( p. a2 N9 l5 j8 R3 M; Q8 f
--------------------------------------------------------------------------------
; y0 D3 Y/ \7 ~# @6 l$ `. n( z, U① 初始化 COM 接口:- e/ D: d7 g" ]2 A2 _5 o: C  T4 e. F
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。. h$ T4 v8 g" ~' T( i" ~
  这两个函数在 #include <comdef.h> 里面定义。! e# z- F. L/ V( ]3 {( J% J

0 y+ p2 k: h* Z/ v! y② 获取访问 WMI 权限:
- h& E, |- M, }' e2 I% |2 {   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
9 D) ?+ j0 m9 x5 S# H   如果这个函数返回 S_OK 获取权限成功, 否则为失败。
' u5 @' g# F# V" [2 L! ?  E! g& {  `8 b# c1 G( w' D: u0 S
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:6 l+ J; |, s/ b$ b' M2 s
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。+ Z6 g  o9 p( U) `- E$ V5 _+ U2 [
$ i* t" e$ m3 A: g/ e" b* O4 j9 E
void GetWmiInfo(TStrings *lpList, WideString wsClass)
" R4 l9 f  c) B5 {{
. ?; s- K: S& j4 l& p- U  IWbemLocator *pWbemLocator = NULL;
: _9 R) n- n" u& w  l6 w& O  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
- ]3 n" x/ ^4 l7 G! @   {
; H$ f$ K6 j0 t' {3 o; f: d, h     IWbemServices *pWbemServices = NULL;
! m/ u. ~* O* p* `- N; l" K     WideString wsNamespace = (L"root\\cimv2");
" r3 P) u1 O" T' X' [6 [0 d     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
% x1 i, y; }# N. b. m) K' ]      {5 ?2 {0 D1 _7 N
       IEnumWbemClassObject *pEnumClassObject = NULL;
4 @6 ]6 d6 w' Z+ H        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;" ?, X. s4 V5 k& s  D1 i
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
* J# L$ T: ~( D5 J( w& S         {
2 I& P' O( J, u% f7 g8 K$ @1 ~           IWbemClassObject *pClassObject = NULL;
! p: V- L& }6 X; b           ULONG uCount = 1, uReturned;& X" c4 t3 y2 _( g% ?
          if(pEnumClassObject->Reset() == S_OK); [& R7 }7 h) l  q; R3 t* G
           {3 w$ D, I! R. E' L/ N* ~
             int iEnumIdx = 0;
6 _9 @6 f' _. g" ~! E# F              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
$ m& C! A% s8 i* L6 }! ^1 @# c               {5 M6 Z3 [' s" p% q4 S  d
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");- G* ~. I+ F  ?. P1 @+ V

* t: N, G- U2 f! ~, |& b* K9 J" G                 SAFEARRAY *pvNames = NULL;- t) e* M  i' O* V5 Q2 |, z' J
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)2 V# t% G+ R3 D$ i7 H, {; p. [/ c% P
                 {# v. B: y0 Y# \. v2 v& y( y7 G
                   long vbl, vbu;- G" K) K6 j: m0 M3 N# W8 k. q
                   SafeArrayGetLBound(pvNames, 1, &vbl);
3 L# T  R& v; s8 f* o6 p( N                    SafeArrayGetUBound(pvNames, 1, &vbu);
) A$ e, o9 H/ X: H) A! S- ]                    for(long idx=vbl; idx<=vbu; idx++)
4 @$ r! E( p% T/ K4 u+ Y4 Q                     {- r8 v8 g3 b. t4 Y
                      long aidx = idx;
5 C% Q! p( B) x  a6 j1 L4 ?- ~                       wchar_t *wsName = 0;) k1 w; Q9 Y5 g5 r$ n, y! O
                      VARIANT vValue;- d, W/ W9 K, z; @) Z/ y
                      VariantInit(&vValue);
: _9 ^) U4 f" f7 I  V" C) b7 S1 u                       SafeArrayGetElement(pvNames, &aidx, &wsName);
" L, w( I! |- V8 D& t
/ {  n0 I/ j2 w$ ]                       BSTR bs = SysAllocString(wsName);
! {2 T7 }5 T& l/ S9 s, \( e                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
, R. @- L, g; C; @- @7 x+ P                       SysFreeString(bs);
  T: h) B* L! c( i+ K) _
  [* b! L% l  D% E% a& u/ M                       if(hRes == S_OK)
5 C3 m4 c: n& Y1 ^; G) j                        {
% Y9 M' h9 f9 Q2 |2 ]                          AnsiString s;
7 Z$ \( n% \( h0 }7 `" c! i                          Variant v = *(Variant*)&vValue;* C* u& a- w0 `; q7 N* J% |. @
                         if(v.IsArray())
  S/ a4 g7 v, \  n: J& Y                           {
6 B2 G& Y/ l' k% W( W; i. h                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)$ u2 \! V8 S! K! ^
                             {
: a- ~2 ?5 m4 J                                Variant a = v.GetElement(i);
) h$ A- g4 {- c                                if(!s.IsEmpty())% C7 C$ t! j# A4 @% k% F
                                 s+=", ";0 v. e% a" r3 \8 Y5 k' w' J
                               s+=VarToStr(a);
( [  ]  O/ X7 @& `5 l. i                              }* \9 }& `! @0 M
                          }
' ?( V& U$ d1 P. r+ M                          else
5 M2 K3 E5 h2 S$ X0 f/ p& o& X                           {
# p9 E7 X& l; p                             s = VarToStr(v);
* }" N6 e7 L* ~$ t- U( [( U/ o% k                           }3 \2 B, Z$ m# k! ?! \
                         lpList->Add(AnsiString(wsName)+"="+s);
& h% O$ |) W' h                        }/ Q! Q. S  W8 y

( F8 i: H. w; P                       VariantClear(&vValue);
6 f) V5 Q  Y0 U$ r! t                       SysFreeString(wsName);
. z3 k# ~1 G! |4 P- h! a                     }. i7 U: x7 M  D: c
                 }2 h  O# @: q% v$ W
                if(pvNames)SafeArrayDestroy(pvNames);
7 d. U" l0 s0 _9 D  J: }5 H* J" X                 iEnumIdx++;( T- j/ j  s* p0 J: b
              }
" m& @0 y/ {0 [- v. E' o            }
+ r; M+ w5 s/ v# [; s- l( ]           if(pClassObject)pClassObject->Release();( C: L  U* L7 f, L5 S' e4 j3 Y- W' f; W" v
        }
) ?3 I0 \- _  U3 z* x( j8 N        if(pEnumClassObject)pEnumClassObject->Release();
8 p4 ?! j- I# O4 |* U; G4 J      }
, ~# `/ h, X; q6 [3 b- U     if(pWbemServices)pWbemServices->Release();
6 P+ g/ @1 ]$ o3 M! m- X( h   }2 Y' ^# k3 {8 z. n7 n2 m
  if(pWbemLocator)pWbemLocator->Release();( B  `0 Z/ m/ \* s* h& B
}
! Q/ o; k  X9 y4 d//---------------------------------------------------------------------------
- z0 w1 `4 O& v; r3 Q) @$ {# W
// 通过 WIN32_bios 获取 BIOS 信息:6 B, ?& l( L! Q3 s! D3 }
void __fastcall TForm1::Button1Click(TObject *Sender)
! E( H9 p; O1 w# }{
5 D+ A% U# p0 e. D9 A    Memo1->Lines->Add("================== [WIN32_bios] =================");9 G" N2 {9 R* @6 `% u7 v
   GetWmiInfo(Memo1->Lines, "WIN32_bios");
2 Q: h* T! F& }* X4 s8 R; s% o3 E' @    Memo1->Lines->Add("");
7 z7 }6 a+ k! ~4 [: M}
9 ?: R/ i% B8 d6 K
6 N% l" e- Y* s9 H5 a7 _. ^& V1 q$ P--------------------------------------------------------------------------------' n" v. b2 l" e/ C8 E* N. S* B

. m  E+ A- Z8 K$ s0 F) y. RWMI 可以访问的信息类型有:
) x$ f: K- Y0 J   Win32_1394Controller
* `: v9 I9 ]0 A- y   Win32_BaseBoard; a8 y; X/ ]  v+ f( |9 I  Z  n: `
  Win32_Battery
% s0 o3 c: t/ E. X( a) K7 x   Win32_BIOS
0 F4 ~/ L% _" \) Z$ v( s* Y   Win32_Bus
. B# L9 y* x, ^/ r( m6 J   Win32_CacheMemory
- X4 G) I3 N; E. G! v. g! w   Win32_CDROMDrive1 x8 I) e5 V# q( s" T+ \0 o5 j; F6 ^- b
  Win32_CurrentProbe
% |+ l' [0 n! @$ I   Win32_DesktopMonitor8 y( t3 P4 c3 [. H
  Win32_DeviceMemoryAddress
8 |& _4 H4 z# ]! R( r- G& q   Win32_DiskDrive. B5 P& ?  l  _5 D7 u9 P
  Win32_DisplayConfiguration2 W4 D8 s6 N' j# b) q
  Win32_DisplayControllerConfiguration! |/ [% j3 O: b( g+ K/ b2 Q( v
  Win32_DMAChannel* K8 N9 c- D* X. T
  Win32_Fan
! C3 p7 f( C  ~9 x   Win32_FloppyController
; u) K% p8 i( c0 S0 G: k( X   Win32_FloppyDrive* D! a; I! Q- {+ v) c
  Win32_HeatPipe. s# m' P1 C- |% V% J: |
  Win32_IDEController% M/ {- p0 c" S" G8 {; U; Q
  Win32_InfraredDevice$ o. C' i: L$ m
  Win32_IRQResource" F. E5 b/ J$ @3 k
  Win32_Keyboard
& S8 T2 i% G& \. r7 _4 Z; P   Win32_MemoryArray
7 X2 d5 t# s* I2 K/ A% l# B   Win32_MemoryDevice
- R8 G3 g6 s0 p   Win32_MotherboardDevice
5 n3 ?! m& P9 r) W  Q& {   Win32_NetworkAdapter) U4 P5 \& W1 m
  Win32_NetworkAdapterConfiguration8 Z, |/ D' X2 t. ~5 x- k
  Win32_OnBoardDevice
. k) v7 z( z. {' D6 X   Win32_ParallelPort) r# m8 }0 D9 T& Z
  Win32_PCMCIAController: f( k4 V7 G: Z5 r) A* X/ z
  Win32_PhysicalMemory- X4 f! M# }2 J: l0 |, l
  Win32_PhysicalMemoryArray  r8 N% V" O" [2 n5 }+ D
  Win32_PnPEntity' z2 c2 x2 `) J( U# w1 d3 x
  Win32_PointingDevice
4 g1 F5 X4 k& f   Win32_PortableBattery9 K% b# l: b( @5 s9 B: h: K
  Win32_PortConnector
2 j$ [6 |6 b- ?: x   Win32_PortResource0 c" R: g( ~6 p1 l* C
  Win32_POTSModem& @, f8 T: L' a/ D
  Win32_PowerManagementEvent
+ N- B1 ~8 ~% y. E4 x1 I7 T- {6 [   Win32_Printer
) v, Y" }2 j% V: w   Win32_PrinterConfiguration
6 s- n- n( L0 }   Win32_PrintJob$ [) [; e8 A; g: [  r8 |
  Win32_Processor
( e3 K- \2 g' j  [5 w/ c/ h   Win32_Refrigeration
: _6 n* t0 D% H: `: e   Win32_SerialPort
) K' m, |0 v9 ?   Win32_SerialPortConfiguration
, x3 N7 [' w/ k' n4 `   Win32_SMBIOSMemory
9 H& S9 m0 w9 t( R   Win32_SoundDevice; r& @; B; ]+ i. b
  Win32_SystemEnclosure0 o, J+ R+ U, Y2 Z* Z) n
  Win32_SystemMemoryResource
  @# {" T2 @. |: Y" R" b   Win32_SystemSlot7 a( |( Q) i! q1 a1 h" X
  Win32_TapeDrive0 [0 w4 U* T) x
  Win32_TemperatureProbe
$ R7 i8 Q* n  _: z* X+ E8 \* j   Win32_UninterruptiblePowerSupply* R7 U/ L8 b3 @# h2 n, |
  Win32_USBController
' a- ^( i7 F, h6 [# L1 e   Win32_VideoConfiguration- i0 v' m$ }; i! |* S
  Win32_VideoController8 ?9 Y& W0 N  @* W: S% y
  Win32_VoltageProbe
3 b) |% \, T4 ~. w- r% O2 x; g5 }  C1 I. z: g* T7 ~
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-11 15:07 , Processed in 0.019540 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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