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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
" j( B8 s  h* |/ I; n" \& @: ?0 E% H0 W" n8 C7 I2 e$ ^) p" R

# l3 e! x- b0 L; S& J3 U--------------------------------------------------------------------------------; y& `$ {5 u3 y/ r  o* T- o  N" S
WMI: Windows Management Instrumentation (Windows 管理工具)
0 M9 K: D) m7 |" H   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
' W7 _2 r: H5 ?3 ~# ?6 K8 @   利用这个工具可以管理本地或客户端系统中几乎所有的信息。
; ^) y  |' J2 I* r1 W( U   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 9 N& W: M9 B: K' D! ]( q9 E

* l  s0 Y, o$ W: N0 N--------------------------------------------------------------------------------) i( ~7 q! Z" e0 {! f1 t5 H5 o& U
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面* l3 R7 l3 F% f+ y5 W, t

0 n# G# ^6 o& [0 z  f3 T  Q--------------------------------------------------------------------------------
& B5 q& d' c9 ]" @0 g9 b① 初始化 COM 接口:+ w. C! q5 T* Y( }$ Z3 [9 A/ Y
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。  X2 v8 z- ^3 q: k+ |. ^# L- L
  这两个函数在 #include <comdef.h> 里面定义。
& h! v. w; ]6 l" P! [3 ?: x: [
4 b9 ?, S& i; F. m2 h( N② 获取访问 WMI 权限:, m% c9 q! _9 B. C% p
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);* H- N) E/ I; `- ?
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。6 d2 G( `1 j7 S# B9 u7 n
4 @8 b: ~6 n) U+ c& f# V
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
) z4 P. g7 e$ K; s& s   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
, ]/ E- {; d6 D1 ?8 f/ g4 \- U. Y; S7 J: t. Y
void GetWmiInfo(TStrings *lpList, WideString wsClass)6 h% t8 y( y/ X
{
( I. ?7 N# S+ G7 L/ f  IWbemLocator *pWbemLocator = NULL;
; |# }* z" e( m+ [7 B2 I4 T" F- l& {  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
6 M3 [# n6 ]/ s7 w   {8 S- y8 d8 Q% ^' B' R# v
    IWbemServices *pWbemServices = NULL;9 z0 i7 _6 t! j5 i- V5 f5 L6 q/ v- o
    WideString wsNamespace = (L"root\\cimv2");
; y2 w+ e5 b. |7 L; o. A     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)" p  ?# @- \1 O" x$ p
     {4 q* q$ L" v  m+ R- c
       IEnumWbemClassObject *pEnumClassObject = NULL;. u! |2 m7 n6 d; H. i% @
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;% Y( M, E: e, Y. G+ Q3 i
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
8 [1 q* ?' S6 [" c2 _2 q         {
3 T% q+ ]; ~5 P+ F& ^4 w           IWbemClassObject *pClassObject = NULL;
) t$ |# J% b" X% Q           ULONG uCount = 1, uReturned;
$ |4 g/ X% N( f# G           if(pEnumClassObject->Reset() == S_OK)
; \1 x/ H; ^# }4 n; m1 _( I            {
7 K* O. B4 R1 x              int iEnumIdx = 0;
; m& }: D" S+ I# g' Y9 x8 U/ ^              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
4 I! ]0 A2 z8 R- q0 @: M               {. X5 s1 z2 l2 r3 y  O
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
: C, ?* B, A: m' U
% B: k: \3 `) b) }4 g) Y1 y, R4 \% G                 SAFEARRAY *pvNames = NULL;
) r6 r* x9 M- f                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
( M+ r0 k5 [; t6 X9 [* r/ X                  {
- \, p' G: K' |5 y/ @                    long vbl, vbu;
( a' c4 V" s; }3 k0 N& w/ g/ |5 P" a. @                    SafeArrayGetLBound(pvNames, 1, &vbl);: ]2 L  ]# C% [" _8 h0 _. C
                   SafeArrayGetUBound(pvNames, 1, &vbu);
0 @" w: |: Q: |5 v3 d7 n                    for(long idx=vbl; idx<=vbu; idx++)
6 p$ |% S$ k- h; f                     {
" `4 U4 [: |6 S                       long aidx = idx;, E" w$ s# c! J
                      wchar_t *wsName = 0;
' R/ v8 c+ `1 H. W" b- w  O* n: F                       VARIANT vValue;
  s0 }6 S) [: ]+ G& M  z; _                       VariantInit(&vValue);6 p  ]$ J# X- `2 ]7 D
                      SafeArrayGetElement(pvNames, &aidx, &wsName);6 O7 `! P# P% m7 [+ d5 q  R
( d: v; p5 r: t: v5 J1 H& w0 Y& c. O
                      BSTR bs = SysAllocString(wsName);
% e4 ?7 r' n1 ~" b  U( l/ j7 l                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
" ^4 ~5 i, E5 T% }! ]                       SysFreeString(bs);
  h7 n% ]! ]% D, w9 w& I* K: u
0 X$ b1 K3 b! O# K; Z' ~# b                       if(hRes == S_OK)
6 v/ v1 z6 a4 J9 G) \                        {
; r: H9 H, U& x% t; E                          AnsiString s;1 a0 y4 U" S2 ]8 R
                         Variant v = *(Variant*)&vValue;) I  X/ Q5 u9 h: h8 L1 v
                         if(v.IsArray()), }3 E; w8 M7 I, q' x+ N7 \
                          {
9 y+ ^- e  w. t6 d0 S                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
' s& Q9 u0 `. D+ E) c* X                              {
& u5 V  f" F% ]+ T; }5 X                                Variant a = v.GetElement(i);3 {7 Z0 W6 y0 b
                               if(!s.IsEmpty())
+ e" T3 |% w/ E- W; J                                  s+=", ";
7 o7 ^. n$ o* K% M: V$ d) J) C                                s+=VarToStr(a);$ u7 G0 w7 X4 O! g
                             }* b6 Q: e! l( ?. I8 |
                          }3 M7 H- m6 [9 s' H) S  k# {
                         else' _8 w3 ~4 j0 _2 J, L! N! K
                          {: y' w% S: u3 N; Z4 ]8 t' [+ b
                            s = VarToStr(v);2 ?! a" l6 q8 t1 [
                          }6 U' P0 ]$ ~7 M
                         lpList->Add(AnsiString(wsName)+"="+s);' q) v$ p. t) \8 @3 c
                       }
8 ]+ U3 z7 w; f( m; G1 b2 E9 O, D3 q
                      VariantClear(&vValue);
& U, q; a5 i# r* C6 }8 O& |8 J% @- g                       SysFreeString(wsName);- S! e, K3 @& q- y; w
                    }& ]4 |, c  `' C& V4 a$ }
                 }
% b8 V$ q' P  m1 W0 H, n4 y4 Z& b3 k                 if(pvNames)SafeArrayDestroy(pvNames);  e& @1 @( H* C5 c5 x$ W$ f1 U
                iEnumIdx++;9 y- F' c& D1 S( t" _
              }
: M. w- A/ w5 J4 _0 D; `            }
* ^' p; T, V& F           if(pClassObject)pClassObject->Release();
6 E6 [3 b# n6 v# @; L         }
6 c1 x, y! g: Y# I        if(pEnumClassObject)pEnumClassObject->Release();
" f# t; _, F) [7 P      }* ]4 ^! C; _7 F! p4 J
    if(pWbemServices)pWbemServices->Release();
3 C) m% g& C: m1 @2 t   }8 t( n6 x" H8 X+ C
  if(pWbemLocator)pWbemLocator->Release();
0 Y' l; Y7 ~! L4 ~8 E% f; @}. Q* D. U! ]6 O
//---------------------------------------------------------------------------* A0 ?( R  V& |6 \4 V9 V

0 X8 C4 Y" C- h// 通过 WIN32_bios 获取 BIOS 信息:
# b9 a) l' B- a7 evoid __fastcall TForm1::Button1Click(TObject *Sender)
5 p0 N# m' i( E1 T$ ^& l9 _/ Z{
/ O/ O0 Z9 F/ S. a6 B$ }9 E    Memo1->Lines->Add("================== [WIN32_bios] =================");% ^& R# K  H6 G- t0 _
   GetWmiInfo(Memo1->Lines, "WIN32_bios");3 Y. M0 [- d4 B# T4 r7 |
   Memo1->Lines->Add("");
8 H, ~3 ?7 X- p7 @: c5 Z}! d  t0 B# I2 N; M8 r1 q! Q

