2010-03-05 53 views
5

我發現WMI功能非常強大,似乎能夠返回PC硬件的大多數屬性。我想在任何PC上顯示可用的並行端口,並找到它們的I/O地址 - 我知道通常這是使用內核驅動程序完成的,但這是一個遺留需求 - 不要問!目前我們查看設備管理器,然後輸入顯示的地址。我想使用WMI來查找這些信息。 在1有一組優秀的WMI類,但我看不到如何迭代。如何使用Delphi和WMI查找可用的並行端口及其I/O地址

謝謝。

回答

2

人們必須進行試驗,以提取一個WMI複雜的信息。我試圖找到我的PC上的並行端口地址,這是報告:

首先我查詢了Win32_ParallelPort類來查找所有並行端口。 (使用與他之前的帖子中的PRUZ相同的代碼):'Select * From Win32_ParallelPort'。其結果是(我在我的系統中只有一個並行口):

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"; 
}; 

其次,我質疑Win32_PNPAllocatedResource( 'SELECT * FROM Win32_PnPAllocatedResource')。我在這裏得到了很多信息,但我僅選擇了PNPDeviceID =「ACPI \ PNP0401 \ 4 & 25C6B52A & 0」的3個條目。

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\""; 
}; 

第三項沒有意義。前兩個項目給了我們兩(十進制)開始的地址(888和1912)

最後我質疑Win32_PortResource(「SELECT * FROM Win32_PortResource」)找到對應的起始地址結束地址888和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"; 
}; 

更新

我已經使用了相同的代碼RRUZ,在GUI應用程序(見下文)。你需要編譯的唯一東西是WbemScripting_TLB.pas單元。該單元是由類型庫導入嚮導生成的,你可以在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. 
+0

這看起來很有前途的塞爾格 - 地址是我的目標 - 任何一些粗糙的Delphi代碼的機會?感謝Brian。 – 2010-03-06 09:25:28

+0

@Serg:太棒了 - 我會經驗的。謝謝。布萊恩。 – 2010-03-07 21:47:01

+0

@Serg:這很好,非常感謝 - 正是我需要讓我開始。 – 2010-03-18 08:59:31

1

可能,這將幫助你:

uses ComObj, ActiveX; 

function TForm1.GetObject(const objectName: String): IDispatch; 
var 
    bindCtx: IBindCtx; 
    moniker: IMoniker; 
    chEaten: Integer; 
begin 
    OleCheck(CreateBindCtx(0, bindCtx)); 
    OleCheck(MkParseDisplayName(bindCtx, StringToOleStr(objectName), chEaten, moniker)); 
    OleCheck(moniker.BindToObject(bindCtx, nil, IDispatch, Result)); 
end; 
procedure TForm1.Button1Click(Sender: TObject); 
var 
    objWMIService: OLEVariant; 
    colItems, colItem: OLEVariant; 
    oEnum : IEnumvariant; 
    iValue, test : longword; 
begin 
    objWMIService := GetObject('winmgmts:\\YourPCname\root\CIMV2'); 
    colItems := objWMIService.ExecQuery('SELECT * FROM Win32_ParallelPort',,48); 
    oEnum := IUnknown(colItems._NewEnum) as IEnumVariant; 
    while oEnum.Next(1, colItem, iValue) = 0 do begin 
    //You can get all the properties here 
    //for example colItem.Caption 
    // properties of Win32_ParalelPort class : http://msdn.microsoft.com/en-us/library/aa394247%28VS.85%29.aspx 
    end; 
end; 
1

@布賴恩,你只需要使用Win32_parallelPort類來獲取信息。

檢查此代碼。

program GetWMI_ParallelPortInfo; 

{$APPTYPE CONSOLE} 

uses 
    Windows, 
    Classes, 
    ActiveX, 
    Variants, 
    SysUtils, 
    WbemScripting_TLB in '..\..\Documents\RAD Studio\5.0\Imports\WbemScripting_TLB.pas'; 

procedure GetWMIParallelPortInfo; 
var 
    WMIServices : ISWbemServices; 
    WMILocator : ISWbemLocator; 
    Root   : ISWbemObjectSet; 
    SWbemObject : ISWbemObject; 
    Item   : IEnumVariant; 
    rgVar  : OleVariant; 
    pCelFetched : Cardinal; 

begin 
    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 
     Writeln(SWbemObject.GetObjectText_(0));//The GetObjectText_ method of the SWbemObject object returns a textual rendering of the object in MOF format 
    end; 
    end; 
end; 

begin 
try 
    CoInitialize(nil); 
    try 
     GetWMIParallelPortInfo; 
     Readln; 
    finally 
     CoUninitialize; 
    end; 
except 
    on E:Exception do 
    Begin 
     Writeln(E.Classname, ': ', E.Message); 
     Readln; 
    End; 
    end; 
end. 

alt text http://i48.tinypic.com/2e67wxz.png

+0

@RRUZ謝謝,代碼給了我一個開始。我如何找到'StartAddress'參數?布萊恩。 – 2010-03-06 09:26:38

+0

@RRUZ:感謝指針 - 現在已經解決了。 – 2010-03-18 09:00:46

1

瞭解德爾福2009年的過程中,我不明白什麼是你需要的值。
如果你需要知道這一點:

alt text http://img682.imageshack.us/img682/2382/imagen333.png

我覺得你可以在Win32_PortResource類和Win32_portConnector

找到它,你能否證實這個是這樣的嗎?
做個測試;打開CMD窗口,然後輸入:
> WMIC端口列表FULL

alt text http://img215.imageshack.us/img215/1696/imagen332.png

如果這是您正在搜索的價值,你可以開發一個new component in GLibWMI(或說向我求助您),其檢索這個值。

問候。

PD:對英語錯誤的原因。

+0

是的!這正是我想要的。我不明白如何顯示信息,而一個常見的問題是當有兩個並行端口時,我需要知道兩者的信息,而不僅僅是第一個。我希望我不會感到困惑。你的英語很好!布萊恩。 – 2010-03-06 09:23:03

相關問題