Dim aa$, t#
Private Sub Command1_Click()
Call Shell("cmd /c mode >c:\tt.txt", vbHide)
t = Timer
Do: DoEvents: Loop Until Timer > t + 1
Open "c:\tt.txt" For Input As #1
While Not EOF(1)
Line Input #1, aa
If InStr(aa, "COM") > 0 Then Print aa
Wend
Close #1
'Kill "c:\tt.txt"
End Sub
'*********************************读取注册表串口值
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
'Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_SZ = 1
Dim i&, ComStr$(), S
Private Sub Command1_Click()
MsgBox GetAllPort
End Sub
Public Function GetAllPort() As String
On Error Resume Next
S = GetSerialPort(HKEY_LOCAL_MACHINE, "HARDWARE\DEVICEMAP\SERIALCOMM")
If ComStr(0) = "" Then Exit Function
GetAllPort = ""
For i = 0 To UBound(S)
GetAllPort = GetAllPort & S(i) & ","
Next i
End Function
Public Function GetSerialPort(RegAddr&, Items$) As String()
On Error Resume Next
Dim hKey&, S1$, S2$, L&, L1&, J&
RegOpenKey RegAddr, Items, hKey
ReDim Preserve ComStr$(0)
ComStr(0) = "": i = 0: J = 0: Rtn = 0
Do
L = 1000: L1 = 1000
S1 = Space(L): S2 = Space(L)
Rtn = RegEnumValue(hKey, i, S1, L, 0, REG_SZ, S2, L1)
If Rtn = 0 Then
If InStr(S1, Chr(0)) > 0 And InStr(S2, Chr(0)) > 0 Then
S1 = UCase(Left(S1, InStr(S1, Chr(0)) - 1))
S2 = UCase(Left(S2, InStr(S2, Chr(0)) - 1))
If InStr(S2, "COM") > 0 Then
ReDim Preserve ComStr$(J)
ComStr(J) = S2
J = J + 1
End If
End If
End If
i = i + 1
Loop Until Rtn <> 0
GetSerialPort = ComStr()
End Function