delphi中WMI的使用(网卡是否接入)

WMI(Windows Management Instrumentation,Windows 管理规范)是一项核心的 Windows 管理技术;用户可以使用 WMI 管理本地和远程计算机。

通过使用WMI,我们可以很方便的获取到电脑的系统信息,但是很遗憾的是,我在网上找到的基本上都是对于某个功能的实现,比如查询IP,获取计算机名称,检测操作系统信息等等,如果只是在工作中用到了来查一下是没有问题的,但是,要想全面的了解WMI中的类库,来系统的掌握就很困难,WMI中有哪些系统的类,里面有哪些属性,这些属性都代表什么意思?MSND上面对于vbscript脚本实现的教程很详细,但是具体在delphi中对应的类型和参数名称是不一样的。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls ,ActiveX,comobj;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}


 //网卡是否接入
function GetNetworkConnected(const sName: string): boolean;
var
  C: Cardinal;
  Enum: IEnumVariant;
  Wmi, Objs, Obj, V: OleVariant;
begin
  Result := False;
  Wmi := CreateOleObject('WbemScripting.SWbemLocator');
  try
    // ConnectServer无参表示连接本机      NetConnectionStatus=2 表示已经接入 7表示断开
    Objs := Wmi.ConnectServer().ExecQuery('Select * from Win32_NetworkAdapter WHERE NetConnectionStatus=2');
    Enum := IEnumVariant(IUnknown(Objs._NewEnum));
    Enum.Reset;
    //迭代信息
    while Enum.Next(1, Obj, C) = S_OK do
    begin
      V := Obj.Properties_.Item('Name', 0).Value;
      if VarIsClear(V) or VarIsNull(V) then
        Continue;
      if V=sName then
      begin
         Result:=True;
         Break
      end;
    end;
  finally
    Wmi := Unassigned;
  end;
end;




procedure TForm1.Button1Click(Sender: TObject);

begin
  ShowMessage(booltostr(GetNetworkConnected(Edit1.Text)));
end;

end.

因为WMI中通用的类大多是Win32_开头的,这里只需要传递后面的部分。

那么下一步,怎么知道我需要的是什么参数呢?以获取计算机名为例:

str := GetWMIProperty('OperatingSystem','CSName');即可获得。

当然,计算机名信息是比较常用的,在好几个类里面都有。

Win32_开头的类有:

Win32_Account
Win32_ActiveRoute
Win32_AutochkSetting
Win32_BaseBoard
Win32_BaseService
Win32_BIOS
Win32_BootConfiguration
Win32_Bus
Win32_ComputerSystem
Win32_ComputerSystemWindowsProductActivation
Win32_Desktop
Win32_DesktopMonitor
Win32_DeviceSettings
Win32_DiskDrive
Win32_DiskDrivePhysicalMedia
Win32_DisplayConfiguration
Win32_Environment
Win32_FontIntoAction
Win32_Group
Win32_IDEController
Win32_IP4PersistedRouteTable
Win32_IP4RouteTable
Win32_Keyboard
Win32_LogicalDisk
Win32_LogicalProgramGroup
Win32_LogonSession
Win32_MappedLogicalDisk
Win32_MemoryDevice
Win32_MotherboardDevice
Win32_NetworkAdapter
Win32_NetworkAdapterConfiguration
Win32_NetworkAdapterSetting
Win32_NetworkClient
Win32_NetworkConnection
Win32_OperatingSystem
Win32_PerfFormattedData_RemoteAccess_RASPort
Win32_PerfFormattedData_RemoteAccess_RASTotal
Win32_PerfRawData_RemoteAccess_RASPort
Win32_PhysicalMedia
Win32_PhysicalMemory
Win32_PNPDevice
Win32_PortConnector
Win32_POTSModem
Win32_POTSModemToSerialPort
Win32_Printer
Win32_PrinterConfiguration
Win32_Process
Win32_Processor
Win32_Product
Win32_ProgramGroup
Win32_ScheduledJob
Win32_SerialPort
Win32_SerialPortConfiguration
Win32_SerialPortSetting
Win32_Service
Win32_ServiceControl
Win32_SoundDevice
Win32_StartupCommand
Win32_SystemAccount
Win32_SystemBIOS
Win32_SystemSetting
Win32_SystemTimeZone
Win32_SystemUsers
Win32_TapeDrive
Win32_TemperatureProbe 
Win32_USBController
Win32_USBControllerDevice
Win32_UserAccount
Win32_VideoController
Win32_VideoSettings
Win32_WindowsProductActivation

Win32_NetworkAdapter 网卡 参数说明

AdapterType  --适配器的类型
AdapterTypeID  --适配器的类型ID
AutoSense  --如果为True,网络适配器能自动判断连接或网络媒体的速度
Availability  --可用性和设备的状态
Caption  --设备的简短描述
ConfigManagerErrorCode  --Windows配置管理器错误代码
ConfigManagerUserConfig  --如果为True,该设备使用用户定义的配置
CreationClassName  --第一个具体类的名称出现在创建实例所使用的继承链
Description  --描述
DeviceID  --系统中的其他设备的网络适配器的唯一标识符
ErrorCleared  --如果为True,在上一个错误代码报告现已清除
ErrorDescription  --错误的描述信息
GUID  --全局唯一标识符进行连接
Index  --存储在系统注册表中网络适配器的索引号
InstallDate  --日期和时间对象安装
Installed  --如果为True,网络适配器安装在系统中
InterfaceIndex  --索引值唯一标识本地网络接口
LastErrorCode  --报告的逻辑设备上一个错误代码
MACAddress  --MAC地址
Manufacturer  --制造商的名称
MaxNumberControlled  --通过这个网络适配器支持直接寻址的端口的最大数量
MaxSpeed  --最大速度,以每秒位数,为网络适配器
Name  --适配器的名称
NetConnectionID  --网络连接的名称出现在网络连接控制面板程序
NetConnectionStatus  --网络适配器连接至网络的状态
NetEnabled  --指明适配器是否已启用。如果为True,适配器已启用。您可以启用或通过启用和禁用方法禁用网卡。
NetworkAddresses  --适配器的IP地址
PermanentAddress  --网络地址硬编码到一个适配器。这种硬编码地址,可以通过固件升级或软件配置进行更改
PhysicalAdapter  --指明适配器是否是物理或逻辑适配器。如果为True,适配器是物理
PNPDeviceID  --即插即用逻辑设备的播放设备标识符
PowerManagementCapabilities  --逻辑设备的特定功率相关的能力阵列
PowerManagementSupported  --如果为True,该设备可以是电源管理(可放进入暂停模式,等等)
ProductName  --网络适配器的产品名称
ServiceName  --网络适配器的服务名
Speed  --估计当前带宽比特每秒
Status  --对象的当前状态
StatusInfo  --对象的当前状态详细信息
SystemCreationClassName  --该作用域计算机的创建类别名称属性的值
SystemName  --系统名称

TimeOfLastReset  --最后复位日期和时间的网络适配器

猜你喜欢

转载自www.cnblogs.com/hhmm99/p/10648416.html
wmi
今日推荐