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