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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
" ^( A1 {( h+ \: {" u1 u
1 M( ~' u) D3 B/ Z# b- |
; q* R- I) F( m+ O--------------------------------------------------------------------------------
) ~9 w, Z. h$ ?7 T7 ]1 U% m+ xWMI: Windows Management Instrumentation (Windows 管理工具)
& V; T7 ?: w2 n+ J( [( P   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 + d) \* y$ h) F2 i' i0 v4 D
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。
! J/ p+ m. s" [. ~4 `6 M   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
) `3 E- k( l6 w  Y3 o' ?
& o8 \+ F. i4 ~! z: t8 w--------------------------------------------------------------------------------
3 H: l9 f" }. sBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
- w1 j6 `( Y+ {7 b1 E8 v
, N9 j9 t/ g: e% a# q--------------------------------------------------------------------------------
' U1 p& A5 p/ z3 ?, u( J, t① 初始化 COM 接口:% Z! X3 k; ?5 m. O
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
- C, O1 R+ ~* d1 `7 t, w   这两个函数在 #include <comdef.h> 里面定义。
  ?7 G, j8 W! x( |  x- k! w4 s- W( @9 G9 ]
② 获取访问 WMI 权限:) B- w# j: @( L3 l8 t" C! z
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
; V3 L( K, @/ m7 Q   如果这个函数返回 S_OK 获取权限成功, 否则为失败。
* U. y. o" J$ Z0 X3 U( h
5 z3 d/ p! S2 O6 o2 W8 h③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
1 o( k# X% p* z2 n) p; C   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
, |$ v: T0 n* q/ }6 ]
) B  u3 @( p1 n. G+ mvoid GetWmiInfo(TStrings *lpList, WideString wsClass)
) z5 }. b/ ]. o4 I{& I* P4 C7 A. M$ D
  IWbemLocator *pWbemLocator = NULL;
% D! t) u! b+ O9 u5 `8 h4 m& t  j  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)- x( m- y& K# }; a4 T9 @
  {
' [! v; C- j( G/ U# v. t     IWbemServices *pWbemServices = NULL;
5 O) U2 y5 ^+ n0 g& l5 Y( x     WideString wsNamespace = (L"root\\cimv2");: B6 V5 U" ~1 B+ y3 L4 m/ M
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)% W# N! ?8 ?& N: h" r9 P# `$ ]
     {* _9 M. ?' W. m& i" n  s' P$ I
       IEnumWbemClassObject *pEnumClassObject = NULL;
" o+ j7 x9 u4 O( t+ C3 j6 w/ ]9 M        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;* S1 d  Y7 i* r8 l% |- E
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)3 L5 r/ r- s& F5 L) p7 J
        {( ?$ r6 n, G. ?+ U
          IWbemClassObject *pClassObject = NULL;
9 \  X6 S5 L; [. P3 p6 U           ULONG uCount = 1, uReturned;  {2 z4 L! I8 W0 ~0 I/ l  T3 e# Z7 Z
          if(pEnumClassObject->Reset() == S_OK)5 c7 t! [- _: |) p! e( B2 u' [
           {. D/ [5 Y" K* R$ v, ^5 ~2 x
             int iEnumIdx = 0;( |' j. Z5 |; Q
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
+ v* ^: q  R6 K4 z& ^$ e8 q               {3 e, u2 i6 T5 P9 U: M( F- [' ]
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");% F% i9 a8 I3 H

  j& `. S& {$ q! B2 _3 Z# h6 _                 SAFEARRAY *pvNames = NULL;( B8 _$ b% |- }6 w, g
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
8 b/ r  q8 |2 r# }                  {0 K; T8 N# Q. J% y$ q* k  ]
                   long vbl, vbu;8 I% \- f7 B$ ?  c  ~; K
                   SafeArrayGetLBound(pvNames, 1, &vbl);: }/ `3 W- _5 Q7 u
                   SafeArrayGetUBound(pvNames, 1, &vbu);7 E) c  A5 f* V# t: t4 `& l  L
                   for(long idx=vbl; idx<=vbu; idx++)
+ X" b7 H) M1 w5 Z( H7 ^. r                     {8 }% [) |% q3 q! k9 C, z
                      long aidx = idx;
* K+ S' x% L' d0 }/ }' ^: Z, D                       wchar_t *wsName = 0;: \3 w- K) H8 Q0 e) F
                      VARIANT vValue;
" L% @, t' b0 r6 ]( g' t                       VariantInit(&vValue);
2 j( U. j6 f5 V) o; u0 S0 J3 A                       SafeArrayGetElement(pvNames, &aidx, &wsName);
  [$ F: B( d- _" A: |% b' ~# ~$ R  P/ n" e
                      BSTR bs = SysAllocString(wsName);: v3 U" q5 m3 E9 L$ P( f
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);  y7 Y) \" b* Y0 t1 f5 C% w
                      SysFreeString(bs);
- }+ ~/ y% x7 ^% H( e+ C/ n
' X8 e& W& k% \- f+ Q* T8 b  V                       if(hRes == S_OK)$ q% K+ a2 P, f6 P
                       {, l2 j3 l% ]( [# S$ ?* l! n
                         AnsiString s;$ a! R( `# l1 _5 @, F
                         Variant v = *(Variant*)&vValue;& [3 c# y; `8 C# X  O3 K4 O
                         if(v.IsArray())2 B, E! l- |+ O
                          {
) e. M. s2 f- c" d' P                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)# Q( c$ L6 c& H. H9 [1 Z
                             {3 n1 `. I9 S# h3 Q& t( O
                               Variant a = v.GetElement(i);# j8 z$ H% ]( O1 U+ v5 R+ j9 h
                               if(!s.IsEmpty())
6 b& ~( p% ^$ @, j5 x& K5 [& l                                  s+=", ";- m* z$ R# O0 g: ?6 |' ]
                               s+=VarToStr(a);& i, O$ W: A6 V+ t3 b
                             }
. x( o* m+ z( j1 o' j  I2 X# f                           }
/ M+ i! _% E4 F                          else
* _( U: l# l6 f* a/ y                           {
% }" {  k7 ~! I% I" r                             s = VarToStr(v);+ d7 b5 S  S. V: G  ^  Y" T5 V1 I
                          }
" n% B$ u$ _1 Q. F0 h: y* e                          lpList->Add(AnsiString(wsName)+"="+s);
0 C. P/ W. @& S+ y3 E9 Z                        }
/ ?5 k1 x# i# S2 p  W+ l9 J9 O/ W! B) ]. Z$ ?) q- O
                      VariantClear(&vValue);
5 F1 u- t* N: S                       SysFreeString(wsName);
" J7 U; v/ s* N7 g* A" W6 O                     }) M. \* y0 r6 Y$ E% T6 E
                 }$ `/ W6 \+ l: `/ l# N
                if(pvNames)SafeArrayDestroy(pvNames);
9 l) j$ b/ W* e4 Y. T( [$ R                 iEnumIdx++;
# g  ~  t" G" h9 `" s               }/ u+ a8 h- u9 v( J: Z* I8 H# _$ q
           }
$ o4 k+ u; H4 Y* o5 y% c           if(pClassObject)pClassObject->Release();& Q" Q' E6 I% J# Z  U( [
        }
" l/ N3 c; K3 [( R: R3 ~8 J        if(pEnumClassObject)pEnumClassObject->Release();! X) c3 x- A  q0 K
     }& S# A$ H# `, w# A% U6 _
    if(pWbemServices)pWbemServices->Release();
5 i% z8 M8 E  X+ M6 s3 v* H3 |1 A2 a4 P   }
* l+ _$ R5 S7 ^  if(pWbemLocator)pWbemLocator->Release();
: M3 R8 R8 @1 l/ S- `: t( m}
% p# V6 G, i+ i* v//---------------------------------------------------------------------------
* Z- n6 ]3 ^# b% Z* S% s# s3 B: r3 E1 S9 v9 d8 Z8 ~0 x
// 通过 WIN32_bios 获取 BIOS 信息:
! b8 y9 Y9 f8 C6 Mvoid __fastcall TForm1::Button1Click(TObject *Sender)$ p3 f/ ?( _& y: Y' d1 _, z
{
" l5 a1 H; ?. q  k0 I  H, l* b7 B    Memo1->Lines->Add("================== [WIN32_bios] =================");
. ^* \9 H% F8 Y  N" C    GetWmiInfo(Memo1->Lines, "WIN32_bios");, O4 R1 \3 v8 ]: f( w& O& I7 ?0 p
   Memo1->Lines->Add("");' g" x) [6 |% s- R3 {: Z. k( N: V
}- c6 v' d, }: y# V2 U

! K4 y( C' d. U$ ^# D- [8 o--------------------------------------------------------------------------------
" c/ J- u( }7 j' s' i- |# d& y3 r$ O9 n* }4 h4 n
WMI 可以访问的信息类型有:
8 x: i& X  j/ i3 w8 n# i& _: e; T   Win32_1394Controller, E, Y7 N5 ?2 ^$ C( T9 l9 H) k
  Win32_BaseBoard
/ N! W5 e3 L/ m& u6 x" w( ]7 n   Win32_Battery; ]. W$ _5 O$ [& X- l( l! o* N
  Win32_BIOS9 x3 G* q( U* n6 R, ^" f, k
  Win32_Bus
8 q1 e8 C$ _  F; l" Y9 |   Win32_CacheMemory. S2 q- O/ n  O- C4 e/ M
  Win32_CDROMDrive
/ H8 C( q& Q; G$ ]   Win32_CurrentProbe
7 S9 |" T( Z' n/ y   Win32_DesktopMonitor
5 \5 |# p, p( M* l1 Z4 f8 X! \   Win32_DeviceMemoryAddress' r- F/ a; l9 l& i
  Win32_DiskDrive
0 J3 i% \" [6 {( j9 A8 n* Q   Win32_DisplayConfiguration
3 Y6 c- T# }9 C0 K4 \3 |   Win32_DisplayControllerConfiguration0 ~2 t+ _' R+ H6 j. v
  Win32_DMAChannel/ Y6 b: F- W9 D& }" {! S
  Win32_Fan
( c/ E$ M/ u% F8 Y/ j! B* S' I   Win32_FloppyController
4 r! u+ b2 H2 m   Win32_FloppyDrive
1 B" U/ Y- P. c9 J& q   Win32_HeatPipe: F$ \! L* O0 w# l% e
  Win32_IDEController/ e1 h$ B9 W2 y3 N7 d3 C
  Win32_InfraredDevice
  g5 K7 w( [: `/ l0 n8 m+ Q' Z   Win32_IRQResource
" y3 n  X+ [" J: K4 {) B- U) O, }   Win32_Keyboard( {' a) ?; O, s- c+ }- n
  Win32_MemoryArray7 j5 ]6 a# f) [' N  V8 C( A" a
  Win32_MemoryDevice
! V( ]4 c: e  U3 V3 k   Win32_MotherboardDevice
! K$ D# x/ e9 [   Win32_NetworkAdapter: a' r( `0 J4 S: l. j  I
  Win32_NetworkAdapterConfiguration
' }5 g+ M+ W3 L   Win32_OnBoardDevice
9 b6 k6 K- W! Y   Win32_ParallelPort
& f& Z( c: i1 g( ^) Q" ]& f. m. z   Win32_PCMCIAController
2 Y5 h4 o* N2 R$ l) ~0 M   Win32_PhysicalMemory
- Q: I: E, u' G3 }1 H: x   Win32_PhysicalMemoryArray
* f- L1 W& ?! `' n" t, v   Win32_PnPEntity
% h% \4 U. b/ Z( t5 c6 Y9 M# l   Win32_PointingDevice
) n) ~$ h9 f2 w) L! q9 N" a5 t   Win32_PortableBattery
# v2 ~- N- O0 C: e0 ], ^/ l   Win32_PortConnector
% e$ E7 i: e1 S$ s3 g! U$ H: p' c+ `* I   Win32_PortResource
+ R" a3 I  W. d9 T   Win32_POTSModem
) G  N: G/ w# a" @" E: d1 F   Win32_PowerManagementEvent
& V  o% w: G/ S  ?   Win32_Printer
7 w% }0 q( }# z- `# C   Win32_PrinterConfiguration
' S3 t+ Y3 G2 p) Q   Win32_PrintJob" }9 ~- y5 O4 m8 z2 }) S6 c. `
  Win32_Processor/ p) J9 Q! S; W+ }+ ]: t8 j1 p
  Win32_Refrigeration* p4 G3 d  j3 _4 g6 F7 _
  Win32_SerialPort, M3 r& T( R5 E/ n
  Win32_SerialPortConfiguration
# T7 u7 x, x) \  d4 q- [1 H  z0 q   Win32_SMBIOSMemory
3 A+ T7 ?) u0 q3 G* f- W$ t% ~& f* D   Win32_SoundDevice
7 W9 o6 J5 z, P1 t) L   Win32_SystemEnclosure+ b  c8 b+ j4 ?/ y
  Win32_SystemMemoryResource
+ t; {3 r! K+ ^2 w   Win32_SystemSlot2 F7 d( J5 b6 D4 v4 ^4 b( m
  Win32_TapeDrive! x0 z9 r: ?3 K7 _8 c0 z
  Win32_TemperatureProbe
# `4 Y% d, H4 V1 z   Win32_UninterruptiblePowerSupply( I5 C# [; q: c$ l* A! s7 x
  Win32_USBController
. [0 n& A' t8 V7 k   Win32_VideoConfiguration
0 O4 U7 B% D5 u4 E4 m   Win32_VideoController
+ L1 T+ _& j0 p. i* }, b   Win32_VoltageProbe
. t/ a4 A$ H2 }- C6 m! }' A! F; V8 E4 a. D" Y5 C
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-28 16:30 , Processed in 0.024469 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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