如何使用Delphi和WMI查找可用的并行端口及其I / O地址 [英] How to find available parallel ports and their I/O addresses using Delphi and WMI

查看:192
本文介绍了如何使用Delphi和WMI查找可用的并行端口及其I / O地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到WMI非常强大,似乎能够返回PC硬件的大部分属性。我想在任何PC上显示可用的并行端口,并找到他们的I / O地址 - 我知道通常这是使用内核驱动程序完成的,但这是一个传统的需求 - 不要问!目前,我们查看设备管理器,然后输入显示的地址。我想使用WMI来查找这些信息。
1 上有一套优秀的WMI类,但是我不能看看如何迭代。



谢谢。

解决方案

从WMI提取复杂信息。我试图在PC上找到并行端口地址,这就是报告:



首先我查询过Win32_ParallelPort类来找到所有的并行端口。 (在他之前的帖子中使用与PRUZ相同的代码):'Select * From Win32_ParallelPort'。结果是(我的系统中只有一个并行端口):

  Win32_ParallelPort的实例
{
可用性= 3;
Caption =LPT1;
ConfigManagerErrorCode = 0;
ConfigManagerUserConfig = FALSE;
CreationClassName =Win32_ParallelPort;
描述=LPT1;
DeviceID =LPT1;
Name =LPT1;
OSAutoDiscovered = TRUE
PNPDeviceID =ACPI\\PNP0401\\4& 25C6B52A& 0;
PowerManagementSupported = FALSE;
ProtocolSupported = 17;
SystemCreationClassName =Win32_ComputerSystem;
SystemName =JUPITER;
};

其次,我查询了Win32_PNPAllocatedResource('select * from Win32_PnPAllocatedResource')。我在这里有很多信息,但是我只选择了PNPDeviceID =ACPI\PNP0401\4& 25C6B52A& 0的3个条目。

  Win32_PNPAllocatedResource的实例
{
Antecedent =\\\\\JUPITER\\\\\\\cimv2:Win32_PortResource.StartingAddress = \ 888 \;
Dependent =\\\\\JUPITER\\\\\\\cimv2:Win32_PnPEntity.DeviceID = \ACPI\\\\PNP0401\\\\ \4& 25C6B52A& 0\;
};


Win32_PNPAllocatedResource的实例
{
前提=\\\\JUPITER\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ = \1912\;
Dependent =\\\\\JUPITER\\\\\\\cimv2:Win32_PnPEntity.DeviceID = \ACPI\\\\PNP0401\\\\ \4& 25C6B52A& 0\;
};


Win32_PNPAllocatedResource实例
{
Antecedent =\\\\JUPITER\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ = 3;
Dependent =\\\\\JUPITER\\\\\\\cimv2:Win32_PnPEntity.DeviceID = \ACPI\\\\PNP0401\\\\ \4& 25C6B52A& 0\;
};

第三个条目是没有兴趣的。前两个条目给我们两个(十进制)起始地址(888和1912)



最后我查询了Win32_PortResource('Select * From Win32_PortResource')查找结束地址对应于起始地址888和1912:

  Win32_PortResource的实例
{
Alias = FALSE;
Caption =0x00000378-0x0000037F;
CreationClassName =Win32_PortResource;
CSCreationClassName =Win32_ComputerSystem;
CSName =JUPITER;
描述=0​​x00000378-0x0000037F;
EndingAddress =895;
Name =0x00000378-0x0000037F;
StartingAddress =888;
Status =OK;
};


Win32_PortResource的实例
{
别名= FALSE;
Caption =0x00000778-0x0000077B;
CreationClassName =Win32_PortResource;
CSCreationClassName =Win32_ComputerSystem;
CSName =JUPITER;
描述=0​​x00000778-0x0000077B;
EndingAddress =1915;
Name =0x00000778-0x0000077B;
StartingAddress =1912;
Status =OK;
};






已更新 p>

我在GUI应用程序中使用了与RRUZ相同的代码(见下文)。你需要编译的唯一的东西是WbemScripting_TLB.pas单元。该单元是通过类型库导入向导生成的,您可以在我的博客

  unit Unit1; 

接口

使用
Windows,消息,SysUtils,变体,类,图形,控件,窗体,
对话框,StdCtrls;

type
TForm1 = class(TForm)
Memo1:TMemo;
Button4:TButton;
Button5:TButton;
Button6:TButton;
程序Button4Click(发件人:TObject);
procedure Button5Click(Sender:TObject);
procedure Button6Click(Sender:TObject);
private
{私人声明}
public
{公开声明}
end;

var
Form1:TForm1;

实现

使用WbemScripting_TLB,ActiveX;


{$ R * .dfm}


程序TForm1.Button4Click(发件人:TObject);
var
WMIServices:ISWbemServices;
WMILocator:ISWbemLocator;
根:ISWbemObjectSet;
SWbemObject:ISWbemObject;
项目:IEnumVariant;
rgVar:OleVariant;
pCelFetched:红衣主教;

