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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
1 w, x- H: c& m& I1 L1 T$ y3 _, |7 u% F3 o: G
4 S6 P; m: P) _
--------------------------------------------------------------------------------
; J. D( v% E+ y3 e$ ?1 ^WMI: Windows Management Instrumentation (Windows 管理工具)
' |" u3 {6 W: K+ h( P* V! k$ k   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 * S! i8 R/ e/ D9 o! G6 m
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。
5 z4 O0 P0 @; U- U) S% I* I% u9 j   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
1 `" E' ~, j& P; I( h: ], H1 T# t( e" F4 B3 }* w6 ?5 X$ S% q) q
--------------------------------------------------------------------------------
% G! o  X5 v7 a! yBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
& p& s5 P3 o( a) _, d/ Z8 s2 i2 v/ R. _: N' r
--------------------------------------------------------------------------------
% R; b* j: J+ ]8 L/ P& M7 l- Z① 初始化 COM 接口:( o! y4 n, I' e* ]) d1 }/ {- X
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
' Z7 U" s  T& [& |/ S$ f' i! n8 T8 O   这两个函数在 #include <comdef.h> 里面定义。
# I$ p# O9 _! b8 K. L
$ P  L. ?; X7 f) |8 G4 k9 N( o: m② 获取访问 WMI 权限:% {2 f  r" I2 e2 n& v" w
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
( o+ H! T/ z4 n8 x" A# ^2 i   如果这个函数返回 S_OK 获取权限成功, 否则为失败。
# q( G7 W; q$ u! I& P( c# I8 C3 S; t0 ]
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:' F( l! @/ Z6 r' F
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
3 c% E3 t  K( M+ h  w( V" z
& I0 A* R  j9 U/ L* Ivoid GetWmiInfo(TStrings *lpList, WideString wsClass)
1 k1 V& E0 m' R2 I{
, o: o- ~# F: d6 V0 f7 g* [  IWbemLocator *pWbemLocator = NULL;' R9 C, p+ X5 l, L8 t' i6 c
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
- O0 B; X5 u9 n* n) V8 e* ^1 a   {
- H$ e( Z  a( M0 R; B5 m     IWbemServices *pWbemServices = NULL;' }7 H+ C" v6 b  s+ z9 U
    WideString wsNamespace = (L"root\\cimv2");) d. A1 V1 {4 a/ A4 x
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)3 m8 B" G+ p8 D3 w) H+ g2 W! n. G
     {
5 S  Z6 k2 \8 E( g        IEnumWbemClassObject *pEnumClassObject = NULL;% \+ A0 Q4 i& t; X; t
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
2 S  O* S* Q& q        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
" ?4 I5 [, [+ c' p, x1 m         {
* s, c9 T# P" K/ {: T; j$ R5 _           IWbemClassObject *pClassObject = NULL;2 ^2 o) L2 {9 j' o! W6 ?) j4 g4 ?1 ?% U
          ULONG uCount = 1, uReturned;' q5 I$ c. j4 G7 Y# Y8 M, T
          if(pEnumClassObject->Reset() == S_OK)
1 `' `- F6 ]9 ^            {; U6 l' T! @: [2 X5 U; \
             int iEnumIdx = 0;
" U8 |, d; H  T! K' d              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)+ I* j5 b# s4 r! y3 Z
              {
5 ?+ k( {9 S6 n" U) a                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");8 P) u8 k6 u, {" C. W3 j0 {/ Y
- Y' ]7 R& J5 y0 I( _$ y+ f5 C
                SAFEARRAY *pvNames = NULL;. v2 T! v3 t+ j" H
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)3 C, u& W- n& x- _, V0 i1 s
                 {
7 F; Z8 e0 r( _8 E                    long vbl, vbu;
1 D) X& |% q/ V& i% j                    SafeArrayGetLBound(pvNames, 1, &vbl);
6 O; P8 i# h6 h) L$ s                    SafeArrayGetUBound(pvNames, 1, &vbu);2 B* f2 a1 w( m6 L5 r: n5 r
                   for(long idx=vbl; idx<=vbu; idx++)
7 F' S. R/ h$ F; {, T                     {
# ^0 @, a7 J& t3 W9 i, @+ {) v                       long aidx = idx;% k2 M: e9 g" Y1 M3 V  R# N
                      wchar_t *wsName = 0;
2 b* r# @% Z' j8 w                       VARIANT vValue;
/ j5 ]# Y/ d; o- a- T                       VariantInit(&vValue);- S. ?$ K. o* w- T
                      SafeArrayGetElement(pvNames, &aidx, &wsName);& M; L+ t5 [- c: y
; k8 N$ S, ?- `
                      BSTR bs = SysAllocString(wsName);7 C5 ]; \* D# n/ T5 Q
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
5 [- |$ T& ^, X! h+ O( H9 d5 H) I# ~                       SysFreeString(bs);
9 H2 o6 C) s2 d! k+ t% f! _$ w9 D& v, a. o5 F
                      if(hRes == S_OK)
" A- s% q& ^$ _3 y                        {9 Q9 `: t% K0 o; v% D
                         AnsiString s;
- ~+ w' h2 E/ ^% [+ c  h2 M                          Variant v = *(Variant*)&vValue;
5 c/ v. L1 K  ^7 k4 F                          if(v.IsArray())
" {3 x! ?) L/ A. `' o- ^, U                           {
( x2 Q* s" P+ B$ }% ~" H4 S                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
! ~0 e( o( N( c2 w: Y+ |                              {
( M) X# H' M6 d                                Variant a = v.GetElement(i);
3 l) n1 ?2 e) G  v6 U                                if(!s.IsEmpty())
0 w& s5 R  D) Z, n8 Y/ h$ Z                                  s+=", ";* s' d# m4 }/ \9 o; j
                               s+=VarToStr(a);
# b  P+ p8 K, \( _- f/ q& N                              }
' n9 _1 }- _6 H* ~                           }
" [6 @" v! l% A  f8 a8 I: o                          else4 x% L; z& w2 t+ ^2 }! T
                          {
+ U, s/ {# E5 f! K                             s = VarToStr(v);
; v/ q+ k$ [- X" V" k; y                           }
6 [4 n' s+ M6 A5 x                          lpList->Add(AnsiString(wsName)+"="+s);8 P! M+ {1 h6 N+ F& P. _+ R' @
                       }
8 X* o9 R# C, B- X7 A$ q1 i0 ]/ k8 I" U6 I6 Y
                      VariantClear(&vValue);
: Q: _4 D: @. y: b/ i                       SysFreeString(wsName);
4 f: V7 Y. c& R# _3 z/ `0 ~: f                     }- n0 X0 d1 N7 P" Z. g) d
                 }" z  B9 B% a5 G4 z( w5 A
                if(pvNames)SafeArrayDestroy(pvNames);
; S" N# [9 K) H0 E                 iEnumIdx++;
0 D, L7 v/ N/ C. V* j7 O! f- j3 k               }
5 j  l6 j* A) v% D0 M            }
( z, l2 |2 Q; z- ?! O' Y           if(pClassObject)pClassObject->Release();
. o1 j4 V4 y7 z, @% T4 l6 P         }
4 a. t% Q, X! S5 ?7 P+ }0 h        if(pEnumClassObject)pEnumClassObject->Release();! ]+ e. S5 ^: Z. N$ ?4 T
     }
