Search code examples
delphiserial-portenumerate

Serial port enumeration in Delphi using SetupDiGetClassDevs


I'm trying to enumerate "friendly names" for COM ports. The ports may dynamically change as USB-serial devices are connected and disconnected at runtime.

Based on the possible methods described in this question, I am attempting to use the SetupDiGetClassDevs method.

I found this example code, but it is written for an older version of the setupapi unit (the original link to homepages.borland.com doesn't work of course).

I tried using the setupapi unit from the current JVCL(JVCL340CompleteJCL221-Build3845), but it doesn't seem to be compatible with Delphi 7. I get compiler errors:

if SetupDiGetDeviceRegistryProperty(DevInfoHandle,DeviceInfoData,
    RegProperty,
    @PropertyRegDataType,
    @S1[1],RequiredSize,@RequiredSize) then begin

In the call to function SetupDiGetDeviceRegistryProperty, I get the error "Types of actual and formal parameters must be identical" on the parameters @PropertyRegDataType, and @RequiredSize.

The Delphi3000 site says the code was written in 2004 and is intended for Delphi 7, so I'm not sure why it doesn't work with Delphi 7 now, unless setupapi has changed. Is anyone familiar with the changes to setupapi that could cause these problems?

I'm testing with a simple console program. The uses statement is " windows, sysutils, classes, setupAPI, Registry;"

The main program is:

  begin
  ComPortStringList := SetupEnumAvailableComPorts;
  for Index := 0 to ComPortStringList.Count - 1 do
      writeln(ComPortStringList[Index]);
  end;
  end.

Solution

  • Looks like some arguments of type PDWord were replaced by var DWord in SetupApi.pas. All you need is to remove '@' from these arguments in your code like that:

    if SetupDiGetDeviceRegistryProperty(DevInfoHandle,DeviceInfoData,
                                        RegProperty,
                                        PropertyRegDataType,
                                        @S1[1],RequiredSize,RequiredSize) then begin