8 t- D8 z1 e! z3 Y  j/ y& o  F' w--------------------------------------------------------------------------------& G. x, I! N8 W5 O
* m( [# F& F! W7 _6 ?4 E% |2 ?4 q
WMI 可以访问的信息类型有:/ X. K/ l8 T, l) L
  Win32_1394Controller
! w+ t* ?/ |$ f; I2 Y3 \7 O   Win32_BaseBoard
* m1 Y4 M( {! `! f/ l/ l   Win32_Battery
7 |7 `) |+ ^" H" Y% I) g5 h   Win32_BIOS/ c$ r( M' N6 }( a* [0 F0 ^' P6 P3 U
  Win32_Bus, o3 \% g4 F0 V6 u% z8 ~
  Win32_CacheMemory
1 J; E: u: ]* }" ]" D   Win32_CDROMDrive
5 o" o4 q# m  D& u9 T' s   Win32_CurrentProbe
+ D( b1 O" a" I  N   Win32_DesktopMonitor& r+ X3 H2 I* Q  R: Y
  Win32_DeviceMemoryAddress
8 @5 E" u0 Q' v; f7 x   Win32_DiskDrive
$ ?9 B/ ~4 u% I: V   Win32_DisplayConfiguration
$ @9 p9 J) [8 u; j! A   Win32_DisplayControllerConfiguration
) z- p6 B# V- _$ @( X   Win32_DMAChannel
! E6 o5 u2 j. n/ D& u   Win32_Fan5 V. v4 k! y% \" s8 R% v& n
  Win32_FloppyController8 k. g, C) r. y, @. B4 K) j0 z
  Win32_FloppyDrive  a0 e9 G, l. b& Q! r
  Win32_HeatPipe  S/ l3 w$ y0 t7 |5 R0 o% x
  Win32_IDEController
