如何使用VB 6读取硬盘卷序列号?

Rob*_*ung 2 vb6 ntfs serial-number fat partition

如何使用VB 6读取硬盘卷序列号但不使用任何ActiveX控件或第三方附加组件?

C-P*_*uru 5

Private Declare Function GetVolumeInformation _
    Lib "kernel32" Alias "GetVolumeInformationA" _
    (ByVal lpRootPathName As String, _
    ByVal pVolumeNameBuffer As String, _
    ByVal nVolumeNameSize As Long, _
    lpVolumeSerialNumber As Long, _
    lpMaximumComponentLength As Long, _
    lpFileSystemFlags As Long, _
    ByVal lpFileSystemNameBuffer As String, _
    ByVal nFileSystemNameSize As Long) As Long

Public Function GetSerialNumber( _
    ByVal sDrive As String) As Long

    If Len(sDrive) Then
        If InStr(sDrive, "\\") = 1 Then
            ' Make sure we end in backslash for UNC
            If Right$(sDrive, 1) <> "\" Then
                sDrive = sDrive & "\"
            End If
        Else
            ' If not UNC, take first letter as drive
            sDrive = Left$(sDrive, 1) & ":\"
        End If
    Else
        ' Else just use current drive
        sDrive = vbNullString
    End If

    ' Grab S/N -- Most params can be NULL
    Call GetVolumeInformation( _
        sDrive, vbNullString, 0, GetSerialNumber, _
        ByVal 0&, ByVal 0&, vbNullString, 0)
End Function
Run Code Online (Sandbox Code Playgroud)

致电:

Dim Drive As String
Drive = InputBox("Enter drive for checking SN")
MsgBox Hex$(GetSerialNumber(Drive))
Run Code Online (Sandbox Code Playgroud)

资料来源:http://www.devx.com/tips/Tip/15908