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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
+ E+ T; l' u+ \/ z' T
9 d* M9 Y2 p1 ~, j/ f. Z! e. ]8 L/ P9 z0 n3 ?3 h
--------------------------------------------------------------------------------
" d, y1 _1 _: S  |7 @* i! |WMI: Windows Management Instrumentation (Windows 管理工具)" [. s1 ^; ~$ p2 i
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
: W' w0 K& i; C! O: N- `   利用这个工具可以管理本地或客户端系统中几乎所有的信息。( ?! m* C, ?, z: U0 `* q) G
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
) Y; V) A6 Z- M( Z( ?0 Y$ e1 S4 d  X4 n* P* I
--------------------------------------------------------------------------------7 U/ p7 _  f1 V5 ?8 q! w
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面0 \( A6 H& s2 `

1 h: b/ u# g. r# `# O$ c1 N--------------------------------------------------------------------------------
2 \; f8 w% l) h8 R① 初始化 COM 接口:
* w8 }) m3 x, v- }! T   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。* q* V0 i# w& ?( V8 Q. Z. R- O
  这两个函数在 #include <comdef.h> 里面定义。2 _6 [  R, A" n8 J3 W. D' b2 ^, z0 m

+ M1 t  `# f- o# l1 E( J; I6 i" C② 获取访问 WMI 权限:$ O. A& I" R  ~* Z8 H. M
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
: B' J; ^* ^6 H7 O   如果这个函数返回 S_OK 获取权限成功, 否则为失败。
( d; |, b5 D8 T8 N1 W6 r9 ^( a) b& L6 ?9 N; ~
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:8 k! _) ?4 S' z! m: t9 x
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
  ?3 M1 H0 k7 D2 x# w* u1 I/ o8 D2 i& x" Y$ |6 I. Z: p
void GetWmiInfo(TStrings *lpList, WideString wsClass)
/ \& V0 l" p  k9 _) O8 C, c$ {& t3 V{  `: B. X' q$ m7 c0 s# l5 e
  IWbemLocator *pWbemLocator = NULL;' M! \6 O# L, o- b0 _
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK), A+ u8 y. V6 v2 d/ L! Z. m
  {
! S, b. a8 G" m0 T: S/ i0 R+ A     IWbemServices *pWbemServices = NULL;
$ J- u, p. P5 |5 x     WideString wsNamespace = (L"root\\cimv2");5 S& V5 P6 o. \
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
9 E; y. N; C" ?% D" `) y/ _3 V      {5 e  H5 r$ w9 t0 _, v
       IEnumWbemClassObject *pEnumClassObject = NULL;2 }/ y4 i5 P% Q0 s
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;# A* R% C  V. r  E- j& E; b4 ~- K
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)( a% Z! Z/ \) U+ y- p# Z
        {, {* i7 X! j0 Y0 ~/ b
          IWbemClassObject *pClassObject = NULL;, j6 o8 U. m% T* r2 g! i, r
          ULONG uCount = 1, uReturned;
! k: U4 \8 T+ K0 d" s7 ^) q7 i           if(pEnumClassObject->Reset() == S_OK)
3 a$ Y; U% c, Q( s! j6 r3 l) J: ^            {1 S" O+ O5 y. F: W
             int iEnumIdx = 0;
" b7 E" E* C! d              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
* P2 h! W" v: Z  z' S               {7 T- N. {& a! f- O  s3 C
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
( k) j  c  l; a% R5 k$ @; \3 ]1 Y
& P, b- x; H2 X  K5 ]6 ~1 B( I                 SAFEARRAY *pvNames = NULL;
9 r; n6 M; q+ s# ?3 Q                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
+ j% U% }$ W4 D( p8 {1 [                  {
7 r/ K6 X- q0 f2 e0 B( J( q, j                    long vbl, vbu;) k& M, }+ c; |8 Q$ \% p
                   SafeArrayGetLBound(pvNames, 1, &vbl);
8 J: d1 e/ y' J                    SafeArrayGetUBound(pvNames, 1, &vbu);" P/ ?& g. C" b
                   for(long idx=vbl; idx<=vbu; idx++)5 ?4 K* ~* Q) }9 r( H. S; j5 A
                    {8 J: `; I2 d6 K: d
                      long aidx = idx;6 l  `2 P# |1 V5 e% P# p
                      wchar_t *wsName = 0;5 S! p7 e3 J! n7 v
                      VARIANT vValue;5 e8 C# z1 L* r7 o7 L
                      VariantInit(&vValue);
+ i4 z8 ^7 @3 g                       SafeArrayGetElement(pvNames, &aidx, &wsName);
2 G; s9 x" `$ |7 Y& B" B6 X+ ~% G* J
                      BSTR bs = SysAllocString(wsName);" L7 I6 w) j2 O/ q' u  L% {
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);/ @1 a& Y4 j3 C( ~$ o: A& m$ q# `
                      SysFreeString(bs);
8 Q* ]' Y( R' z: m" ]0 O/ \5 k9 |, i2 {+ Q
                      if(hRes == S_OK)4 z) N0 n& r- [
                       {
, k1 @$ K+ y- z7 P4 S+ ?                          AnsiString s;
+ H/ d  k4 _& J7 K, @                          Variant v = *(Variant*)&vValue;# w& T0 K( \1 D, X( [
                         if(v.IsArray())
. T* v" C" Y  h; C                           {
# `/ V  y5 J6 F                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
% _$ w  s( I9 e* ^& W" Z3 ~' I                              {2 m6 h8 S  m8 ?3 }2 Q1 `5 i" C8 W3 |
                               Variant a = v.GetElement(i);* X( a8 s- Q+ B* `5 F: a# ?
                               if(!s.IsEmpty())5 e6 B: }5 H9 K, I1 ^; B
                                 s+=", ";! N% M  H# i. P( Z
                               s+=VarToStr(a);
% _0 }" h- T; r' t' V                              }
! H# r6 e3 k( U- R' l/ m, n3 |! Z                           }& N: J+ {) |7 D! X) \
                         else
! P. m# K" D" x- d0 A& n                           {
% C% `& n8 _+ P8 D# N                             s = VarToStr(v);2 b& E! C3 i# W* H& n& K
                          }
( R+ @( U( \0 B( G+ ]                          lpList->Add(AnsiString(wsName)+"="+s);) V0 i7 G- I5 P
                       }
" O9 W. H* G7 e/ `; ?6 |* u
( |0 T$ h2 C# [6 q0 _                       VariantClear(&vValue);1 }& M' s2 t9 I$ Z- ]7 K
                      SysFreeString(wsName);
8 _/ A& p9 E: O* i" r. i  g                     }" U. R+ y6 @& N$ P& [" c
                 }
8 H( H: d0 Q' @$ b; ^% b+ m% u                 if(pvNames)SafeArrayDestroy(pvNames);
; z; F9 z  ^5 ^$ U) Q                 iEnumIdx++;
' s* {: S! D3 W1 U" n0 w3 z( }               }
/ C, W; p3 z2 v$ L' J2 Z8 ~            }
  T, P: {0 n$ [  o* X$ x           if(pClassObject)pClassObject->Release();5 v# a& M/ Z3 M# m# G& R
        }
# u) I# R# ?$ t0 G! z5 R2 e5 v        if(pEnumClassObject)pEnumClassObject->Release();9 g0 p" `& d( L
     }
2 @! D. M2 p) n. w2 L7 a6 N     if(pWbemServices)pWbemServices->Release();, ^$ O1 H4 H- |/ T% x* ?
  }3 i- A$ c& v0 d' x, A
  if(pWbemLocator)pWbemLocator->Release();* r3 V* J: U* i! f5 ~
}- ^, w+ z9 Z& i* i9 O, H& P
//---------------------------------------------------------------------------
5 \% ?7 @; p  @2 r5 t5 E) t. g3 P" J1 W0 c6 C) U
// 通过 WIN32_bios 获取 BIOS 信息:) X  z9 K9 M, p  s% }  L
void __fastcall TForm1::Button1Click(TObject *Sender)' B( @/ @" ?3 J. \# D5 X
{
# c& t# S+ J2 u: z3 s2 S    Memo1->Lines->Add("================== [WIN32_bios] =================");
6 n& O! t0 d, N* n5 E6 ^0 U    GetWmiInfo(Memo1->Lines, "WIN32_bios");9 ?  R* ~3 C0 }5 t2 W3 G& b0 C6 x
   Memo1->Lines->Add("");
& `) ~( K5 D! s& L0 H: r9 ~7 H}
& I/ @* P: ^2 S8 y6 R' U! L2 M& b$ ?7 |# M+ f+ l
--------------------------------------------------------------------------------( ]5 Z% t) j1 [4 q. ]5 z7 |+ b" D
+ v, D# i( H5 y* N0 Y2 I
WMI 可以访问的信息类型有:) f. J. t! k, m% n
  Win32_1394Controller
$ ^9 F! S3 r- Q   Win32_BaseBoard
! u* y! Z2 Q0 C9 S- f   Win32_Battery
" p9 r  m) I! w) A( b   Win32_BIOS
( |7 |% o1 {( `2 f) h, ]   Win32_Bus" Q2 Z- [0 r3 G; r  A+ [% R5 I
  Win32_CacheMemory