begin
Memo1.Lines.Clear;
WMILocator:= CoSWbemLocator.Create();
WMIServices:= WMILocator.ConnectServer('。','root\cimv2','','','','',0,nil); //
根:= WMIServices.ExecQuery('select * from Win32_PnPAllocatedResource','WQL',0,nil);
Item:=(Root._NewEnum)as IEnumVariant;
while(Item.Next(1,rgVar,pCelFetched)= S_OK)do
begin
SWbemObject:= IUnknown(rgVar)as ISWBemObject;
if(SWbemObject<> nil)then
begin
SWbemObject.Properties _; //加载属性以读取
Memo1.Lines.Add(SWbemObject.GetObjectText_(0) );
结束
结束
结束

procedure TForm1.Button5Click(Sender:TObject);
var
WMIServices:ISWbemServices;
WMILocator:ISWbemLocator;
根:ISWbemObjectSet;
SWbemObject:ISWbemObject;
项目:IEnumVariant;
rgVar:OleVariant;
pCelFetched:红衣主教;

begin
Memo1.Lines.Clear;
WMILocator:= CoSWbemLocator.Create();
WMIServices:= WMILocator.ConnectServer('。','root\cimv2','','','','',0,nil); //
根:= WMIServices.ExecQuery('select * from Win32_PortResource','WQL',0,nil);
Item:=(Root._NewEnum)as IEnumVariant;
while(Item.Next(1,rgVar,pCelFetched)= S_OK)do
begin
SWbemObject:= IUnknown(rgVar)as ISWBemObject;
if(SWbemObject<> nil)then
begin
SWbemObject.Properties _; //加载属性以读取
Memo1.Lines.Add(SWbemObject.GetObjectText_(0) );
结束
结束
结束

程序TForm1.Button6Click(Sender:TObject);
var
WMIServices:ISWbemServices;
WMILocator:ISWbemLocator;
根:ISWbemObjectSet;
SWbemObject:ISWbemObject;
项目:IEnumVariant;
rgVar:OleVariant;
pCelFetched:红衣主教;

begin
Memo1.Lines.Clear;
WMILocator:= CoSWbemLocator.Create();
WMIServices:= WMILocator.ConnectServer('。','root\cimv2','','','','',0,nil); //
根:= WMIServices.ExecQuery('select * from Win32_ParallelPort','WQL',0,nil);
Item:=(Root._NewEnum)as IEnumVariant;
while(Item.Next(1,rgVar,pCelFetched)= S_OK)do
begin
SWbemObject:= IUnknown(rgVar)as ISWBemObject;
if(SWbemObject<> nil)then
begin
SWbemObject.Properties _; //加载属性以读取
Memo1.Lines.Add(SWbemObject.GetObjectText_(0) );
结束
结束
结束

结束。


I see that WMI is very powerful and seems to be able to return most properties of a PC hardware. I'd like to display the available parallel ports on any PC and find their I/O addresses - I know that normally this is done using a Kernel driver but this is a legacy need - dont ask! Presently we look in Device Manager and then have to type in the address displayed there. I'd like to use WMI to find out this information. There are an excellent set of WMI classes at 1 but I cannot see how to iterate.

Thanks.

解决方案

One must experiment to extract a complex information from WMI. I have tried to find the parallel port addresses on my PC and that is the report:

First of all I have queried Win32_ParallelPort class to find all parallel ports. (using the same code as PRUZ in his post before): 'Select * From Win32_ParallelPort'. The result is (I have only one parallel port in my system):

instance of Win32_ParallelPort
{
    Availability = 3;
    Caption = "LPT1";
    ConfigManagerErrorCode = 0;
    ConfigManagerUserConfig = FALSE;
    CreationClassName = "Win32_ParallelPort";
    Description = "LPT1";
    DeviceID = "LPT1";
    Name = "LPT1";
    OSAutoDiscovered = TRUE;
    PNPDeviceID = "ACPI\\PNP0401\\4&25C6B52A&0";
    PowerManagementSupported = FALSE;
    ProtocolSupported = 17;
    SystemCreationClassName = "Win32_ComputerSystem";
    SystemName = "JUPITER";
};

Second, I have queried Win32_PNPAllocatedResource ('Select * From Win32_PnPAllocatedResource'). I have got a lot of information here, but I have selected only the 3 entries by PNPDeviceID = "ACPI\PNP0401\4&25C6B52A&0".

instance of Win32_PNPAllocatedResource
{
    Antecedent = "\\\\JUPITER\\root\\cimv2:Win32_PortResource.StartingAddress=\"888\"";
    Dependent = "\\\\JUPITER\\root\\cimv2:Win32_PnPEntity.DeviceID=\"ACPI\\\\PNP0401\\\\4&25C6B52A&0\"";
};


instance of Win32_PNPAllocatedResource
{
    Antecedent = "\\\\JUPITER\\root\\cimv2:Win32_PortResource.StartingAddress=\"1912\"";
    Dependent = "\\\\JUPITER\\root\\cimv2:Win32_PnPEntity.DeviceID=\"ACPI\\\\PNP0401\\\\4&25C6B52A&0\"";
};


