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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)) t2 P$ B0 C& X5 J
% U: C( F% Q9 [8 o- Q. e

* m  f- `) s; S, e% y* N3 q--------------------------------------------------------------------------------
% D! B+ r0 C' S% C6 f0 NWMI: Windows Management Instrumentation (Windows 管理工具)
  B8 J  K9 z; F0 u   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 6 i2 V1 X0 `7 ]3 s
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。+ _/ f/ j( l  b; c' O  i& |
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 # H7 U/ A2 ?. @, i* D

2 Y" q3 }* q3 P& a* v/ U) Y9 I3 W--------------------------------------------------------------------------------7 z- r6 m. g" h/ S
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
; r  B% D8 W' i2 D1 E& L: Y  X
- i0 J6 v& ?% x8 r5 b  S* m. x--------------------------------------------------------------------------------
& ?3 X. Q3 x, c; x① 初始化 COM 接口:
- |2 X$ z. @9 S8 d6 e/ J3 ?+ O- I   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
+ g( J2 ?" f3 d1 y1 x( W0 b   这两个函数在 #include <comdef.h> 里面定义。) n+ |- I2 b% O& @
* \9 j/ @7 M$ E5 f, W1 C3 ]/ A
② 获取访问 WMI 权限:% T4 }7 N) B0 {( d8 W6 p
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);8 s0 o* J2 Y" G6 M% d
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。+ v- }' n9 Q  t+ V4 ^' h

3 n" `! {1 `' W) x6 I③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
, ^! Q3 I& D, i3 Z   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
7 |1 o: i4 K7 V# P
' C2 a& c' o3 |4 M6 B0 B8 v7 Bvoid GetWmiInfo(TStrings *lpList, WideString wsClass)
7 e9 v$ N- r0 j. `{9 \4 ]$ V/ X2 Q
  IWbemLocator *pWbemLocator = NULL;
: U" h3 v/ h! G  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)" n* w" ^' X4 k# N, o7 G4 x
  {0 Q) B/ O  X8 \0 v7 J
    IWbemServices *pWbemServices = NULL;
5 M$ @1 o; b/ K$ o9 D     WideString wsNamespace = (L"root\\cimv2");) [( k' p6 E" e& |& ~
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)9 \( q$ j  d+ z# e' h- P7 |7 x3 U
     {+ ~2 s$ w" J% k+ n& L$ h
       IEnumWbemClassObject *pEnumClassObject = NULL;4 w0 f4 s. Y3 ]6 Z. W& A* N: B
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;& h" v. a8 g* T* ]
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
/ V8 n( G) k* }         {. _- i3 G9 K9 r, C# Q; l1 y* Z
          IWbemClassObject *pClassObject = NULL;( R9 D( D# c# p3 t5 J0 s# [2 ]
          ULONG uCount = 1, uReturned;# F" U+ {3 X  J, j. m
          if(pEnumClassObject->Reset() == S_OK)
; C* x; `* Y* t* v1 |            {
: Y4 R6 S/ b; |8 M* e* M              int iEnumIdx = 0;7 F# \  _* F! a) K
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)/ G; l5 e, H8 s7 J
              {
8 M; T  N: x$ s( O  ~3 D- M" ^8 L                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");1 }0 U# B; Q9 y- R