2 L) E. Y! a) F1 R& C: i   Win32_CDROMDrive
' I. X  s: m. P1 @   Win32_CurrentProbe3 y6 [! I: c/ `
  Win32_DesktopMonitor6 T3 t1 a* N8 R' ]* G" Q
  Win32_DeviceMemoryAddress
' V! O$ b9 ^, }4 B9 k7 \( ]   Win32_DiskDrive
2 \3 R; h8 I8 b4 D   Win32_DisplayConfiguration! R1 N, D- E: i7 F2 @# F' t
  Win32_DisplayControllerConfiguration+ Z* |0 H0 _& y& ~
  Win32_DMAChannel# p9 D3 c0 E8 j5 g
  Win32_Fan
4 P) ~* h* u5 n/ z& A5 ?   Win32_FloppyController  I: g9 Y. Q- E; z5 E$ d% q/ ^
  Win32_FloppyDrive
" j" ^3 p5 l9 h3 v   Win32_HeatPipe$ T& e5 @) B/ l4 O7 Y$ n
  Win32_IDEController2 H3 s) d% J8 x, K5 ^# c& ?: u6 _
  Win32_InfraredDevice
: }% s8 F# \7 Z+ \2 T/ i( i   Win32_IRQResource
0 ], M( _/ }7 F8 l; l   Win32_Keyboard8 u) |" ]3 Y8 S
  Win32_MemoryArray
