2

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
4

1 回答 1

5

PNPDeviceID由于包含反斜杠 ( \) 并且 WQL 查询中的反斜杠必须加倍,您会收到错误消息。只需在将其插入查询之前替换\with \\in ,您的脚本就可以正常工作:port.PNPDeviceID

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


您可能还会发现这个问题很有用:How to find available parallel ports and their I/O address using Delphi and WMI

于 2010-07-24T16:49:26.973 回答