Pippa Rose Smith
Pippa Rose Smith

Reputation: 1416

Getting device names using a COM port number through the Windows API in Delphi

I need a way to use the WMI to find the name of a modem (or other device) which is currently attatched to a COM port which I already know.

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.

Currently I have code using Win32_PnPEntity which can extract a list of pnp devices with either modem or COM in the name but unfortunatley when I return the COM devices they do not carry the modem name and when I extract modem device they do not associate with a COM port (so if I have two modems attached I do not know which is in COM port 3). I have also found a Win32_SerialPort function but this does not return all devices attatched to my computer via serial ports.

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;

Upvotes: 3

Views: 5275

Answers (1)

RRUZ
RRUZ

Reputation: 136391

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

You can use the AttachedTo property of the Win32_POTSModem WMI class to get the Port to which the modem is attached.

Try this sample

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

Upvotes: 4

Related Questions