gpt4 book ai didi

delphi - 通过 Delphi 中的 Windows API 使用 COM 端口号获取设备名称

转载 作者:行者123 更新时间:2023-12-03 15:52:45 29 4
gpt4 key购买 nike

我需要一种方法来使用 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;

最佳答案

...For example lets say I have already extracted that the device I want is on COM port 3 and I also know it is a modem, how can I find the name of the modem associated with that COM port.

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

尝试这个示例

{$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.

关于delphi - 通过 Delphi 中的 Windows API 使用 COM 端口号获取设备名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12163106/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com