Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Hard Drive Serial Number

Posted on 1998-04-25
2
Medium Priority
?
452 Views
Last Modified: 2011-10-03
How do you go about getting the hard drive serial number in Visual Basic.
0
Comment
Question by:majestic
2 Comments
 
LVL 5

Accepted Solution

by:
bin_huwairib earned 400 total points
ID: 1428982
majestic,

=== 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

0
 
LVL 9

Expert Comment

by:Dalin
ID: 1428983
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question