1 m' r" J1 ~5 d: R     if(pWbemServices)pWbemServices->Release();
. u' F: w1 R4 K' D& ^! g   }: h# A& l" Y# c+ t8 I# d! p
  if(pWbemLocator)pWbemLocator->Release();
' a  ^; k9 x9 m}
' f8 }0 O) y# x9 C+ \//---------------------------------------------------------------------------* [6 K9 K4 S+ u, M, \! E
! ~" b% r, H, ?, C0 w' e7 ]8 b9 J
// 通过 WIN32_bios 获取 BIOS 信息:
7 q! Q) w; J- Q5 r/ q! xvoid __fastcall TForm1::Button1Click(TObject *Sender)! m4 K# d9 Q5 z6 K- k1 m$ W* L6 i
{
7 @4 E$ o* @, y$ T5 p    Memo1->Lines->Add("================== [WIN32_bios] =================");
0 |8 W5 ]* J8 z5 m    GetWmiInfo(Memo1->Lines, "WIN32_bios");$ A: w7 y( e! y
   Memo1->Lines->Add("");: O& v( f6 ^0 B5 A# a" t0 h, l9 F
}3 i# |' `; J2 K$ e
  W5 o, z6 e2 P
--------------------------------------------------------------------------------4 r& g' |! `8 A$ l

6 p  ]$ d( H) c) z' C, P/ uWMI 可以访问的信息类型有:% k  L4 p/ Q4 ]( H- k
  Win32_1394Controller# d7 b1 W, h* v0 f9 ?6 z
  Win32_BaseBoard
