Search code examples
delphiwindows-7usb32-bitmodem

How to enumerate all installed usb modem using Windows API


I have bought a ZTE MF190 usb modem and began to harness it.

So far, I have managed to programmatically have an sms sent to another mobile equipement using free AsyncPro components of TurboPowers.

I wonder wether there is a way to retrieve the list of all installed modems along with their attached serial COM ports (hopefully with other valuable capabilities).

Thanks in advance.


Solution

  • To list the installed modems you can use the Win32_POTSModem WMI class

    Check this sample code.

    {$APPTYPE CONSOLE}
    
    uses
      SysUtils,
      ActiveX,
      ComObj,
      Variants;
    
    
    procedure  GetWin32_POTSModemInfo;
    const
      WbemUser            ='';
      WbemPassword        ='';
      WbemComputer        ='localhost';
      wbemFlagForwardOnly = $00000020;
    var
      FSWbemLocator : OLEVariant;
      FWMIService   : OLEVariant;
      FWbemObjectSet: OLEVariant;
      FWbemObject   : OLEVariant;
      oEnum         : IEnumvariant;
      iValue        : LongWord;
    begin;
      FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
      FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
      FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_POTSModem','WQL',wbemFlagForwardOnly);
      oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
      while oEnum.Next(1, FWbemObject, iValue) = 0 do
      begin
        Writeln(Format('AttachedTo      %s',[String(FWbemObject.AttachedTo)]));// String
        Writeln(Format('DeviceID        %s',[String(FWbemObject.DeviceID)]));// String
        Writeln(Format('Model           %s',[String(FWbemObject.Model)]));// String
        Writeln(Format('Name            %s',[String(FWbemObject.Name)]));// String
        Writeln(Format('PortSubClass    %s',[String(FWbemObject.PortSubClass)]));// String
        FWbemObject:=Unassigned;
      end;
    end;
    
    
    begin
     try
        CoInitialize(nil);
        try
          GetWin32_POTSModemInfo;
        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.