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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
3 ~1 i6 @9 H% f
! n6 _+ p" b$ w0 k7 |- Z$ Q( s8 ^7 _: H$ X! }
--------------------------------------------------------------------------------
1 J6 U& \9 G" R! _9 g+ U6 yWMI: Windows Management Instrumentation (Windows 管理工具)
  y( l3 L5 p5 M4 F   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 , t! T4 Q4 z) W  J$ ~5 I
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。  v4 \6 s0 C" J/ @4 F5 `7 u
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 9 r( k9 U! v9 \* }; f, n5 ^, J
. Z/ m) ^# P' G6 D' e( c! k9 v
--------------------------------------------------------------------------------% o5 `* q- T$ R. g6 F( V
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
$ r" a3 i: @$ b: I2 P3 f( p  R
% Z2 U5 u( W2 E--------------------------------------------------------------------------------
* j* l4 Q0 N, f( P9 ^- b- i7 _① 初始化 COM 接口:
; b5 l, |- M: ?( J! w- h$ X7 E   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。& \# M5 n! P' t2 b
  这两个函数在 #include <comdef.h> 里面定义。4 V2 x. _6 i$ {
" P3 ^5 \% F+ z( p0 Y  g5 p
② 获取访问 WMI 权限:! X1 p9 T' c) R' G4 S
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);( r9 M' g( c  x8 {
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。) a; o- ^* a' h7 N& W1 r

/ B  ~; M7 p* b$ r4 S③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
- P. `9 W' N; o   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。! K7 E( Z( k# g; e
& r! H6 Q4 Y0 O9 s
void GetWmiInfo(TStrings *lpList, WideString wsClass)0 w" F9 i: K5 ^
{
% x0 v3 d* e, D4 |. ^- ?  IWbemLocator *pWbemLocator = NULL;; G. T( A: ^# R8 H3 t
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)1 D8 a# E0 x- ~/ A
  {5 R3 v5 Y( ~: b' p5 h5 C
    IWbemServices *pWbemServices = NULL;
" M6 @1 @9 V8 i) W: I0 Y     WideString wsNamespace = (L"root\\cimv2");
; o  ~- a! ~9 F     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
) h2 I, s8 {' ^8 D+ E' H      {
! U6 q- a! P% Y( m        IEnumWbemClassObject *pEnumClassObject = NULL;
2 `/ B* ?* d& ~5 x" y  q6 W        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
6 G/ L4 p" [: ]# s4 _# N        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)1 @: d( O" c* W4 X9 l* D
        {  E3 z& Y! O& y7 K
          IWbemClassObject *pClassObject = NULL;* a2 r7 N  n0 V7 A* B
          ULONG uCount = 1, uReturned;% e9 y3 c- K1 Y9 A
          if(pEnumClassObject->Reset() == S_OK)0 T$ X% d1 j2 k5 m+ B* l2 ~( ?
           {
+ N' B: a* R! x% r0 o              int iEnumIdx = 0;$ f8 E6 Z5 R, D/ J* v. W; S) y
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)' d3 E  ?6 y& z2 i
              {
6 e) k' {! v+ x- B+ M                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");& V/ ~: D8 c  Q% _- K: O* Y

( O# v" ?% s# J                 SAFEARRAY *pvNames = NULL;7 L' X* \4 n" n" n& a
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)" n! |/ ^* G, Z3 D+ v3 [7 i
                 {" s' V4 h  F! T! `" l
                   long vbl, vbu;) l' b+ y& W" \, Y0 `( I, t" D
                   SafeArrayGetLBound(pvNames, 1, &vbl);( Q+ R: |) {* M% v; w! {3 v9 i2 T
                   SafeArrayGetUBound(pvNames, 1, &vbu);
) S1 G) v( d& d3 H5 U! Q/ p                    for(long idx=vbl; idx<=vbu; idx++)
! R* L+ h8 b/ n& }0 J- {                     {
! n- ^: }3 E3 A" @4 z5 S) R                       long aidx = idx;
6 ], ?% U" i+ C                       wchar_t *wsName = 0;
/ r; Q; C3 i" h                       VARIANT vValue;. K7 K$ C+ ^8 W
                      VariantInit(&vValue);5 ]2 f3 k# u) M3 g4 \& @. U/ {
                      SafeArrayGetElement(pvNames, &aidx, &wsName);, P0 W# Z5 Z+ I( _+ g

. d2 Z2 z/ R5 t  B$ F% r, S  ?: P* d                       BSTR bs = SysAllocString(wsName);* O. M# o( Z9 q- O3 p) j. ?
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
* s2 M* C5 T7 Q' H0 X. ]7 O                       SysFreeString(bs);8 y% ?+ }( y0 N0 H, X2 F# ^
- D8 y9 T1 F1 O5 M
                      if(hRes == S_OK)8 ~4 P5 g2 E. p' V: T( [
                       {
, _" o: B5 x" I                          AnsiString s;# n9 _' S8 D1 J% V
                         Variant v = *(Variant*)&vValue;7 b% m8 @8 Y; L1 V" D$ q! I
                         if(v.IsArray())1 ?) ^, y' C. N; K3 d
                          {4 l3 V. N! G" M& i$ B  g8 r
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
' P7 d& \7 K# `' y$ B) n. B! s                              {
, N* z9 O- B# x                                Variant a = v.GetElement(i);
) D" _! H- s1 K$ H% }9 l; }                                if(!s.IsEmpty())
" B( l: V; l. l4 u) }9 h. T                                  s+=", ";  n' U! V( v+ k# f5 b+ R1 P/ {
                               s+=VarToStr(a);; H3 U" F4 A! f. g' Q  q
                             }& J8 O# t4 C1 ~0 h; @3 T2 G! s% W' x
                          }
: _& i+ u6 m+ `( J) s7 l" G5 w4 l- E                          else
8 }; g& a6 H( ~* C* S& x/ ?                           {/ Q0 t) {' A; e% y, E
                            s = VarToStr(v);9 H+ i- m4 X4 U% q
                          }4 n. Q) h- a/ r) U8 @9 @
                         lpList->Add(AnsiString(wsName)+"="+s);: q2 N4 \- Q+ P( Q2 L) Z% ^
                       }. ^  x, B1 y( x: H7 t  F

" C' {5 u2 `; B9 x: C" N                       VariantClear(&vValue);5 Y: h) r+ F/ r  Z$ m8 m
                      SysFreeString(wsName);
3 X$ L8 t$ [8 c6 s9 C2 m                     }4 P! A+ K( Y7 T
                 }
6 n! ^2 B, D$ D8 J3 [' o: y% q                 if(pvNames)SafeArrayDestroy(pvNames);% E* N& k, H) o& {0 }: B! C
                iEnumIdx++;! ^, F- Y% m) A2 f( [
              }+ d" Z+ b+ c5 E4 z
           }. ]7 B( |! T9 O$ ~
          if(pClassObject)pClassObject->Release();
- p) e5 s) t5 c: D/ J1 S: B         }
! l4 S) O) C5 J1 U, f9 Q" d        if(pEnumClassObject)pEnumClassObject->Release();
8 q: t! ?% H% [! `7 @/ K: }      }
) W0 c# x' D" d* O. x     if(pWbemServices)pWbemServices->Release();1 D) ]8 E2 |/ R% \
  }
) t$ ]2 m" N+ m3 r' m$ @  if(pWbemLocator)pWbemLocator->Release();; n) z& l0 g6 L$ j; k
}8 o  P0 f2 z! U; h7 `, S8 ~
//---------------------------------------------------------------------------
$ e5 a' f# r3 r; f1 j  m
! D! ~) J. W$ ]+ k; [' m// 通过 WIN32_bios 获取 BIOS 信息:* D! k8 E* g) ?: D) N+ T" Y* p. S
void __fastcall TForm1::Button1Click(TObject *Sender)
( N/ N+ b9 [) [, r6 n{/ N# f* c' q8 L' X
   Memo1->Lines->Add("================== [WIN32_bios] =================");
/ T) F" E4 K: R) G7 a    GetWmiInfo(Memo1->Lines, "WIN32_bios");* E- `; Y* e. _; X% A" f* l
   Memo1->Lines->Add("");
3 b  g2 P1 B! [" ~3 i/ `1 m}
- M# B1 \3 z# V$ g  u& O4 C
( j: V. r/ |& G0 ~# V--------------------------------------------------------------------------------
% ^, m/ J" \/ s0 m/ W7 a  R/ F7 \. C& `* D. d' h
WMI 可以访问的信息类型有:
" t, t9 |( @0 `" O  G+ J9 r   Win32_1394Controller
" _1 e3 q/ W* P1 F+ S   Win32_BaseBoard  S: |! u( o$ X. r' q4 Q
  Win32_Battery
1 V* U* z( r3 s+ y   Win32_BIOS4 w1 j) F( T& [+ F, a
  Win32_Bus
; K6 v# c4 k; z  Y   Win32_CacheMemory. Y* @; j3 F7 Z" m
  Win32_CDROMDrive1 c" n2 \* _/ h7 J$ t2 I. {6 c0 R
  Win32_CurrentProbe
! m0 K9 c( d1 T/ y- p   Win32_DesktopMonitor
4 T# e" u: B( f% Y   Win32_DeviceMemoryAddress8 {( F4 z. x% B: F4 k. Y4 n, v
  Win32_DiskDrive7 Z, L: b( X9 p8 v- c
  Win32_DisplayConfiguration
& m) Z  g" E6 M0 l   Win32_DisplayControllerConfiguration5 c* L9 {, C* G8 P8 d. Q
  Win32_DMAChannel
* D; `9 B8 R( g% S; v   Win32_Fan
8 s3 p( T; m2 m* w1 x   Win32_FloppyController9 l# V& P9 [4 L4 T$ s* j9 G
  Win32_FloppyDrive
" c* a+ y0 e8 v; i  H: g   Win32_HeatPipe
' U1 k" q0 t" i8 Z- R' |   Win32_IDEController5 s: |* J  ~9 c8 t# _" v- @
  Win32_InfraredDevice3 I) ?0 x  H' y2 k
  Win32_IRQResource) O' a* \7 o" x( t
  Win32_Keyboard0 H# z  y( k4 ]8 ?2 u2 q; [7 o
  Win32_MemoryArray- j$ j1 \2 l9 S% R/ r$ x- N+ r
  Win32_MemoryDevice
4 f0 z) _1 E. i: }  Q' n' \   Win32_MotherboardDevice
# l8 _6 g. c7 z   Win32_NetworkAdapter; O. D  V2 m- o4 q& q2 r# H. C
  Win32_NetworkAdapterConfiguration
, c/ K5 B6 B; d' C   Win32_OnBoardDevice
' C7 P7 e% \2 l   Win32_ParallelPort
) v6 r5 N! w4 J1 t3 w; S- b2 h   Win32_PCMCIAController
9 N9 T9 `" o; Q* q: u   Win32_PhysicalMemory5 C& z4 h1 n1 Y- |$ k( M
  Win32_PhysicalMemoryArray3 T3 s+ x6 j3 H) |2 T
  Win32_PnPEntity& }, t0 O$ B% O8 \: t: w
  Win32_PointingDevice1 A9 n" a3 C2 {
  Win32_PortableBattery
0 _7 v9 F: U+ w# _+ ~   Win32_PortConnector
% \  L6 ~. F; T: r   Win32_PortResource
' C* j; n6 }, C5 W- J4 }   Win32_POTSModem
4 R5 [) S3 f8 j9 a   Win32_PowerManagementEvent+ d( ~, {+ G, q5 n+ Z" b" @' ^
  Win32_Printer
' V' t, m, m9 q) y+ ^* t   Win32_PrinterConfiguration/ ]& p: a: F5 ~* n' ^* m
  Win32_PrintJob
! J* \$ q8 `) f; M# y. R" G9 {   Win32_Processor" t3 }/ P, R. i8 _1 ]( l8 G  [
  Win32_Refrigeration
. d6 e+ G; ~  c' K. G   Win32_SerialPort
& y# b  G( O( }, D7 l* k; j   Win32_SerialPortConfiguration$ ?+ f0 M+ s0 X; V7 I$ x5 n
  Win32_SMBIOSMemory! T; ]3 D- }4 Q# P  \5 m
  Win32_SoundDevice( [4 r# P; E  k: f! Y7 H
  Win32_SystemEnclosure
. C8 e5 t" T$ ~, o+ r3 S& @, a6 f   Win32_SystemMemoryResource6 g$ l7 ?( W2 E" j2 z" N- y. H* i5 |
  Win32_SystemSlot
2 b4 {; a% Y3 s$ f  \1 e  O6 W4 z   Win32_TapeDrive9 a& P8 J; ?" @  K4 Z7 ]  |$ d
  Win32_TemperatureProbe' s! H- W3 W- S# J8 B6 {" j( A
  Win32_UninterruptiblePowerSupply
8 p+ i/ ~, k4 d' t$ w* m   Win32_USBController
  y! ^2 \" [* U: J, b5 M8 U   Win32_VideoConfiguration
- ?/ `$ R0 I, D+ |7 M   Win32_VideoController
- y3 c  p% s, `& O7 D   Win32_VoltageProbe) r8 D( n' a# a! i; z$ e
/ l$ e. k$ Q8 I- @" M
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-29 10:38 , Processed in 0.019923 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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