& d9 s4 q5 h0 ?4 A" W$ l% @' c+ D   Win32_Battery
0 X6 m  f/ ^+ n! H2 U   Win32_BIOS. f8 Q. ~- H/ Z, V! [6 G
  Win32_Bus: M+ p. f1 g( Z2 z; K
  Win32_CacheMemory
8 E2 _1 x8 D% ]; T! b2 t   Win32_CDROMDrive) r& ]' g) A6 H- p  U" d
  Win32_CurrentProbe
, x5 r! p! m# N3 {   Win32_DesktopMonitor6 E% ^. \$ p# Y0 u$ N+ i2 S
  Win32_DeviceMemoryAddress
- Q% d1 d5 {+ ~) j( W( P   Win32_DiskDrive) ~- m; A# R& T
  Win32_DisplayConfiguration
* O* I4 d, m5 M# F1 F. U' M. `   Win32_DisplayControllerConfiguration, s) ~5 {5 X0 L
  Win32_DMAChannel: P" j. ?$ t' a. \
  Win32_Fan) F2 H, j, v8 D) c' r4 K* @$ @7 q8 X
  Win32_FloppyController  [; q& _9 R. o1 k7 u) J
  Win32_FloppyDrive
  T7 n9 v( b% E4 Z   Win32_HeatPipe
( V2 l5 c5 W1 P  ^0 @0 F   Win32_IDEController3 i5 v* j/ e1 K0 w( U; w- k
  Win32_InfraredDevice
# d- S! {1 ], D) B- U   Win32_IRQResource
8 \/ ~# p0 |: s3 K1 I   Win32_Keyboard
  ~7 n0 x- }) ~1 f   Win32_MemoryArray
* o- `% E3 e; _6 G! V   Win32_MemoryDevice$ @  R; Z4 p4 A( V
  Win32_MotherboardDevice6 l8 A( O/ M% j% i3 Y
  Win32_NetworkAdapter2 R( b4 E7 r+ u/ \. t
  Win32_NetworkAdapterConfiguration, a1 ]* A; u3 G
  Win32_OnBoardDevice- g" r0 w  K# W0 t2 ]
  Win32_ParallelPort
3 n; I' Y8 @- N  U5 B- ^  x1 }   Win32_PCMCIAController( j5 n) E% ?: v, @! _- J# Q; v2 h+ L
  Win32_PhysicalMemory7 A) F( w" P4 X, p- n" ]9 u
  Win32_PhysicalMemoryArray
  M' }+ I; [% L   Win32_PnPEntity, t! m7 x5 r, |! l! \
  Win32_PointingDevice* L  ]8 z: t& ?4 ^6 N  b
  Win32_PortableBattery
) |  j- N1 r4 J& ]# U+ h   Win32_PortConnector) K# \$ [  W- |5 }
  Win32_PortResource
' j2 R. ^$ ], P6 {. d   Win32_POTSModem$ S% m8 z* f; @4 q+ F+ |$ u
  Win32_PowerManagementEvent: u& l7 m. Q6 V2 r
  Win32_Printer4 l+ Y6 E  F+ j; \% [' s5 n
  Win32_PrinterConfiguration
7 p* o& p! p6 ^0 \   Win32_PrintJob) B- F1 N' C7 {; l
  Win32_Processor
8 `  |: Z  ^" u0 Y. ]   Win32_Refrigeration
* |4 O2 i% F7 C3 O& t7 M, p4 u* E# q   Win32_SerialPort
# @2 ^0 H8 V( K% ]& {1 a* N7 ?   Win32_SerialPortConfiguration5 @* _2 r1 d; f( ]
  Win32_SMBIOSMemory
- G" r) O. [0 r6 B0 n   Win32_SoundDevice
( X2 y1 ^6 L- ]4 k. e4 \1 j   Win32_SystemEnclosure
4 P6 t/ `0 _8 w8 A4 ~& T, L% s1 r   Win32_SystemMemoryResource
0 [0 E5 p8 _- e$ G: x- @* j% `   Win32_SystemSlot
2 g7 q  Q6 O% m   Win32_TapeDrive5 Z- ?  y) ^( J) `& l! P
  Win32_TemperatureProbe5 w8 l9 t2 G; n  \4 m2 R0 l9 [5 t) O
  Win32_UninterruptiblePowerSupply
6 d  Y' n' z% \% C/ w8 p   Win32_USBController
0 f+ t. ~: [! h( w  P0 N" T   Win32_VideoConfiguration7 b1 w4 T2 }# ]' w6 f. R
  Win32_VideoController- S' |  S5 q0 @! C2 W0 I
  Win32_VoltageProbe
- @9 U4 p/ Q/ ^) }
. j" b1 F- K- `4 |& T以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-13 02:02 , Processed in 0.016434 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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