如何枚举 Windows LPT 端口及其 I/O 范围?
我正在开发通过 lpt 端口控制某些设备的项目。 我正在使用 inpout32.dll 来获取对端口的原始访问权限,现在尝试枚举所有可用的 LPT 端口并获取它们的 I/O 范围。
我现在可以检查设备管理器,但是有没有更自动化的方法?
现在我尝试使用 WMI 一些应该可以工作的示例代码,但它不在
Set wmiService = GetObject("winmgmts:\\.\root\cimv2")
Set parallelports = wmiService.ExecQuery("SELECT * FROM Win32_ParallelPort")
For Each port In parallelports
q = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID = '" & port.PNPDeviceID & "'"
Set pnpentities = wmiService.ExecQuery(q)
For Each pnpentity In pnpentities
wscript.echo pnpentity.PNPDeviceID
Next
Next
“For Each pnpentity In pnpentities”行上出现错误。 我也不确定找到相应的实体是否会对我有帮助。
附言。 最后我弄清楚了如何枚举 lpt i/o 端口范围。
Set wmiService = GetObject("winmgmts:\\.\root\cimv2")
Set parallelports = wmiService.ExecQuery("SELECT * FROM Win32_ParallelPort")
For Each port In parallelports
Set port_resources = wmiService.ExecQuery("ASSOCIATORS OF {Win32_ParallelPort.DeviceID='" & port.DeviceID & "'} WHERE ResultClass = Win32_PortResource")
For Each port_resource In port_resources
wscript.echo port_resource.Caption
Next
Next
I am working on project for controlling some devices through lpt port.
I am using inpout32.dll to get raw access to ports and now trying to enumerate all available LPT ports and get their I/O Range.
I now I can check device manager, but is there any more automated way?
Now I am trying to use WMI some sample code that should work but it does not
Set wmiService = GetObject("winmgmts:\\.\root\cimv2")
Set parallelports = wmiService.ExecQuery("SELECT * FROM Win32_ParallelPort")
For Each port In parallelports
q = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID = '" & port.PNPDeviceID & "'"
Set pnpentities = wmiService.ExecQuery(q)
For Each pnpentity In pnpentities
wscript.echo pnpentity.PNPDeviceID
Next
Next
on line 'For Each pnpentity In pnpentities' I get error.
Also I am not shure if finding corresponding entity will help me.
PS.
Finally i figured out how to enumerate lpt i/o port ranges.
Set wmiService = GetObject("winmgmts:\\.\root\cimv2")
Set parallelports = wmiService.ExecQuery("SELECT * FROM Win32_ParallelPort")
For Each port In parallelports
Set port_resources = wmiService.ExecQuery("ASSOCIATORS OF {Win32_ParallelPort.DeviceID='" & port.DeviceID & "'} WHERE ResultClass = Win32_PortResource")
For Each port_resource In port_resources
wscript.echo port_resource.Caption
Next
Next
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您会收到错误,因为
PNPDeviceID
包含反斜杠 (\
),并且 WQL 查询中的反斜杠必须加倍。只需将port.PNPDeviceID
中的\
替换为\\
,然后将其插入到查询中,您的脚本就会正常工作:您可能还会发现这个问题很有用:如何使用 Delphi 和 WMI 查找可用的并行端口及其 I/O 地址。
You get an error because
PNPDeviceID
contains backslashes (\
) and backslashes in WQL queries must be doubled. Just do a replace of\
with\\
inport.PNPDeviceID
before inserting it into your query, and your script will work fine:You may also find this question useful: How to find available parallel ports and their I/O addresses using Delphi and WMI.