我也插个嘴,这个问题处理下com错误就行了
大佬什么意思麻烦指点一下 万分感谢{:1_227:}
#include <GuiListView.au3>
#include <Editconstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <ARRAY.au3>
Global $__g_oTemplateCOMErrorHandler = 0
Template_COMErrorRegister()
Global $objWMIService
Global $ListView_HitItem, $Listview_HitItemIndex, $Listview_HitItemSubItem, $ListView_HitItemInput, $Portnames;定义全局变量
Global Const $DBT_DEVICEARRIVAL = 0x8000 ;设备插入
Global Const $DBT_DEVICEREMOVECOMPLETE = 0x8004 ;设备移除
Global Const $DBT_DEVTYP_PORT = 0x00000003
$Form = GUICreate("端口读取工具", 820, 450)
$Button = GUICtrlCreateButton('OK', 727, 412, 80, 30, -1, $WS_EX_WINDOWEDGE)
GUICtrlCreateLabel("BY-LayVersion1.0.1", 8, 420, 200, 40)
Local $sFont = "宋体"
GUICtrlSetFont(-1, 10, 100, 2, $sFont)
GUICtrlSetState(-1, 128)
GUISetFont(9, 400, 0, "微软雅黑")
$Combox = GUICtrlCreateCombo('', 620, 415, 80, 30)
GUICtrlSetTip($Combox, '选择已有的快捷方式名称,或者手动粘贴需要打开软件的路径', '提示', 1, 1)
$Check = GUICtrlCreateCheckbox('', 600, 415, 15, 30)
GUICtrlSetTip($Check, '勾选复选框,根据端口数量打开对应软件窗口数量', '提示', 1, 1)
GUICtrlSetData($Combox, 'QGNSS|PowerGPS|TTFF_Tester', 'QGNSS')
$ListView = GUICtrlCreateListView("序列(List)|端口(Port)|模块编号(ModuleId)", 8, 8, 800, 400)
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES);扩展风格
$label = GUICtrlCreateLabel("", -100, -100, 1, 1)
GUISetState(@SW_SHOW)
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 0, 100)
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 1, 500)
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 2, 195)
Local $Ports = ''
$objWMIService = ObjGet("WinMgmts:")
$colBios = $objWMIService.ExecQuery("Select * fromWin32_PNPEntity")
For $object In $colBios
$Ports &= StringMid($object.Caption, 1) & @CRLF
Next
$Bports = StringRegExp($Ports, '(?m).+\(COM\d+\)$', 3)
If UBound($Bports) < 1 Then
Sleep(50)
Else
For $i = 1 To UBound($Bports)
GUICtrlCreateListViewItem($i & '|' & $Bports[$i - 1] & "|", $ListView)
Next
EndIf
GUIRegisterMsg($WM_DEVICECHANGE, 'My_Message_Test')
;~ GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
;~ GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
While GUIGetMsg() <> -3
Sleep(10)
WEnd
Template_COMErrorUnregister()
Func My_Message_Test($hWnd, $Msg, $wParam, $lParam)
Switch $wParam
Case $DBT_DEVICEARRIVAL
Local $dbhdr = DllStructCreate('DWORD size;DWORD devicetype;DWORD reserved', $lParam)
If DllStructGetData($dbhdr, "devicetype") = $DBT_DEVTYP_PORT Then
Local $dbport = DllStructCreate('DWORD size;DWORD devicetype;DWORD reserved;wchar name', $lParam)
$Portnames = DllStructGetData($dbport, 'name')
AdlibRegister('_Refresh', 500)
EndIf
EndSwitch
EndFunc ;==>My_Message_Test
Func _Refresh()
$colBios = $objWMIService.ExecQuery("Select * from Win32_SerialPort")
If @error Then Return SetError(1, 0, '')
AdlibUnRegister('_Refresh')
GUICtrlSendMsg($ListView, $LVM_DELETEALLITEMS, 0, 0)
Local $Ports
For $object In $colBios
$Ports &= StringMid($object.Caption, 1) & @CRLF
Next
$Bports = StringRegExp($Ports, '(?m).+\(COM\d+\)$', 3)
If Not @error Then
For $i = 1 To UBound($Bports)
GUICtrlCreateListViewItem($i & '|' & $Bports[$i - 1] & "|", $ListView)
Next
EndIf
EndFunc ;==>_Refresh
Func Template_COMErrorRegister()
$__g_oTemplateCOMErrorHandler = ObjEvent("AutoIt.Error", "Template_COMErrFunc")
EndFunc ;==>Template_COMErrorRegister
Func Template_COMErrorUnregister()
$__g_oTemplateCOMErrorHandler = 0
EndFunc ;==>Template_COMErrorUnregister
Func Template_COMErrFunc()
EndFunc ;==>Template_COMErrFunc
页:
1
[2]