代码之家  ›  专栏  ›  技术社区  ›  yatagarasu

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

  •  2
  • yatagarasu  · 技术社区  · 15 年前

    我的工作是通过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 pnpenties In pnpenties'行中,我得到一个错误。 如果找到相应的实体可以帮助我,我也不是舒尔。

    附言。 最后我想出了如何枚举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
    
    1 回复  |  直到 15 年前
        1
  •  5
  •   Community CDub    8 年前

    你出错是因为 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 .

    推荐文章