By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

Get hard drive serial without API using VBA

P: 200
Has anyone used this code to get the serial for a hard drive?

Expand|Select|Wrap|Line Numbers
  1. Function HDSerialNumber() As String
  2. Dim fsObj As Object
  3. Dim drv As Object
  4.     Set fsObj = CreateObject("Scripting.FileSystemObject")
  5.     Set drv = fsObj.Drives("C")
  6.     HDSerialNumber = Left(Hex(drv.SerialNumber), 4) _
  7.                      & "-" & Right(Hex(drv.SerialNumber), 4)
  8.     Debug.Print HDSerialNumber
  10. End Function
Is this a reliable way to do it or is there a better way?
Dec 12 '11 #1
Share this Question
Share on Google+
3 Replies

P: 759
The function seems to return all the time the same value for the same drive letter.
But, as far as I don't know how to check the drive serial using other method I can't say if your code is working.

The Drives() function do not accept a variable as argument as I expect.

This are values that I obtain running the function:

For "C" : 84E8-BE57
For "D" : F8CB-609F

For CD drives it seems to depend of CD (or DVD) but is all the time the same for a certain CD (DVD). I have two CD (DVD) drives attached to my PC and I make tests for both of them with the same results: same CD (or DVD) => same "serial".

Hope this information help you.
Any way I post here for subscribe purpose.
Dec 12 '11 #2

Expert 5K+
P: 8,638
Here is another approach using the API:
  1. API Declaration:
    Expand|Select|Wrap|Line Numbers
    1. Public Declare Function GetVolumeInformation Lib "Kernel32" Alias "GetVolumeInformationA" _
    2.                          (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, _
    3.                          ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, _
    4.                          lpMaximumComponentLength As Long, lpFileSystemFlags As Long, _
    5.                          ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long
  2. Call to Function:
    Expand|Select|Wrap|Line Numbers
    1. Dim strSerialNum As Long
    2. Dim strVName As String
    3. Dim strFSName As String
    5. 'Get the Volume information
    6. GetVolumeInformation "C:\", strVName, 255, strSerialNum, 0, 0, strFSName, 255
    8. Debug.Print "The Serial Number of Drive C:\ is: " & Trim(Str$(strSerialNum))
  3. OUTPUT:
    Expand|Select|Wrap|Line Numbers
    1. 'With Drive Substitution:
    2. The Serial Number of Drive C:\ is: 1755256066
    3. The Serial Number of Drive F:\ is: 861870070
Dec 12 '11 #3

P: 200
Thanks for your input ADezii. I am not sure what number I am presented with when I run your code. With Hardware ID Extractor I find that the code I posted gives the partition ID (volume serial number) which unfortunately will change if the drive is reformatted.

Is there a way to verify that the code you provided gives the real serial of the hard drive?

DiskId32 from www(dot)winsim(dot)com/diskid32/diskid32(dot)html gives different information than both of the solutions posted here.

Now I'm confused.
Dec 13 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.