% m5 V; V4 t0 |# {   Win32_InfraredDevice
0 d' d) m) O; k) d   Win32_IRQResource
4 x; K5 ]% g+ C, l! i% G! W2 M, M8 W0 n   Win32_Keyboard4 G7 @% [6 a" G: |' Z- m5 D* h+ r6 Z$ F
  Win32_MemoryArray+ U9 W/ V! v5 n4 D* X, b/ H/ a
  Win32_MemoryDevice6 e0 b8 h4 W: M1 d0 O7 ?3 d
  Win32_MotherboardDevice
7 Y0 X2 s6 g( p! s' ^4 M$ [  W& i   Win32_NetworkAdapter
0 Z' T8 L. p8 Z2 \   Win32_NetworkAdapterConfiguration
( {: H8 C& u2 B: F: Q   Win32_OnBoardDevice1 x; y0 t, C* k& [5 t& a# x6 z
  Win32_ParallelPort
% R2 `0 }- \# E5 D, I$ B7 D   Win32_PCMCIAController
& W* M9 y  h$ C1 L$ C8 O$ s% u' }   Win32_PhysicalMemory' @. h! P  z2 \4 k0 T% `, ^
  Win32_PhysicalMemoryArray$ G% }+ T. X+ \4 K  G
  Win32_PnPEntity
. J2 @$ W( l$ f( d$ z   Win32_PointingDevice
9 P  y+ Z3 N2 F2 x+ Y3 D$ O; {7 K   Win32_PortableBattery- J  P: k7 M6 u. k
  Win32_PortConnector
4 D3 V9 H$ L& A: B& `! P   Win32_PortResource3 h7 e4 P4 b$ m
  Win32_POTSModem
9 r$ E: j. f3 q& i( k   Win32_PowerManagementEvent1 l& r& j+ N% _) `' p9 ]* d  o
  Win32_Printer
  a! p, G6 f0 o! Q8 t   Win32_PrinterConfiguration" t: b% |3 g2 x) K
  Win32_PrintJob6 N7 O' i8 z; k" i! q# Z
  Win32_Processor
3 }- z* R$ n6 i6 H1 Q2 d2 q   Win32_Refrigeration9 a2 Z: x; Y  l! o# _- m/ }( B
  Win32_SerialPort) |. M5 p! k# L3 g3 r) L
  Win32_SerialPortConfiguration
( F/ ?5 |( n. l3 n, r( q4 P; n7 J$ V' W2 F   Win32_SMBIOSMemory7 n" `$ o! J$ X
  Win32_SoundDevice
4 L! F- E0 Y' N+ I1 O9 P   Win32_SystemEnclosure
* @2 i6 S: @, U& E  R* d; g  k   Win32_SystemMemoryResource
8 }- i- B5 i7 I& N+ }   Win32_SystemSlot0 u8 R! q% N9 r- z* P8 g
  Win32_TapeDrive
: D3 i8 i& g( |7 n* Q   Win32_TemperatureProbe0 B2 i9 f% R/ B/ g
  Win32_UninterruptiblePowerSupply
9 m# i/ w$ ?- H% K9 p6 e9 Y   Win32_USBController
9 V: ?9 {8 ~5 G: C; i   Win32_VideoConfiguration) p3 a# m) G( r: G8 F
  Win32_VideoController/ S" H' @& g: D% v2 E$ g
  Win32_VoltageProbe
9 w- d8 L3 F) L$ z2 \
* w9 J  J- i1 n1 z" S& x以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-4 10:37 , Processed in 0.017529 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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