$ ^) k( c, j  M* g  c) n                 SAFEARRAY *pvNames = NULL;
2 f9 e  E- D: O- O0 X' B% d3 s0 o                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
5 K  P+ ^) k% ?" f. w- {  E                  {
7 `" \% @& s. H  X3 Z  s                    long vbl, vbu;
2 [& R7 L6 P$ [                    SafeArrayGetLBound(pvNames, 1, &vbl);
0 L% \+ J6 t7 y% _, W) Z. s& J                    SafeArrayGetUBound(pvNames, 1, &vbu);, G# H: `: C: j! p
                   for(long idx=vbl; idx<=vbu; idx++)+ |4 u' a0 a# ^: ]
                    {: t2 x  ?8 R  z5 D9 m
                      long aidx = idx;1 c, a- @, {/ [; Z9 N9 e3 q& z4 t6 u
                      wchar_t *wsName = 0;1 J/ G# Q/ x3 J. ^9 S
                      VARIANT vValue;
* [9 W. H4 b( s  ^                       VariantInit(&vValue);. x( }+ c2 J9 a/ A
                      SafeArrayGetElement(pvNames, &aidx, &wsName);
  K$ L9 d& t9 p5 _4 }( }7 p, }3 [  ^# @! b$ H9 q: |  T
                      BSTR bs = SysAllocString(wsName);  h5 S+ k; A8 v+ e
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);3 o* I; I( U% N" H" a; G/ f$ Z6 ]
                      SysFreeString(bs);
% n. W$ e- H4 I- \2 s
0 x7 y4 d6 i  e9 P                       if(hRes == S_OK)
5 L+ Y* ^5 L; @9 e4 v2 I                        {$ d* h* E. E3 q- I4 p
                         AnsiString s;5 y. @% ?+ r3 ^* v7 z8 j9 b2 C
                         Variant v = *(Variant*)&vValue;
# W; F0 ?3 _: P+ U1 o                          if(v.IsArray())
) P" }( x5 W$ _2 Q+ U                           {
' N" \7 G* J( c6 E- r4 n                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)0 d- N2 U' i' r
                             {0 d' L! h5 r0 X" ?5 P
                               Variant a = v.GetElement(i);
. {) n" x8 T8 I  R+ @                                if(!s.IsEmpty())
& e( ^8 I. D1 m0 ~) c2 y7 n3 o                                  s+=", ";
/ ~3 F7 g/ r+ B                                s+=VarToStr(a);
+ K! O2 b+ {1 o% ^1 I                              }
/ S3 j2 w# e, J7 c! _                           }
, M- U, j$ i$ l& E. w5 d                          else
% E; U$ S4 \% l' I                           {
& l9 \% v9 X$ U, e9 I6 v, b! s9 _                             s = VarToStr(v);: x" x4 r; ~; ~  }* O* F6 o0 c: }
                          }
3 _: `  e) @+ O' _                          lpList->Add(AnsiString(wsName)+"="+s);
, e0 z. K) i$ P                        }/ D* Y) K3 {! t/ Q

7 H: p1 t/ j; G0 n                       VariantClear(&vValue);  M$ f, E6 Z3 z/ N( V
                      SysFreeString(wsName);
, u, S9 ?3 B- M. A+ o2 F$ j! T                     }
+ q7 `1 j8 D! @                  }2 j+ [8 [5 a; r
                if(pvNames)SafeArrayDestroy(pvNames);
9 V7 }; e) ?1 S( w# q; A                 iEnumIdx++;# X5 Q2 g0 v  \/ W( c( b8 d; X! C
              }& l. r+ `4 O. l
           }
5 e: h3 u1 D/ j0 f) t           if(pClassObject)pClassObject->Release();; F' `4 h' k4 a4 X! i
        }
: p& ~' m/ S  u8 J5 K' j9 W        if(pEnumClassObject)pEnumClassObject->Release();5 Q. Z0 D, v. A3 V
     }
