ポート名の列挙(T)             <TOP>


ポート名を列挙します。

EnumPorts ポート名を列挙する関数

RtlMoveMemory 別の位置にメモリブロックを移動する関数

 

左:PC-VL100/2(WindowsXP)    右:FLORA(Windows2K)

 

例2.FLORA(Windows98)

 

'================================================================
'= ポート名取得
'=    (PortName.bas)
'================================================================
#include"Windows.bi"

' ポート情報を受け取る構造体
Type PORT_INFO_1
    PNAME As Long
End Type

' ポート名を列挙する関数の宣言
Declare Function Api_EnumPorts& Lib "winspool.drv" Alias "EnumPortsA" (ByVal pName$, ByVal Level&, lpPorts As Any, ByVal cbBuf&, pcbNeeded&, pcReturned&)

' ある位置から別の位置にメモリブロックを移動
Declare Function MoveMemory& Lib "Kernel32" Alias "RtlMoveMemory" (Dest As Any, Source As Any, ByVal length&)

Var Shared List1 As Object
List1.Attach GetDlgItem("List1") : List1.SetFontSize 14

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Var Level As Long
    Var Needed As Long
    Var Returned As Long
    Var PortName As String * 64
    Var ct As Long
    Var Ret As Long

    ' 構造体のレベルを指定
    Level = 1

    ' バッファに必要なサイズを取得
    Ret = Api_EnumPorts(Chr$(0), Level, ByVal 0, 0, Needed, Returned)

    ' バッファを確保
    Var Buffer(Needed - 1) As byte

    ' ポート情報を取得
    Ret = Api_EnumPorts(Chr$(0), Level, Buffer(0), Needed, Needed, Returned)

    ' 取得したポート情報の構造体を確保
    Var pi1(Returned - 1) As PORT_INFO_1

    For ct = 0 To Returned - 1

        'データ列から構造体1つ分を抜き取る
        Ret = MoveMemory(pi1(ct), Buffer(ct * Len(pi1(ct))), Len(pi1(ct)))

        'ポインタから最大字数でポート名を得る
        PortName = String$(64, Chr$(0))
        Ret = MoveMemory(PortName, ByVal pi1(ct).PNAME, Len(PortName))

        'ポート名末尾の不要部分を捨てる
        PortName = KLeft$(PortName, KInStr(PortName, Chr$(0)) - 1)
        List1.AddString PortName
    Next CT
End Sub

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