コンピュータ上のボリューム名を取得          <TOP>


GetLogicalDrives 利用可能ディスクドライブ取得
FindVolumeClose 指定したボリューム検索ハンドルを閉じる
FindFirstVolume コンピュータ上のボリューム名を返す
FindNextVolume FindFirstVolume 関数により開始したボリューム検索を継続
lstrlenW (Null文字で終了する)UNICODE文字列の文字数を返す
GetVolumeNameForVolumeMountPoint マウントポイントまたはルートディレクトリを取得し、それに対応する一意のボリューム名を返す
 

 

'================================================================
'= コンピュータ上のボリューム名を取得
'=    (FindFirstVolume.bas)
'================================================================
#include "Windows.bi"

' 利用可能ディスクドライブ取得
Declare Function Api_GetLogicalDrives& Lib "kernel32" Alias "GetLogicalDrives" ()

' 指定したボリューム検索ハンドルを閉じる
Declare Function Api_FindVolumeClose& Lib "kernel32" Alias "FindVolumeClose" (ByVal hFindVolume&)

' コンピュータ上のボリューム名を返す  
Declare Function Api_FindFirstVolume& Lib "kernel32" Alias "FindFirstVolumeA" (ByVal lpszVolumeName$, ByVal cchBufferLength&)

' FindFirstVolume 関数により開始したボリューム検索を継続  
Declare Function Api_FindNextVolume& Lib "kernel32" Alias "FindNextVolumeA" (ByVal hFindVolume&, ByVal lpszVolumeName$, ByVal cchBufferLength&)

' (Null文字で終了する)UNICODE文字列の文字数を返す  
Declare Function Api_lstrlenW& Lib "kernel32" Alias "lstrlenW" (ByVal lpString&)

' マウントポイントまたはルートディレクトリを取得し、それに対応する一意のボリューム名を返す
Declare Function Api_GetVolumeNameForVolumeMountPoint& Lib "kernel32" Alias "GetVolumeNameForVolumeMountPointA" (ByVal lpszVolumeMountPoint$, ByVal lpszVolumeName$, ByVal cchBufferLength&)

Var Shared List1 As Object
Var Shared Combo1 As Object
Var Shared Button1 As Object
Var Shared Button2 As Object

List1.Attach GetDlgItem("List1") : List1.SetFontSize 12 : List1.SetWindowSize 306, 66
Combo1.Attach GetDlgItem("Combo1") : Combo1.SetFontSize 14
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14
Button2.Attach GetDlgItem("Button2") : Button2.SetFontSize 14

'================================================================
'=
'================================================================
Declare Function TrimNull(startstr As String) As String
Function TrimNull(startstr As String) As String
    TrimNull = Left$(startstr, Api_lstrlenW(StrAdr(startstr)))
End Function

'================================================================
'=
'================================================================
Declare Function GetVolumeFromDrive(sVolumeMountPoint As String) As String
Function GetVolumeFromDrive(sVolumeMountPoint As String) As String
    Var buff As String
    Var cbbuff As Long
    
    buff = Space$(1024)
    cbbuff = Len(buff)
    
    If Api_GetVolumeNameForVolumeMountPoint(sVolumeMountPoint, buff, cbbuff) <> 0 Then
        GetVolumeFromDrive = TrimNull(buff)
    End If
End Function

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Var Drive As String
    Var i As Integer

    Drives = Api_GetLogicalDrives()
    If Drives = 0 Then Exit Sub

    For i = 0 To 25
        If (Drives And 1) = 1 Then
            Drive = Chr$(65 + i)
            Drive = Drive & ":\"
            Combo1.AddString Drive
        End If
        Drives = Drives \ 2
    Next i
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var buff As String
    Var hVolume As Long
    Var cbbuff As Long
    Var sVolumeMountPoint As String
    Var Ret As Long

    buff = Space$(4096)
    cbbuff = Len(buff)

    hVolume = Api_FindFirstVolume(buff, cbbuff)

    List1.ResetContent

    Do
        sVolumeMountPoint = TrimNull(buff)
        List1.AddString sVolumeMountPoint
        buff = Space$(4096)
        cbbuff = Len(buff)
    Loop While Api_FindNextVolume(hVolume, buff, cbbuff)

    List1.AddString ""
    
    Ret = Api_FindVolumeClose(hVolume)
End Sub

'================================================================
'=
'================================================================
Declare Sub Button2_on edecl ()
Sub Button2_on()
    List1.ResetContent
    List1.AddString GetVolumeFromDrive(Left$(Combo1.GetText(Combo1.GetCursel), 3))
End Sub

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