9 U( r: E8 k) N3 m" W5 q- I     if(pWbemServices)pWbemServices->Release();
1 @( E2 K9 ?, p% e) ~6 E+ y& G! ?   }
4 t, V8 o* J& ^& i  if(pWbemLocator)pWbemLocator->Release();/ \% P6 \& _# j- L5 i" u
}) ?3 y: I/ S( v8 C
//---------------------------------------------------------------------------
2 G% t( C2 C" l; a7 @* h; T7 C
- w6 u- U! Y9 Y// 通过 WIN32_bios 获取 BIOS 信息:& U0 b1 |& [8 q7 }
void __fastcall TForm1::Button1Click(TObject *Sender)
- @7 F5 j* r5 y7 T; c8 J, ]# h6 l2 {; i{
5 i- k' g8 b/ @) I9 `# A% p3 w' a0 a    Memo1->Lines->Add("================== [WIN32_bios] =================");
6 D4 L, {; l' {" ^8 s9 R& k2 F7 {    GetWmiInfo(Memo1->Lines, "WIN32_bios");. ^& @! W, E# L2 O2 l
   Memo1->Lines->Add("");
! ~4 D# x& p( V; U& K3 J}
$ ]: |$ Q3 D& l9 ~+ L! I0 {% r8 c
4 ~9 f9 |! F! |7 a3 }2 r. i--------------------------------------------------------------------------------
4 |" K' f1 o1 {: c; s* C6 U! {( r+ i* [, r: x2 _: l8 T
WMI 可以访问的信息类型有:, p1 Y! b9 _/ U  B
  Win32_1394Controller# \/ I! U- g2 m; j5 L
  Win32_BaseBoard4 r5 B8 z1 |, {2 b  A9 ^3 b/ H
  Win32_Battery5 q/ b8 H7 x; O, n$ P" E% L
  Win32_BIOS" n2 A/ b0 q/ s2 ?+ N  T6 ~5 I
  Win32_Bus
6 C6 p7 q5 V( A* ]  D" r3 h   Win32_CacheMemory
2 H0 w3 o* e. |% M- |/ n   Win32_CDROMDrive4 z  n3 Y- D" C( n  F
  Win32_CurrentProbe
% D3 G, h9 U- `  P8 i& H: ~   Win32_DesktopMonitor
3 Z( e: Q& t3 l/ k) S   Win32_DeviceMemoryAddress
* n" n# ]) [$ ~+ k: B) j   Win32_DiskDrive
1 m# K$ v# c. T   Win32_DisplayConfiguration
8 ?$ s0 B/ n3 _9 d; y9 I1 P3 P   Win32_DisplayControllerConfiguration0 @3 g" `. {5 ~: Q. v5 c
  Win32_DMAChannel
; z% _. f6 o- y   Win32_Fan
' `5 s* ]9 E4 }( f   Win32_FloppyController
& K$ c6 |/ |+ }" n& X2 y1 l1 |2 ~. Z   Win32_FloppyDrive
4 p9 a) v# @* E# t. _; H( s' H: S   Win32_HeatPipe
6 _; y: o8 X$ k* k   Win32_IDEController
" l& i. g, i- ^1 p' q; x  P& x& s   Win32_InfraredDevice/ o& h& A' H6 |# Q, s/ P
  Win32_IRQResource0 D: G. P, d0 Q! E' Q: S
  Win32_Keyboard
/ {5 O' S0 ]1 `% ^( w* p  o9 v   Win32_MemoryArray
2 K5 s- i6 b- A   Win32_MemoryDevice
% Q' m5 I0 {* D1 j& j) t$ f5 C   Win32_MotherboardDevice. A; b4 u1 B; G1 g- c: M' _
  Win32_NetworkAdapter
. @8 w0 r: P+ T# V9 y1 @: {   Win32_NetworkAdapterConfiguration
7 b/ L) t8 W2 c% |' B   Win32_OnBoardDevice3 L$ f. r( _' j6 x$ U8 B/ a* p
  Win32_ParallelPort
/ i+ M( F3 R8 C8 w9 r) E6 Q  u- R   Win32_PCMCIAController
- S* ?4 u. Z# f1 h' D2 U   Win32_PhysicalMemory
0 f% w( K# y' ?   Win32_PhysicalMemoryArray/ n/ K7 c& [6 s, F. r
  Win32_PnPEntity
( z5 G7 g4 G4 l2 O8 t   Win32_PointingDevice
) j) g7 J* K3 \( G5 W   Win32_PortableBattery: X# _9 e% i. m* j( o4 e
  Win32_PortConnector3 S3 A) p4 r. M0 n& B
  Win32_PortResource
, ]! @" g' S7 u3 i9 u; z0 O   Win32_POTSModem2 [3 b( m( C* i- b9 U
  Win32_PowerManagementEvent
- ?& N8 W: ?7 d; k: s# g" Y1 G8 Z   Win32_Printer
, a' A$ U0 p5 H   Win32_PrinterConfiguration5 U' ^, w/ [8 a0 c& z
  Win32_PrintJob% C, j2 c6 {. H6 m) Q
  Win32_Processor- d; n1 f- j6 d) f$ X9 B3 G! A5 ]/ T
  Win32_Refrigeration% F+ H/ i4 {+ d2 g: e) Q
  Win32_SerialPort
  {( q# A3 p) I8 u+ G7 H   Win32_SerialPortConfiguration8 {6 b9 q) K/ O
  Win32_SMBIOSMemory% B( Q- i* f4 P( [8 \
  Win32_SoundDevice- }: y6 `1 z+ f9 t$ V
  Win32_SystemEnclosure7 E2 G$ }- [2 d! ]0 O
  Win32_SystemMemoryResource2 P6 ]8 L9 P( N- l9 R* a
  Win32_SystemSlot& j0 \/ |( ~  r8 F8 f+ {' x, H
  Win32_TapeDrive9 n3 R5 C) F- @$ j1 y  O# y2 Q
  Win32_TemperatureProbe
! ?, j4 ]8 N3 ~! ]7 ?9 m1 o   Win32_UninterruptiblePowerSupply
  Z: H0 I+ A8 H5 X& W   Win32_USBController
% ]" b$ V% |& ]/ e7 }* ~, Q   Win32_VideoConfiguration. U: X' K* g1 r8 S6 ~9 F
  Win32_VideoController
9 K& n/ d/ v: u; ]  t# p   Win32_VoltageProbe
, Z. P/ s! D4 C+ ~
' `$ y1 w1 F; w3 R" D以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-7 05:41 , Processed in 0.019875 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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