2010-07-24 65 views
2

我正在通过lpt端口控制某些设备的项目。 我正在使用inpout32.dll来获取对端口的原始访问权限,现在尝试枚举所有可用的LPT端口并获取其I/O范围。如何枚举窗口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 

上线“对于每个pnpentity在pnpentities”我得到的错误。 另外,我不舒服,如果找到相应的实体会帮助我。

PS。 最后我想出了如何枚举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 

回答

5

你得到一个错误,因为PNPDeviceID包含反斜杠(\)和反斜线在WQL查询必须增加一倍。只是做一个有\\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 addresses using Delphi and WMI