[已解决]AU3如何检查设备管理器里面端口名称这一项?
本帖最后由 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 #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"
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 = "" Then
$iResult = DllCall($SETUPAPI, "bool", "SetupDiGetDeviceRegistryPropertyW", "ptr", $hDeviceInfoList, "ptr", $pDeviceInfo, "long", 0, "dword*", 0, "wstr", "", "dword*", 32768, "long*", 0)
If $iResult = "" Then $iResult = "(unknown device)"
EndIf
MsgBox(0, $iResult, 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, $iResult)
EndFunc ;==>DeviceIoControl
Func CloseHandle($hHandle)
Local $iResult = DllCall("Kernel32.dll", "bool", "CloseHandle", "handle", $hHandle)
Return SetError(ster($iResult), 0, $iResult)
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 <> -1), 0, $iResult)
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), $iResult, $iResult)
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, $iResult)
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, $iResult)
EndFunc ;==>SetupDiEnumDeviceInterfaces
Func ster($iResult = 0)
If $iResult Then
Return 0
Else
$iResult = DllCall("Kernel32.dll", "long", "GetLastError")
Return $iResult
EndIf
EndFunc ;==>ster 回复 2# pusofalse
管理员果然厉害,,虽然没看明白代码是什么意思,,不过非常管用!!可以准确的检测到当前的端口!!非常感谢!!!前几天自己也试验了用读取注册表的方法写了一个 回复 3# user11
{:face (460):}还以为不兼容WIN7等64位系统,能成功获取就好~
页:
[1]