找回密码
 加入
搜索
查看: 4942|回复: 4

[AU3基础] [已解决]AU3如何检查设备管理器里面端口名称这一项?

[复制链接]
发表于 2013-5-10 10:44:34 | 显示全部楼层 |阅读模式
本帖最后由 user11 于 2013-5-14 14:25 编辑

我搜了一下论坛都是用wmi获取,可是都是获取cpu 声卡 显卡,我用到个端口里面的名称插在不同的usb接口会改变,因为程序软件每次选择端口也不同

看图,


USB设备,转换成com了,前面的名称名称是很一样的,但是后面的COM9 这个值 是插在不同usb口是不一样,因为每次程序软件里面的设置是不一样,

求高手给个能检查出这个com口数字的代码,谢谢了。例如下面可以检测到键盘鼠标,为什么没有那个usb呢?

info()

func info()
        
$objWMIService = objget("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
$colPorts = $objWMIService.ExecQuery("Select * from Win32_PortConnector")
               
                          


FOR $Port IN $colPorts
        
               
                           MsgBox(0,"",$Port.ExternalReferenceDesignator)
        
Next        

EndFunc

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2013-5-12 17:47:10 | 显示全部楼层
#include <WinAPI.au3>

Const $SETUPAPI = DllOpen("setupapi.dll")

Const $GUID_DEVINTERFACE_COMPORT = "{86E0D1E0-8089-11D0-9CE4-08003E301F73}"

Const $tagSP_DEVICE_INTERFACE_DATA = "dword cbSize;" & $tagGUID & ";dword Flags;ulong_ptr Reserved"
Const $tagSP_DEVINFO_DATA = "dword cbSize;" & $tagGUID & ";dword DevInst;ulong_ptr Reserved"
Const $tagSP_DEVICE_INTERFACE_DETAIL_DATA = "dword cbSize;wchar szDevicePath[4096]"

Const $IOCTL_SERENUM_GET_PORT_NAME = 0x37020C

Local $tInterfaceGUID = _WinAPI_GUIDFromString($GUID_DEVINTERFACE_COMPORT)
Local $pInterfaceGUID = DllStructGetPtr($tInterfaceGUID)

Local $hDeviceInfoList = SetupDiGetClassDevs($pInterfaceGUID, "", 0, 18) ; 18 = DIGCF_PRESENT|DIGCF_DEVICEINTERFACE

Local $tInterfaceInfo = DllStructCreate($tagSP_DEVICE_INTERFACE_DATA)
Local $pInterfaceInfo = DllStructGetPtr($tInterfaceInfo)
Local $tDeviceInfo = DllStructCreate($tagSP_DEVINFO_DATA)
Local $pDeviceInfo = DllStructGetPtr($tDeviceInfo)
Local $tInterfaceDetail = DllStructCreate($tagSP_DEVICE_INTERFACE_DETAIL_DATA)
Local $pInterfaceDetail = DllStructGetPtr($tInterfaceDetail)

Local $iIndex, $sDevicePath, $hFile

DllStructSetData($tInterfaceInfo, "cbSize", DllStructGetSize($tInterfaceInfo))
DllStructSetData($tDeviceInfo, "cbSize", DllStructGetSize($tDeviceInfo))

While SetupDiEnumDeviceInterfaces($hDeviceInfoList, 0, $pInterfaceGUID, $iIndex, $pInterfaceInfo)
        $iIndex += 1

        DllStructSetData($tInterfaceDetail, "cbSize", 6)
        DllStructSetData($tInterfaceDetail, "szDevicePath", "")

        If Not SetupDiGetDeviceInterfaceDetail($hDeviceInfoList, _
                $pInterfaceInfo, $pInterfaceDetail, 4096 * 2 + 4, $pDeviceInfo) Then

                ContinueLoop
        EndIf
        $sDevicePath = DllStructGetData($tInterfaceDetail, "szDevicePath")
        $hFile = CreateFile($sDevicePath, 0x80000000, 3, 0, 3, 0, 0)

        If $hFile = -1 Then
                ContinueLoop
        EndIf

        If DeviceIoControl($hFile, $IOCTL_SERENUM_GET_PORT_NAME, 0, 0, DllStructGetPtr($tInterfaceDetail) + 4, 4096 * 2) Then
                
                $iResult = DllCall($SETUPAPI, "bool", "SetupDiGetDeviceRegistryPropertyW", "ptr", $hDeviceInfoList, "ptr", $pDeviceInfo, "long", 12, "dword*", 0, "wstr", "", "dword*", 32768, "long*", 0)

                If $iResult[5] = "" Then
                        $iResult = DllCall($SETUPAPI, "bool", "SetupDiGetDeviceRegistryPropertyW", "ptr", $hDeviceInfoList, "ptr", $pDeviceInfo, "long", 0, "dword*", 0, "wstr", "", "dword*", 32768, "long*", 0)

                        If $iResult[5] = "" Then $iResult[5] = "(unknown device)"
                EndIf

                MsgBox(0, $iResult[5], DllStructGetData($tInterfaceDetail, "szDevicePath"))
                
        EndIf

        CloseHandle($hFile)
WEnd


Func DeviceIoControl($hFile, $iIOCTL, $pInBuffer, $iInBuffer, $pOutBuffer, $iOutBuffer, $pOverlapped = 0)
        Local $iResult = DllCall("Kernel32.dll", "bool", "DeviceIoControl", "handle", $hFile, "dword", $iIOCTL, "ptr", $pInBuffer, "dword", $iInBuffer, "ptr", $pOutBuffer, "dword", $iOutBuffer, "dword*", 0, "ptr", $pOverlapped)

        Return SetError(ster($iResult[0]), 0, $iResult[0])
EndFunc        ;==>DeviceIoControl

Func CloseHandle($hHandle)
        Local $iResult = DllCall("Kernel32.dll", "bool", "CloseHandle", "handle", $hHandle)
        Return SetError(ster($iResult[0]), 0, $iResult[0])
EndFunc        ;==>CloseHandle

Func CreateFile($sFilePath, $iDesiredAccess, $iShareMode, $pSecurityAttributes, _
        $iCreationDisposition, $iFlagsAndAttributes, $hTemplate = 0)

        Local $iResult = DllCall("Kernel32.dll", "long", "CreateFileW", "wstr", $sFilePath, "dword", $iDesiredAccess, "dword", $iShareMode, "ptr", $pSecurityAttributes, "dword", $iCreationDisposition, "dword", $iFlagsAndAttributes, "handle", $hTemplate)

        Return SetError(ster($iResult[0] <> -1), 0, $iResult[0])
EndFunc        ;==>CreateFile

Func SetupDiGetDeviceInterfaceDetail($hDeviceInfoList, $pInterfaceInfo, $pInterfaceDetail, $iDetailSize, $pDeviceInfo)

        Local $iResult = DllCall($SETUPAPI, "bool" ,"SetupDiGetDeviceInterfaceDetailW", "ptr", $hDeviceInfoList, "ptr", $pInterfaceInfo, "ptr", $pInterfaceDetail, "dword", $iDetailSize, "dword*", 0, "ptr", $pDeviceInfo)

        Return SetError(ster($iResult[0]), $iResult[5], $iResult[0])
EndFunc        ;==>SetupDiGetDeviceInterfaceDetail

Func SetupDiGetClassDevs($pClassGUID, $sEnumerator, $hWnd, $iFlags)
        If $sEnumerator Then
                Local $sParamType = "wstr"
        Else
                Local $sParamType = "ptr"
        EndIf

        Local $iResult = DllCall($SETUPAPI, "ptr", "SetupDiGetClassDevsW", "ptr", $pClassGUID, $sParamType, $sEnumerator, "hwnd", $hWnd, "dword", $iFlags)

        Return SetError(ster($iResult[0]), 0, $iResult[0])
EndFunc        ;==>SetupDiGetClassDevs

Func SetupDiEnumDeviceInterfaces($hDeviceInfoList, $pDeviceInfo, _
        $pInterfaceGUID, $iIndex, $pInterfaceInfo)

        Local $iResult = DllCall($SETUPAPI, "bool", "SetupDiEnumDeviceInterfaces", "ptr", $hDeviceInfoList, "ptr", $pDeviceInfo, "ptr", $pInterfaceGUID, "dword", $iIndex, "ptr", $pInterfaceInfo)

        Return SetError(ster($iResult[0]), 0, $iResult[0])
EndFunc        ;==>SetupDiEnumDeviceInterfaces

Func ster($iResult = 0)
        If $iResult Then
                Return 0
        Else
                $iResult = DllCall("Kernel32.dll", "long", "GetLastError")
                Return $iResult[0]
        EndIf
EndFunc        ;==>ster
 楼主| 发表于 2013-5-14 14:24:23 | 显示全部楼层
回复 2# pusofalse

管理员果然厉害,,虽然没看明白代码是什么意思,,不过非常管用!!可以准确的检测到当前的端口!!非常感谢!!!前几天自己也试验了用读取注册表的方法写了一个
发表于 2013-5-14 18:06:36 | 显示全部楼层
回复 3# user11


    还以为不兼容WIN7等64位系统,能成功获取就好~
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-12-29 09:54 , Processed in 0.086375 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表