instance of Win32_PNPAllocatedResource
{
    Antecedent = "\\\\JUPITER\\root\\cimv2:Win32_DMAChannel.DMAChannel=3";
    Dependent = "\\\\JUPITER\\root\\cimv2:Win32_PnPEntity.DeviceID=\"ACPI\\\\PNP0401\\\\4&25C6B52A&0\"";
};

The third entry is of no interest. The first two entries gives us two (decimal) starting addresses (888 and 1912)

Finally I have queried Win32_PortResource ('Select * From Win32_PortResource') to find the ending addresses corresponding to the starting addresses 888 and 1912:

instance of Win32_PortResource
{
    Alias = FALSE;
    Caption = "0x00000378-0x0000037F";
    CreationClassName = "Win32_PortResource";
    CSCreationClassName = "Win32_ComputerSystem";
    CSName = "JUPITER";
    Description = "0x00000378-0x0000037F";
    EndingAddress = "895";
    Name = "0x00000378-0x0000037F";
    StartingAddress = "888";
    Status = "OK";
};


instance of Win32_PortResource
{
    Alias = FALSE;
    Caption = "0x00000778-0x0000077B";
    CreationClassName = "Win32_PortResource";
    CSCreationClassName = "Win32_ComputerSystem";
    CSName = "JUPITER";
    Description = "0x00000778-0x0000077B";
    EndingAddress = "1915";
    Name = "0x00000778-0x0000077B";
    StartingAddress = "1912";
    Status = "OK";
};


Updated

I have used the same code as RRUZ, in GUI application (see below). The only thing you need to compile it is the WbemScripting_TLB.pas unit. The unit is generated by type library import wizard, you can read about the process in Delphi 2009 in my blog

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses WbemScripting_TLB, ActiveX;


{$R *.dfm}


procedure TForm1.Button4Click(Sender: TObject);
var
  WMIServices  : ISWbemServices;
  WMILocator   : ISWbemLocator;
  Root         : ISWbemObjectSet;
  SWbemObject  : ISWbemObject;
  Item         : IEnumVariant;
  rgVar        : OleVariant;
  pCelFetched  : Cardinal;

begin
  Memo1.Lines.Clear;
  WMILocator := CoSWbemLocator.Create();
  WMIServices := WMILocator.ConnectServer('.', 'root\cimv2','', '', '', '', 0, nil);    //
  Root := WMIServices.ExecQuery('Select * From Win32_PnPAllocatedResource','WQL', 0, nil);
  Item :=  (Root._NewEnum) as IEnumVariant;
  while  (Item.Next(1, rgVar, pCelFetched) = S_OK) do
  begin
    SWbemObject := IUnknown(rgVar) as ISWBemObject;
    if (SWbemObject <> nil) then
    begin
      SWbemObject.Properties_;//Load the Properties to read
      Memo1.Lines.Add(SWbemObject.GetObjectText_(0));
    end;
  end;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  WMIServices  : ISWbemServices;
  WMILocator   : ISWbemLocator;
  Root         : ISWbemObjectSet;
  SWbemObject  : ISWbemObject;
  Item         : IEnumVariant;
  rgVar        : OleVariant;
  pCelFetched  : Cardinal;

begin
  Memo1.Lines.Clear;
  WMILocator := CoSWbemLocator.Create();
  WMIServices := WMILocator.ConnectServer('.', 'root\cimv2','', '', '', '', 0, nil);    //
  Root := WMIServices.ExecQuery('Select * From Win32_PortResource','WQL', 0, nil);
  Item :=  (Root._NewEnum) as IEnumVariant;
  while  (Item.Next(1, rgVar, pCelFetched) = S_OK) do
  begin
    SWbemObject := IUnknown(rgVar) as ISWBemObject;
    if (SWbemObject <> nil) then
    begin
      SWbemObject.Properties_;//Load the Properties to read
      Memo1.Lines.Add(SWbemObject.GetObjectText_(0));
    end;
   end;
end;

procedure TForm1.Button6Click(Sender: TObject);
var
  WMIServices  : ISWbemServices;
  WMILocator   : ISWbemLocator;
  Root         : ISWbemObjectSet;
  SWbemObject  : ISWbemObject;
  Item         : IEnumVariant;
  rgVar        : OleVariant;
  pCelFetched  : Cardinal;

begin
  Memo1.Lines.Clear;
  WMILocator := CoSWbemLocator.Create();
  WMIServices := WMILocator.ConnectServer('.', 'root\cimv2','', '', '', '', 0, nil);    //
  Root := WMIServices.ExecQuery('Select * From Win32_ParallelPort','WQL', 0, nil);
  Item :=  (Root._NewEnum) as IEnumVariant;
  while  (Item.Next(1, rgVar, pCelFetched) = S_OK) do
  begin
    SWbemObject := IUnknown(rgVar) as ISWBemObject;
    if (SWbemObject <> nil) then
    begin
      SWbemObject.Properties_;//Load the Properties to read
      Memo1.Lines.Add(SWbemObject.GetObjectText_(0));
    end;
   end;
end;

end.

这篇关于如何使用Delphi和WMI查找可用的并行端口及其I / O地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