2 n! w; p6 m3 a. k; f9 n% g( G  [   Win32_MemoryDevice" d2 Q/ @0 ]2 Y. i
  Win32_MotherboardDevice
8 y) H/ x1 r/ p1 U; A- d   Win32_NetworkAdapter
+ F7 C( P4 ?5 X& d; G+ F3 X3 a" W" \9 P   Win32_NetworkAdapterConfiguration
5 J, F+ P0 G) J; g$ F1 }   Win32_OnBoardDevice
1 @$ r; E$ `. E6 @8 V   Win32_ParallelPort
, y. p6 ~5 G  K) l% Z2 o" Y   Win32_PCMCIAController' ~- u! i) G$ w) |% K6 ]
  Win32_PhysicalMemory  z; F, V& c& l- ]: Z' _, k; u
  Win32_PhysicalMemoryArray1 M4 Y7 F  N2 `; O* Z4 w2 J; I. @
  Win32_PnPEntity. |) c$ i- d1 `' ~4 ^* F+ l
  Win32_PointingDevice: z' ^( E3 {+ k7 i- l
  Win32_PortableBattery
5 r$ U# }; v* \" i* Q7 S7 \# z   Win32_PortConnector3 N9 \$ }2 ~+ [# }$ M9 n( i9 v0 [& P
  Win32_PortResource8 d7 @: d* c' j" S$ k4 a) `
  Win32_POTSModem, Z: L  n4 f' V$ \# I; H' k. E) g
  Win32_PowerManagementEvent
" b8 N& C5 g4 l* g   Win32_Printer
% C8 v0 s$ h4 @% `3 i. i1 O* P   Win32_PrinterConfiguration: w1 [; N( w( K$ B. k. n6 h
  Win32_PrintJob
; S& o& f4 A9 Y* ~   Win32_Processor
( l  X; T) _5 q! |, j: S   Win32_Refrigeration
3 |( O' y9 @5 l" r6 |   Win32_SerialPort4 T* f& k2 m2 L) \
  Win32_SerialPortConfiguration
5 A8 v4 I: e3 q: `) x; K   Win32_SMBIOSMemory
" L8 F+ h' p; b* L8 o7 {   Win32_SoundDevice
9 a9 |8 m9 s  @9 b& R/ j/ i+ j6 {   Win32_SystemEnclosure
( x9 M! `& V$ Y& Q0 X   Win32_SystemMemoryResource
( u. b8 \: c8 r  o2 U   Win32_SystemSlot# Q1 p& `. ]+ Q
  Win32_TapeDrive
# o2 W5 \. J: E( E$ l8 b   Win32_TemperatureProbe+ ]  T. Y  _! U6 L6 K
  Win32_UninterruptiblePowerSupply5 `, i, N1 Y" V/ K
  Win32_USBController
, t( j2 e4 l! J! n4 Y  f   Win32_VideoConfiguration
& `( m: r2 z9 W  [   Win32_VideoController
+ J1 f% k8 I9 x( A  ^! ^+ L" m   Win32_VoltageProbe
0 j* d3 N" K0 ]' @' g: i/ C/ D0 j$ ?8 L
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-30 05:46 , Processed in 0.019216 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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