デスクドライブのシリアル番号取得(U)          <TOP>


指定したドライブのシリアル番号を取得します。

GetLogicalDrives 利用可能ディスクドライブ取得

GetVolumeSerialNumber ドライブのシリアル番号を取得

 

 

※一見固有の値のようですが、メーカーパソコンで同一機種のボリュームシリアルが、全て同一ということもあるそうです。

MACアドレスとハードディスクボリュームシリアル番号の組み合わせが、絶対ではありませんが確率的には利用可能かも・・

 

'================================================================
'= デスクドライブのシリアル番号取得(U)
'=    (GetVolumeSerialNumber.bas)
'================================================================
#include "Windows.bi"

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

' ドライブのシリアル番号を取得
Declare Function Api_GetVolumeSerialNumber& Lib "kernel32" Alias "GetVolumeInformationA" (ByVal PathName$, ByVal VolNameBuff&, ByVal VolNameSize&, VolSerialNum&, ByVal MaxComponentLen&, ByVal FileSysFlags&, ByVal FileSysNameBuff&, ByVal FileSysNameSize&)

#define MAX_PATH 260

Var Shared Text(3) As Object
Var Shared Combo1 As Object
Var Shared Button1 As Object

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

Var Shared Drive As String

'================================================================
'=
'================================================================
Declare Function VolumeSerial(DriveLetter As String) As Long
Function VolumeSerial(DriveLetter As String) As Long
    var Serial As Long
    var Ret As Long

    Ret = Api_GetVolumeSerialNumber(Ucase$(DriveLetter), 0, 0, Serial, 0, 0, 0, 0)
    VolumeSerial = Serial
End Function

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Drives = Api_GetLogicalDrives()      '利用可能なディスクドライブ取得
    If Drives = 0 Then Exit Sub          '関数の失敗

    For i = 0 To 25                      'A〜Zドライブを検索する
        If (Drives And 1) = 1 Then
            Drive = Chr$(65 + i)         'ドライブ名(A〜Z)に変換
            Drive = Drive & ":\"
            Combo1.AddString Drive
        End If
        Drives = Drives \ 2              'ドライブ検索
    Next i
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var VolLabel As String
    Var Serial As Long
    Var MaxLen As Long
    Var Flags As Long
    Var nName As String
    Var s As String
    Var Ret As Long

    Drive = Left$(Combo1.GetText(Combo1.GetCursel), 3)

    Text(0).SetWindowText "&&H" & Hex$(VolumeSerial(Drive))
    Text(1).SetWindowText Trim$(Str$(VolumeSerial(Drive)))
End Sub

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