Link to home
Start Free TrialLog in
Avatar of gcnagaraj
gcnagaraj

asked on

Hard Disk Serial Number

Hi
I am developing a software in VB for which i need the serial number of the hard disk. I searched thru the net and i was getting the programs which tell me the Volume Number of the drive (ie c: , d: etc). According to my information, this volume number changes whenever that volume is formatted. Can anybody help in getting the Hard Disk serial number which is manufacturer defined and which will not change
thx in advance
nagaraj
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Private Declare Function GetVolumeSerialNumber Lib "kernel32.dll" 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

Public Function VolumeSerialNumber(ByVal RootPath As String) As String
    Dim VolLabel As String
    Dim VolSize As Long
    Dim Serial As Long
    Dim MaxLen As Long
    Dim Flags As Long
    Dim Name As String
    Dim NameSize As Long
    Dim s As String

    If GetVolumeSerialNumber(RootPath, VolLabel, VolSize, Serial, MaxLen, Flags, Name, NameSize) Then
        'Create an 8 character string
        s = Format(Hex(Serial), "00000000")
        'Adds the '-' between the first 4 characters and the last 4 characters
        VolumeSerialNumber = Left(s, 4) + "-" + Right(s, 4)
    Else
        'If the call to API function fails the function returns a zero serial number
        VolumeSerialNumber = "0000-0000"
    End If
End Function

Private Sub Command1_Click()
    MsgBox VolumeSerialNumber("C:\") 'Shows the serial number of your Hard Disk
End Sub
Avatar of fl0yd
fl0yd

That's correct, nagaraj -- the serial number returned by GetVolumeInformation is OS supplied and will change when that drive is partitioned. What you are looking for is a way to retrieve that data from the driver. Although I can't help you with the VB part here is the API call you should check out:

DeviceIoControl( ..., IOCTL_CHANGER_GET_PRODUCT_DATA, ... ) -- not sure about the IOCTL_CHANGER_GET_PRODUCT_DATA. The DeviceIoControl still gives you low-level access to devices so that should get you going.

.f
Are these api's not different then ?
 GetVolumeInformation  or  GetVolumeSerialNumber  !!!
Dhaest,

nope -- they both return the OS provided volume serial number that gets changed when the drive is partitioned. Those API calls DO NOT return the hardware serial number of the drive.

a_pravarakhya provided the link to source code illustrating the use of DeviceIoControl() to retrieve the hardware serial number that stays constant throughout the life of the drive, no matter how many times it is partitioned, how many OS's ran on it or anything else.

.f
And the code of the second link ? Does it provide the correct serialnumber or not ?

Sub ShowDriveInfo(drvpath)
    Dim fs, d, s, t
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set d = fs.GetDrive(fs.GetDriveName(fs.GetAbsolutePathName(drvpath)))
    Select Case d.DriveType
        Case 0: t = "Unknown"
        Case 1: t = "Removable"
        Case 2: t = "Fixed"
        Case 3: t = "Network"
        Case 4: t = "CD-ROM"
        Case 5: t = "RAM Disk"
    End Select
    s = "Drive " & d.DriveLetter & ": - " & t
    s = s & vbCrLf & "SN: " & d.SerialNumber
    MsgBox s
End Sub
Nope, it doesn't -- it's also the OS provided one, subject to change on repartitioning.

.f
Take the serial number of the computer it came out of or is in and go to the manufacturer and then the manufacturer will track it back to that subcomponent.

RIVER FREIGHT
ASKER CERTIFIED SOLUTION
Avatar of mquiroz
mquiroz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi All,
I am also looking for the exactly same solution. I came to this link via searching on net for the problem solution.
But, the URL giving as a solution of this problem is not working as it is quite long time after getting this solution.
So, any one of you, please give me that information/solution?

Awaiting for your valuable help.

thanks and regards,

Kruti.