Hard Drive Serial Number

How do you go about getting the hard drive serial number in Visual Basic.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

bin_huwairibConnect With a Mentor Commented:

=== Add the following code in module1 ===
Public 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)

Public Const MAX_FILENAME_LEN = 256

Public Function DriveSerial(ByVal sDrv As String) As Long
 Dim RetVal As Long
 Dim str As String * MAX_FILENAME_LEN
 Dim str2 As String * MAX_FILENAME_LEN
 Dim a As Long
 Dim b As Long

 Call GetVolumeInformation(sDrv & ":\", str, MAX_FILENAME_LEN, RetVal, _
 a, b, str2, MAX_FILENAME_LEN)

 DriveSerial = RetVal
End Function

=== Add this code in form1 ===
Private Sub Command1_Click()
 Msgbox "Drive C Serial Number is " & Hex(DriveSerial("C"))
End Sub

Best Regards
Bin Huwairib

Option ExplicitPrivate Declare Function GetVolumeInformation _
   Lib "kernel32" Alias "GetVolumeInformationA" _
   (ByVal lpRootPathName As String, _   ByVal lpVolumeNameBuffer 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   Private Sub cmdTest_Click()
   Dim iSerialNum As Long   Dim sVolumeLabel As String
   Dim sVolumeType As String   Dim iRetVal As Long   sVolumeLabel = Space(255)
   sVolumeType = Space(255)   iRetVal = GetVolumeInformation("f:\", _
      sVolumeLabel, Len(sVolumeLabel), _      iSerialNum, _      0, 0, _
      sVolumeType, Len(sVolumeType))  
   MsgBox "Disk Serial Number: " & iSerialNum & vbCrLf _
      & "Volume Type: " & Left$(sVolumeType, InStr(sVolumeType, Chr$(0)) - 1) & vbCrLf _
      & "Volume Label: " & Left$(sVolumeLabel, InStr(sVolumeLabel, Chr$(0)) - 1)
End Sub
All Courses

From novice to tech pro — start learning today.