ポート名列挙(U)          <TOP>


ポート名、モニタ名、ポートタイプ等を列挙します。

EnumPorts ローカルポートの列挙

RtlMoveMemory メモリコピー

 

 

'================================================================
'= ポート名列挙U
'=    (EnumPorts.bas)
'================================================================
#include "Windows.bi"

Type PORT_INFO_1
    pName As String * 256
End Type

Type PORT_INFO_2
    pPortName    As Long
    pMonitorName As Long
    pDescription As Long
    fPortType    As Long
    Reserved     As Long
End Type

#define PORT_TYPE_NET_ATTACHED &H8      '
#define PORT_TYPE_READ &H2              '
#define PORT_TYPE_REDIRECTED &H4        '
#define PORT_TYPE_WRITE &H1             '
#define vbNullString byval 0            '値0の文字列。値0を持つ文字列。空文字列ではない
#define vbNullChar chr$(0)              'NULL文字(\0)

' ローカルポートを列挙
Declare Function Api_EnumPorts& Lib "winspool.drv" Alias "EnumPortsA" (ByVal pName$, ByVal Level&, lpbPorts As byte, ByVal cbBuf&, pcbNeeded&, pcReturned&)

' メモリの指定領域をコピー
Declare Sub Api_RtlMoveMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length&)

' 文字列をコピーする
Declare Function Api_lstrcpy& Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1$, ByVal lpString2&)

' 指定された文字列のバイトまたは文字の長さを返す
Declare Function Api_lstrlen& Lib "kernel32" Alias "lstrlenA" (ByVal lpString&)

Var Shared List1 As Object

List1.Attach GetDlgItem("List1") : List1.SetFontSize 12

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Var Needed As Long
    Var Returned As Long
    Var Buff As String
    Var i As Integer
    Var Ret As Long

    Ret = Api_EnumPorts(Chr$(0), 2, ByVal 0, 0, Needed, Returned)

    If Ret = 0 Then
        Var bytBuff(Needed - 1) As byte

        'ローカルポートの情報を取得
        Ret = Api_EnumPorts(vbNullString, 2, bytBuff(0), Needed, Needed, Returned)

        If Ret Then
            If Returned > 0 Then
                Var pi2(Returned - 1) As PORT_INFO_2
                Api_RtlMoveMemory pi2(0).pPortName, bytBuff(0), Len(pi2(0)) * (Returned)
                For i = 0 To Returned - 1

                    'ポート名
                    Ret = Api_lstrlen(pi2(i).pPortName)
                    Buff = String$(Ret, Chr$(0))
                    Ret = Api_lstrcpy(Buff, pi2(i).pPortName)
                    List1.AddString Buff

                    'モニター名
                    Ret = Api_lstrlen(pi2(i).pMonitorName)
                    Buff = String$(Ret, Chr$(0))
                    Ret = Api_lstrcpy(Buff, pi2(i).pMonitorName)
                    List1.AddString Buff

                    '種類
                    Ret = Api_lstrlen(pi2(i).pDescription)
                    Buff = String$(Ret, Chr$(0))
                    Ret = Api_lstrcpy(Buff, pi2(i).pDescription)
                    List1.AddString Buff

                    'ポートタイプ
                    List1.AddString "ポートタイプ = " & Str$(pi2(i).fPortType)
                    List1.AddString "-----------------------------"
                Next i
            End If
        End If
    End If
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End