ネットワークドライブのファイル名から付加情報を取得          <TOP>


ネットワークドライブ名から付加情報を取得します。

WNetGetUniversalName ドライブベースのパスをネットワーク資源とし、その資源のより汎用的な名前を含む情報構造体を取得

 

例では、XおよびYドライブの情報を取得しています。

 

'================================================================
'= ネットワークドライブのファイル名から付加情報を取得
'=    (WNetGetUniversalName.bas)
'================================================================
#include "Windows.bi"

' ドライブベースのパスをネットワーク資源とし、その資源のより汎用的な名前を含む情報構造体を取得
Declare Function Api_WNetGetUniversalName& Lib "mpr" Alias "WNetGetUniversalNameA" (ByVal lpLocalPath$, ByVal dwInfoLevel&, lpBuffer As Any, lpBufferSize&)

' ある位置から別の位置にメモリブロックを移動する関数の宣言
Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length&)

Type REMOTE_NAME_INFO
    lpUniversalName  As Long
    lpConnectionName As Long
    lpRemainingPath  As Long
End Type

#define UNIVERSAL_NAME_INFO_LEVEL &H1   'UNIVERSAL_NAME_INFO構造体をバッファに格納
#define REMOTE_NAME_INFO_LEVEL &H2      'REMOTE_NAME_INFO構造体をバッファに格納
#define NO_ERROR 0

Var Shared Text(6) As Object
Var Shared Edit1 As Object
Var Shared Button1 As Object

For i = 0 To 6
    Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1)))
    Text(i).SetFontSize 14
Next
Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 14
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub mainForm_Start()
    Edit1.SetWindowtext "X:\Windows\Twein.dll"
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var rni As REMOTE_NAME_INFO
    Var BufferSize As Long
    Var Buffer As String
    Var bytBuffer(1023) As Byte
    Var Ret As Long
    
    'リモート名情報を取得
    Ret = Api_WNetGetUniversalName(Edit1.GetWindowText, REMOTE_NAME_INFO_LEVEL, bytBuffer(0), 1023)

    If Ret = NO_ERROR Then
        'バッファに取得した情報をリモート名情報構造体へコピー
        CopyMemory rni, bytBuffer(0), Len(rni)

        'UNC名を文字列バッファへコピー
        Buffer = Space$(511)
        CopyMemory Buffer, ByVal rni.lpUniversalName, Len(Buffer)

        'UNC名を表示
        Text(4).SetWindowText Left$(Buffer, InStr(Buffer, Chr$(0)) - 1)

        '接続名を文字列バッファへコピー
        Buffer = Space$(511)
        CopyMemory Buffer, ByVal rni.lpConnectionName, Len(Buffer)

        '接続名を文字列バッファへコピー
        Text(5).SetWindowText Left$(Buffer, InStr(Buffer, Chr$(0)) - 1)

        'パス名を文字列バッファへコピー
        Buffer = Space$(511)
        CopyMemory Buffer, ByVal rni.lpRemainingPath, Len(Buffer)

        'パス名を表示
        Text(6).SetWindowText Left$(Buffer, InStr(Buffer, Chr$(0)) - 1)
    End If
End Sub

'================================================================
'=
'================================================================
Declare Sub Edit1_SetFocus edecl ()
Sub Edit1_SetFocus()
    For i = 4 To 6
        Text(i).SetWindowtext ""
    Next
End Sub

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