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
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.