Delphi中使用SetupDiGetClassDevs进行串口枚举 [英] Serial port enumeration in Delphi using SetupDiGetClassDevs

查看:341
本文介绍了Delphi中使用SetupDiGetClassDevs进行串口枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试枚举COM端口的友好名称。由于USB串行设备在运行时连接和断开,端口可能会动态更改。



根据,我试图使用SetupDiGetClassDevs方法。



我发现此示例代码,但它是为较早版本的setupapi单元编写的(原始链接到homeepages.borland.com当然不起作用)。



我尝试使用当前JVCL中的setupapi单元( JVCL340CompleteJCL221-Build3845 ),但它似乎不兼容Delphi 7.我得到编译错误:

 如果SetupDiGetDeviceRegistryProperty(DevInfoHandle,DeviceInfoData,
RegProperty,
@PropertyRegDataType,
@ S1 [1],RequiredSize, @RequiredSize)然后开始

在调用函数 SetupDiGetDeviceRegistryProperty 时,
在参数 @PropertyRegDataType @RequiredSize 之间得到错误实际和形式参数的类型必须相同。



Delphi3000站点说,代码是在2004年写的,适用于Delphi 7,所以我不知道为什么它现在不适用于Delphi 7,除非setupapi已经改变。有人熟悉setupapi的更改可能会导致这些问题吗?



我正在使用简单的控制台程序进行测试。用法语句是windows,
sysutils,
类,
setupAPI,
Registry;



主程序是:

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


解决方案

看起来像一些参数类型 SetupApi.pas 中替换为 var DWord 的PDWord 。所有你需要的是从你的代码中的这些参数中删除'@':

 如果SetupDiGetDeviceRegistryProperty(DevInfoHandle,DeviceInfoData,
RegProperty,
PropertyRegDataType,
@ S1 [1],RequiredSize,RequiredSize)然后开始


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.

解决方案

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

这篇关于Delphi中使用SetupDiGetClassDevs进行串口枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