Newsgroup: it.comp.lang.visual-basic
Titolo: porte seriali di sistema.

Ciao a tutti,
esiste una istruzione in visual basic che permette di individuare quali sono
le porte seriali di sistema?Eventualmente come si usa?
Grazie,
Salvo.

 le porte seriali di sistema?Eventualmente come si usa?
Avevo recuperato il codice qui sotto da qualche parte ma non mi ricordo
dove; ritorna un array con tutte le porte, devi quindi testare se sono COM
Ciao
Lawrence Lebowsky
Public Type PORT_INFO_2
    pPortName As String
    pMonitorName As String
    pDescription As String
    fPortType As Long
    Reserved As Long
End Type
Public Type API_PORT_INFO_2
    pPortName As Long
    pMonitorName As Long
    pDescription As Long
    fPortType As Long
    Reserved As Long
End Type
Private Declare Function EnumPorts Lib "winspool.drv" Alias "EnumPortsA"
(ByVal pName As String, ByVal Level As Long, ByVal lpbPorts As Long, ByVal
cbBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As
Long
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (pTo As
Any, uFrom As Any, ByVal lSize As Long)
Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long,
ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GetProcessHeap Lib "kernel32" () As Long
Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal
dwFlags As Long, lpMem As Any) As Long
Private Declare Function ShellExecute Lib "Shell32.dll" Alias
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal
lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String,
ByVal nShowCmd As Long) As Long
Private Const SW_SHOWNORMAL = 1
Public Declare Function SendMessage Lib _
   "user32" Alias "SendMessageA" _
   (ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long
Public Const LB_SETTABSTOPS = &H192
Public Function TrimStr(strName As String) As String
    'Finds a null then trims the string
    Dim X As Integer
    X = InStr(strName, vbNullChar)
    If X > 0 Then TrimStr = Left(strName, X - 1) Else TrimStr = strName
End Function
Public Function LPSTRtoSTRING(ByVal lngPointer As Long) As String
    Dim lngLength As Long
    'Get number of characters in string
    lngLength = lstrlenW(lngPointer) * 2
    'Initialize string so we have something to copy the string into
    LPSTRtoSTRING = String(lngLength, 0)
    'Copy the string
    CopyMem ByVal StrPtr(LPSTRtoSTRING), ByVal lngPointer, lngLength
    'Convert to Unicode
    LPSTRtoSTRING = TrimStr(StrConv(LPSTRtoSTRING, vbUnicode))
End Function
'Use ServerName to specify the name of a Remote Workstation i.e.
"//WIN95WKST"
'or leave it blank "" to get the ports of the local Machine
Public Function GetAvailablePorts(ServerName As String) As PORT_INFO_2()
    Dim ret As Long
    Dim PortsStruct(0 To 100) As API_PORT_INFO_2
    Dim Ports() As PORT_INFO_2
    Dim pcbNeeded As Long
    Dim pcReturned As Long
    Dim TempBuff As Long
    Dim i As Integer
    'Get the amount of bytes needed to contain the data returned by the API
call
    ret = EnumPorts(ServerName, 2, TempBuff, 0, pcbNeeded, pcReturned)
    'Allocate the Buffer
    TempBuff = HeapAlloc(GetProcessHeap(), 0, pcbNeeded)
    ret = EnumPorts(ServerName, 2, TempBuff, pcbNeeded, pcbNeeded,
pcReturned)
    If ret Then
        'Convert the returned String Pointer Values to VB String Type
        CopyMem PortsStruct(0), ByVal TempBuff, pcbNeeded
        ReDim Ports(pcReturned - 1)
        For i = 0 To pcReturned - 1
            Ports(i).pDescription =
LPSTRtoSTRING(PortsStruct(i).pDescription)
            Ports(i).pPortName = LPSTRtoSTRING(PortsStruct(i).pPortName)
            Ports(i).pMonitorName =
LPSTRtoSTRING(PortsStruct(i).pMonitorName)
            Ports(i).fPortType = PortsStruct(i).fPortType
        Next
    End If
    Rem GetAvailablePorts = pcReturned
    'Free the Heap Space allocated for the Buffer
    If TempBuff Then HeapFree GetProcessHeap(), 0, TempBuff
    GetAvailablePorts = Ports()
End Function

E questo mi rileva anche le porte associate ad eventuali modem integrati ?
Ciao, Claudio.