如何使用Windows API枚举所有已安装的USB调制解调器

时间:2012-07-20 03:53:55

标签: delphi windows-7 usb 32-bit modem

我买了一台中兴MF190 USB调制解调器并开始利用它。

到目前为止,我已设法使用 TurboPowers 的免费 AsyncPro 组件以编程方式将短信发送到另一个移动设备。

我想知道是否有办法检索所有已安装调制解调器的列表及其连接的串行COM端口(希望具有其他有价值的功能)。

提前致谢。

1 个答案:

答案 0 :(得分:5)

要列出已安装的调制解调器,您可以使用Win32_POTSModem WMI类

检查此示例代码。

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