3

我需要一种方法来使用 WMI 来查找当前连接到我已经知道的 COM 端口的调制解调器(或其他设备)的名称。

例如,假设我已经提取了我想要的设备在 COM 端口 3 上,并且我也知道它是一个调制解调器,我怎样才能找到与该 COM 端口关联的调制解调器的名称。

目前我有使用Win32_PnPEntity的代码,它可以提取名称中带有调制解调器或 COM 的 pnp 设备列表,但不幸的是,当我返回 COM 设备时,它们不带有调制解调器名称,并且当我提取调制解调器设备时,它们不与 COM 关联端口(所以如果我连接了两个调制解调器,我不知道哪个在 COM 端口 3 中)。我还找到了一个Win32_SerialPort函数,但这不会返回通过串行端口连接到我的计算机的所有设备。

const   wbemFlagForwardOnly = $00000020;

var

    FSWbemLocator : OLEVariant;
    FWMIService   : OLEVariant;
    FWbemObjectSet: OLEVariant;
    FWbemObject   : OLEVariant;
    oEnum         : IEnumvariant;
    iValue        : LongWord;
    ts            : String;

begin;

    FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
    FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\cimv2', '', '');

//This WMI service checks for plug and play devices
FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_PnPEntity','WQL',wbemFlagForwardOnly);


//This WMI service which I didn't use checks for serial ports and what is on them - currently not displaying sufficient information}
//FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM  Win32_SerialPort','WQL',wbemFlagForwardOnly);

oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;

while oEnum.Next(1, FWbemObject, iValue) = 0 do
    begin
        if not VarIsNull(FWbemObject.name) then
            begin
                ts:= String(FWbemObject.name);

                if pos('(COM',ts)<>0 then
                    pnpForm.listbox1.items.add(ts);
            end;

        FWbemObject:=Unassigned;
    end;
end;
4

1 回答 1

4

...例如,假设我已经提取了我想要的设备在 COM 端口 3 上,并且我也知道它是一个调制解调器,我怎样才能找到与该 COM 端口关联的调制解调器的名称。

您可以使用 WMI 类的AttachedTo属性Win32_POTSModem来获取调制解调器所连接的端口。

试试这个样本

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;


function  GetConnectedModem(const PortName : string):string;
const
  WbemUser            ='';
  WbemPassword        ='';
  WbemComputer        ='localhost';
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin
  Result:='';
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
  FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT Name FROM Win32_POTSModem Where AttachedTo="%s"',[PortName]),'WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  if oEnum.Next(1, FWbemObject, iValue) = 0 then
  begin
    Result:=FWbemObject.Name;
    FWbemObject:=Unassigned;
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      Writeln(GetConnectedModem('COM1'));
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode])); 
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.
于 2012-08-29T01:59:56.707 回答