如何枚举 Windows LPT 端口及其 I/O 范围?

发布于 2024-09-11 09:53:51 字数 1189 浏览 12 评论 0原文

我正在开发通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2024-09-18 09:53:51

您会收到错误,因为 PNPDeviceID 包含反斜杠 (\),并且 WQL 查询中的反斜杠必须加倍。只需将 port.PNPDeviceID 中的 \ 替换为 \\,然后将其插入到查询中,您的脚本就会正常工作:

strPNPDeviceID = Replace(port.PNPDeviceID, "\", "\\")
Set pnpentities = wmiService.ExecQuery( _
    "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID = '" & strPNPDeviceID & "'")

您可能还会发现这个问题很有用:如何使用 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 \\ in port.PNPDeviceID before inserting it into your query, and your script will work fine:

strPNPDeviceID = Replace(port.PNPDeviceID, "\", "\\")
Set pnpentities = wmiService.ExecQuery( _
    "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID = '" & strPNPDeviceID & "'")

You may also find this question useful: How to find available parallel ports and their I/O addresses using Delphi and WMI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文