471,594 Members | 2,062 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

programmatically obtain the hard disk's serial number from VBA?


Discovered this interesting comment on MSDN:

"To programmatically obtain the hard disk's serial number that the
manufacturer assigns, use the Windows Management Instrumentation (WMI)
Win32_PhysicalMedia (a class) property SerialNumber."

I'm sorry to admit it bit I am really undereducated on how to
incorporate some of the Windows SDK stuff into VBA apps. Anyone know
of some sample code that will allow us to read the C drive hardware
serial number? I'm NOT talking about the serial number assigned to a
drive by the OS, I'm talking about the serial number given by the HD
manufacturer and encoded on the drive's ROM chip.

The WHY of this question is too complicated to go into right now.

Any help or advice will be much appreciated.

Thanks folks,

-- LW
Mar 7 '06 #1
14 27589
Lauren Wilson wrote:
Discovered this interesting comment on MSDN:

"To programmatically obtain the hard disk's serial number that the
manufacturer assigns, use the Windows Management Instrumentation (WMI)
Win32_PhysicalMedia (a class) property SerialNumber."

I'm sorry to admit it bit I am really undereducated on how to
incorporate some of the Windows SDK stuff into VBA apps. Anyone know
of some sample code that will allow us to read the C drive hardware
serial number? I'm NOT talking about the serial number assigned to a
drive by the OS, I'm talking about the serial number given by the HD
manufacturer and encoded on the drive's ROM chip.

The WHY of this question is too complicated to go into right now.

Any help or advice will be much appreciated.

Thanks folks,

-- LW


Try:

http://groups.google.com/group/micro...bb45fdfed9d31f

Note: I know that there are ways for customers to get around the use of
the MAC Address of a network card for registering software. There may
be ways to get around the HD info as well.

James A. Fortune
CD********@FortuneJames.com

Mar 7 '06 #2

Thanks. Unfortunately, the "GetVolumeSerialNumber" function returns
the OS generated drive id, not the manufacturers's product serial
number that is stored on the drive's on-board ROM.

The OS drive ID WILL change if the user re-format's his HD. The Mfg
Drive serial number will not. What I need is VBA compatible code
that will make use of the Win32_PhysicalMedia.SerialNumber funtion to
read and return the HARDWARE serial number of a selected drive.
Of course, the user COULD remove and replace any drive, but that
happens FAR less often than a re-format.
On 6 Mar 2006 18:30:21 -0800, CD********@FortuneJames.com wrote:
Lauren Wilson wrote:
Discovered this interesting comment on MSDN:

"To programmatically obtain the hard disk's serial number that the
manufacturer assigns, use the Windows Management Instrumentation (WMI)
Win32_PhysicalMedia (a class) property SerialNumber."

I'm sorry to admit it bit I am really undereducated on how to
incorporate some of the Windows SDK stuff into VBA apps. Anyone know
of some sample code that will allow us to read the C drive hardware
serial number? I'm NOT talking about the serial number assigned to a
drive by the OS, I'm talking about the serial number given by the HD
manufacturer and encoded on the drive's ROM chip.

The WHY of this question is too complicated to go into right now.

Any help or advice will be much appreciated.

Thanks folks,

-- LW


Try:

http://groups.google.com/group/micro...bb45fdfed9d31f

Note: I know that there are ways for customers to get around the use of
the MAC Address of a network card for registering software. There may
be ways to get around the HD info as well.

James A. Fortune
CD********@FortuneJames.com

Mar 7 '06 #3
Air Code: (be careful of news client created line breaks)

Expand|Select|Wrap|Line Numbers
  1. Function DiskZeroName() As String
  2. Dim Buffer As String
  3. Dim PathTxt As String
  4. Dim FileNumber As Integer
  5. On Error Resume Next
  6. Kill "DiskZeroName*.*"
  7. On Error GoTo 0
  8. PathTxt = "DiskZeroName" & Format(Now(), "dddmmmddyyyyhhnnss\.\t\x\t")
  9. Shell "RegEdit /E " & PathTxt & "
  10. HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\Scsi\", vbHide
  11. FileNumber = FreeFile
  12. Open PathTxt For Binary As #FileNumber
  13. Buffer = String(LOF(FileNumber), vbNullChar)
  14. Get #FileNumber, , Buffer
  15. Close #FileNumber
  16. Buffer = StrConv(Buffer, vbFromUnicode)
  17. DiskZeroName = Mid$(Buffer, InStr(Buffer, "Identifier") + 13)
  18. DiskZeroName = Left$(DiskZeroName, InStr(DiskZeroName, Chr(34)) - 1)
  19. End Function
On my laptop this returns
HITACHI_DK23FA-80.

I don't know if this is the number you want.
I don't know if it will work on other machines or other operating
systems.
Mar 7 '06 #4
Lauren Wilson wrote:
Thanks. Unfortunately, the "GetVolumeSerialNumber" function returns
the OS generated drive id, not the manufacturers's product serial
number that is stored on the drive's on-board ROM.

The OS drive ID WILL change if the user re-format's his HD. The Mfg
Drive serial number will not. What I need is VBA compatible code
that will make use of the Win32_PhysicalMedia.SerialNumber funtion to
read and return the HARDWARE serial number of a selected drive.
Of course, the user COULD remove and replace any drive, but that
happens FAR less often than a re-format.


Lauren,

Sorry I wasn't able to help. If you find a solution to this problem
please post it. I am also interested in other's comments about the
usage of this manufacturer HD ID as a "dongle."

Thanks,

James A. Fortune
CD********@FortuneJames.com

Mar 7 '06 #5
On Mon, 06 Mar 2006 19:05:12 -0600, Lauren Wilson <no****@nospam.com> wrote:

Discovered this interesting comment on MSDN:

"To programmatically obtain the hard disk's serial number that the
manufacturer assigns, use the Windows Management Instrumentation (WMI)
Win32_PhysicalMedia (a class) property SerialNumber."

I'm sorry to admit it bit I am really undereducated on how to
incorporate some of the Windows SDK stuff into VBA apps. Anyone know
of some sample code that will allow us to read the C drive hardware
serial number? I'm NOT talking about the serial number assigned to a
drive by the OS, I'm talking about the serial number given by the HD
manufacturer and encoded on the drive's ROM chip.

The WHY of this question is too complicated to go into right now.

Any help or advice will be much appreciated.

Thanks folks,

-- LW


Some of the earlier hacks no longer work and WMI is the way to use on current systems.

Easy in script if this is enabled and can be run in admin mode, for example create a file sn.vbs:
(Scott McNairy)

Expand|Select|Wrap|Line Numbers
  1. set svc = getobject ("winmgmts:root\cimv2")
  2. set objEnum = svc.execQuery ("select * from win32_physicalMedia")
  3. for each obj in objEnum
  4. wscript.echo obj.GetObjectText_
  5. next
and run this run in command mode (you can shell out to this) for example

cscript sn.vbs >result.txt

For VB code you will have to read the documentation yourself probably, or be more successful in
searching. WMI will work in VB but all the examples are in C++.

above script ref
http://groups.google.com.au/group/mi...2363a8ea2debab

general refs
http://msdn.microsoft.com/library/de...sicalmedia.asp
http://msdn.microsoft.com/library/de...r_hardware.asp
and go up and down the tree.

Oh, I see that there are some WMI routines for disk info in Randy Birch's site VBnet (note this is
VB, not VB.net) but on cursory inspection these also seem to use scripting.
Mar 8 '06 #6
Thanks p. person. This is a cool way to retrieve what the OS THINKS
is the actual Mfg SN for drive 0. Since the value is retrieved from
the Registry, then, by definition, it can be wiped out by a
reformat/reinstall.

Please, someone correct me if I'm wrong: The "Win32_PhysicalMedia"
class of the WMI library, actually reads and returns the drive
manufacturer's SN FROM the hardware ROM on a specified drive device.
This would be the serial number/identifier that was burned onto the
ROM when the drive was made. If so, THAT is what we need. Anything
less, is no better than what we have now -- and what we have now is
not adequate.

Can anyone point me in the right direction on this?

Thanks again for your help.

-- LW
On Wed, 08 Mar 2006 13:35:07 GMT, polite person <si*****@ease.com>
wrote:
On Mon, 06 Mar 2006 19:05:12 -0600, Lauren Wilson <no****@nospam.com> wrote:

Discovered this interesting comment on MSDN:

"To programmatically obtain the hard disk's serial number that the
manufacturer assigns, use the Windows Management Instrumentation (WMI)
Win32_PhysicalMedia (a class) property SerialNumber."

I'm sorry to admit it bit I am really undereducated on how to
incorporate some of the Windows SDK stuff into VBA apps. Anyone know
of some sample code that will allow us to read the C drive hardware
serial number? I'm NOT talking about the serial number assigned to a
drive by the OS, I'm talking about the serial number given by the HD
manufacturer and encoded on the drive's ROM chip.

The WHY of this question is too complicated to go into right now.

Any help or advice will be much appreciated.

Thanks folks,

-- LW


Some of the earlier hacks no longer work and WMI is the way to use on current systems.

Easy in script if this is enabled and can be run in admin mode, for example create a file sn.vbs:
(Scott McNairy)

Expand|Select|Wrap|Line Numbers
  1. set svc = getobject ("winmgmts:root\cimv2")
  2. set objEnum = svc.execQuery ("select * from win32_physicalMedia")
  3. for each obj in objEnum
  4.     wscript.echo obj.GetObjectText_
  5. next
  6.  

and run this run in command mode (you can shell out to this) for example

cscript sn.vbs >result.txt

For VB code you will have to read the documentation yourself probably, or be more successful in
searching. WMI will work in VB but all the examples are in C++.

above script ref
http://groups.google.com.au/group/mi...2363a8ea2debab

general refs
http://msdn.microsoft.com/library/de...sicalmedia.asp
http://msdn.microsoft.com/library/de...r_hardware.asp
and go up and down the tree.

Oh, I see that there are some WMI routines for disk info in Randy Birch's site VBnet (note this is
VB, not VB.net) but on cursory inspection these also seem to use scripting.

Mar 8 '06 #7
On Wed, 08 Mar 2006 13:25:32 -0600, Lauren Wilson <no****@nospam.com> wrote:

Thanks p. person. This is a cool way to retrieve what the OS THINKS
is the actual Mfg SN for drive 0. Since the value is retrieved from
the Registry, then, by definition, it can be wiped out by a
reformat/reinstall.

Please, someone correct me if I'm wrong: The "Win32_PhysicalMedia"
class of the WMI library, actually reads and returns the drive
manufacturer's SN FROM the hardware ROM on a specified drive device.
This would be the serial number/identifier that was burned onto the
ROM when the drive was made. If so, THAT is what we need. Anything
less, is no better than what we have now -- and what we have now is
not adequate.

Can anyone point me in the right direction on this?

Thanks again for your help.

-- LW

I would post the question in microsoft.public.win32.programmer.wmi

I would have expected that all WMI objects are actually precalculated, to read the actual number you
would need to be in a driver or with similar privileges. However by analogy with the MAC address, if
it has been spoofed there is an entry which can tell you this or reset it I forget which.
Mar 9 '06 #8
In MSDOS it was easy to retrieve standard HD information,
but in Windows, hardware is virtualised, and you can't get
at it.
Windows does now provide a path to an interface for HD
serial numbers, but it is the SMART drive interface and
I don't know what you will get if SMART is turned off,
or if you have SCSI disks, or if you have Serial ATA disks,
etc.

http://vbnet.mvps.org/index.html?code/disk/smartide.htm
manufacturer assigns, use the WMI Win32_PhysicalMedia
property SerialNumber."
I've never seen that work, but go ahead and try it:
Expand|Select|Wrap|Line Numbers
  1. '----------
  2. Sub GetPhysicalSerial()
  3.  
  4. Dim obj As Object
  5. Dim WMI As Object
  6.  
  7. Set WMI = GetObject("WinMgmts:")
  8.  
  9. For Each obj In WMI.InstancesOf("Win32_PhysicalMedia")
  10. Debug.Print "SN: " & obj.SerialNumber
  11. Next
  12.  
  13. End Sub
  14. '-----------
  15.  
(david)

"Lauren Wilson" <no****@nospam.com> wrote in message
news:rm********************************@4ax.com...
Discovered this interesting comment on MSDN:

"To programmatically obtain the hard disk's serial number that the
manufacturer assigns, use the Windows Management Instrumentation (WMI)
Win32_PhysicalMedia (a class) property SerialNumber."

I'm sorry to admit it bit I am really undereducated on how to
incorporate some of the Windows SDK stuff into VBA apps. Anyone know
of some sample code that will allow us to read the C drive hardware
serial number? I'm NOT talking about the serial number assigned to a
drive by the OS, I'm talking about the serial number given by the HD
manufacturer and encoded on the drive's ROM chip.

The WHY of this question is too complicated to go into right now.

Any help or advice will be much appreciated.

Thanks folks,

-- LW
Mar 13 '06 #9
Hi David! Thanks for your response.

This works perfectly. However, it naturally returns SNs for ALL of
the HDs on my system. I need a way to return JUST the primary drive
SN (Drive 0). I tried inserting Debug.Print "SN: " & obj.name & ": "
& obj.SerialNumber, but the Name property returned only empty
strings.
Is there a way to limit the output to only the SN for Drive 0?

Thanks again.

-- LW.


On Mon, 13 Mar 2006 16:38:48 +1100, "david epsom dot com dot au"
<david@epsomdotcomdotau> wrote:
In MSDOS it was easy to retrieve standard HD information,
but in Windows, hardware is virtualised, and you can't get
at it.
Windows does now provide a path to an interface for HD
serial numbers, but it is the SMART drive interface and
I don't know what you will get if SMART is turned off,
or if you have SCSI disks, or if you have Serial ATA disks,
etc.

http://vbnet.mvps.org/index.html?code/disk/smartide.htm
manufacturer assigns, use the WMI Win32_PhysicalMedia
property SerialNumber."


I've never seen that work, but go ahead and try it:

'----------
Sub GetPhysicalSerial()

Dim obj As Object
Dim WMI As Object

Set WMI = GetObject("WinMgmts:")

For Each obj In WMI.InstancesOf("Win32_PhysicalMedia")
Debug.Print "SN: " & obj.SerialNumber
Next

End Sub
'-----------

(david)

"Lauren Wilson" <no****@nospam.com> wrote in message
news:rm********************************@4ax.com.. .

Discovered this interesting comment on MSDN:

"To programmatically obtain the hard disk's serial number that the
manufacturer assigns, use the Windows Management Instrumentation (WMI)
Win32_PhysicalMedia (a class) property SerialNumber."

I'm sorry to admit it bit I am really undereducated on how to
incorporate some of the Windows SDK stuff into VBA apps. Anyone know
of some sample code that will allow us to read the C drive hardware
serial number? I'm NOT talking about the serial number assigned to a
drive by the OS, I'm talking about the serial number given by the HD
manufacturer and encoded on the drive's ROM chip.

The WHY of this question is too complicated to go into right now.

Any help or advice will be much appreciated.

Thanks folks,

-- LW

Mar 13 '06 #10

Never mind. It appears that Drive 0 is always listed first.

On Mon, 13 Mar 2006 13:34:03 -0600, Lauren Wilson <no****@nospam.com>
wrote:
Hi David! Thanks for your response.

This works perfectly. However, it naturally returns SNs for ALL of
the HDs on my system. I need a way to return JUST the primary drive
SN (Drive 0). I tried inserting Debug.Print "SN: " & obj.name & ": "
& obj.SerialNumber, but the Name property returned only empty
strings.
Is there a way to limit the output to only the SN for Drive 0?

Thanks again.

-- LW.


On Mon, 13 Mar 2006 16:38:48 +1100, "david epsom dot com dot au"
<david@epsomdotcomdotau> wrote:
In MSDOS it was easy to retrieve standard HD information,
but in Windows, hardware is virtualised, and you can't get
at it.
Windows does now provide a path to an interface for HD
serial numbers, but it is the SMART drive interface and
I don't know what you will get if SMART is turned off,
or if you have SCSI disks, or if you have Serial ATA disks,
etc.

http://vbnet.mvps.org/index.html?code/disk/smartide.htm
manufacturer assigns, use the WMI Win32_PhysicalMedia
property SerialNumber."


I've never seen that work, but go ahead and try it:

'----------
Sub GetPhysicalSerial()

Dim obj As Object
Dim WMI As Object

Set WMI = GetObject("WinMgmts:")

For Each obj In WMI.InstancesOf("Win32_PhysicalMedia")
Debug.Print "SN: " & obj.SerialNumber
Next

End Sub
'-----------

(david)

"Lauren Wilson" <no****@nospam.com> wrote in message
news:rm********************************@4ax.com. ..

Discovered this interesting comment on MSDN:

"To programmatically obtain the hard disk's serial number that the
manufacturer assigns, use the Windows Management Instrumentation (WMI)
Win32_PhysicalMedia (a class) property SerialNumber."

I'm sorry to admit it bit I am really undereducated on how to
incorporate some of the Windows SDK stuff into VBA apps. Anyone know
of some sample code that will allow us to read the C drive hardware
serial number? I'm NOT talking about the serial number assigned to a
drive by the OS, I'm talking about the serial number given by the HD
manufacturer and encoded on the drive's ROM chip.

The WHY of this question is too complicated to go into right now.

Any help or advice will be much appreciated.

Thanks folks,

-- LW

Mar 14 '06 #11
On Mon, 13 Mar 2006 13:34:03 -0600, Lauren Wilson <no****@nospam.com> wrote:
Hi David! Thanks for your response.

This works perfectly. <snip>


Hi
I'm sure all the WMI methods will give the same result but in answer to your worry about whether the
result is really the serial number or whether it is just what the OS thinks it is .. see

http://msdn.microsoft.com/library/de...ce_drivers.asp
Accessing Device Drivers

http://msdn.microsoft.com/library/de...m_provider.asp
WDM Provider

or http://www.expresscomputeronline.com...chspace1.shtml
for an outside overview of how WMI works.

Draw your own conclusions! Maybe you should read the refs in reverse order.
Mar 14 '06 #12
Thanks a lot PP! The last article was VERY helpful.

We have now tested my WMI code on several Windows PCs, all XP or 2000
machines. In every case, the drive ID's returned exactly matched the
drive serial number originally provided by the drive mfg! I don't
know if this will work on EVERY PC. So far, it looks good.

The bottom line for ALL of this is that there is a growing need to
identify a particular PC with absolute certainty. This approach may
not be the final method, but it seems to move in that direction.
On Tue, 14 Mar 2006 06:36:00 GMT, polite person <si*****@ease.com>
wrote:
On Mon, 13 Mar 2006 13:34:03 -0600, Lauren Wilson <no****@nospam.com> wrote:
Hi David! Thanks for your response.

This works perfectly. <snip>


Hi
I'm sure all the WMI methods will give the same result but in answer to your worry about whether the
result is really the serial number or whether it is just what the OS thinks it is .. see

http://msdn.microsoft.com/library/de...ce_drivers.asp
Accessing Device Drivers

http://msdn.microsoft.com/library/de...m_provider.asp
WDM Provider

or http://www.expresscomputeronline.com...chspace1.shtml
for an outside overview of how WMI works.

Draw your own conclusions! Maybe you should read the refs in reverse order.

Mar 14 '06 #13
> The bottom line for ALL of this is that there is a growing need
identify a particular PC with absolute certainty. This approach
It doesn't identify my PC :~) I get a blank for the serial
number using that method.

(david)

"Lauren Wilson" <no****@nospam.com> wrote in message
news:k4********************************@4ax.com... Thanks a lot PP! The last article was VERY helpful.

We have now tested my WMI code on several Windows PCs, all XP or 2000
machines. In every case, the drive ID's returned exactly matched the
drive serial number originally provided by the drive mfg! I don't
know if this will work on EVERY PC. So far, it looks good.

The bottom line for ALL of this is that there is a growing need to
identify a particular PC with absolute certainty. This approach may
not be the final method, but it seems to move in that direction.
On Tue, 14 Mar 2006 06:36:00 GMT, polite person <si*****@ease.com>
wrote:
On Mon, 13 Mar 2006 13:34:03 -0600, Lauren Wilson <no****@nospam.com>
wrote:
Hi David! Thanks for your response.

This works perfectly. <snip>


Hi
I'm sure all the WMI methods will give the same result but in answer to
your worry about whether the
result is really the serial number or whether it is just what the OS
thinks it is .. see

http://msdn.microsoft.com/library/de...ce_drivers.asp
Accessing Device Drivers

http://msdn.microsoft.com/library/de...m_provider.asp
WDM Provider

or http://www.expresscomputeronline.com...chspace1.shtml
for an outside overview of how WMI works.

Draw your own conclusions! Maybe you should read the refs in reverse
order.

Mar 14 '06 #14
On Wed, 15 Mar 2006 09:53:34 +1100, "david epsom dot com dot au"
<david@epsomdotcomdotau> wrote:
The bottom line for ALL of this is that there is a growing need
identify a particular PC with absolute certainty. This approach
It doesn't identify my PC :~) I get a blank for the serial
number using that method.


Interesting. Mind saying what is your OS, and primary HD brand name?

(david)

"Lauren Wilson" <no****@nospam.com> wrote in message
news:k4********************************@4ax.com.. .
Thanks a lot PP! The last article was VERY helpful.

We have now tested my WMI code on several Windows PCs, all XP or 2000
machines. In every case, the drive ID's returned exactly matched the
drive serial number originally provided by the drive mfg! I don't
know if this will work on EVERY PC. So far, it looks good.

The bottom line for ALL of this is that there is a growing need to
identify a particular PC with absolute certainty. This approach may
not be the final method, but it seems to move in that direction.
On Tue, 14 Mar 2006 06:36:00 GMT, polite person <si*****@ease.com>
wrote:
On Mon, 13 Mar 2006 13:34:03 -0600, Lauren Wilson <no****@nospam.com>
wrote:

Hi David! Thanks for your response.

This works perfectly. <snip>

Hi
I'm sure all the WMI methods will give the same result but in answer to
your worry about whether the
result is really the serial number or whether it is just what the OS
thinks it is .. see

http://msdn.microsoft.com/library/de...ce_drivers.asp
Accessing Device Drivers

http://msdn.microsoft.com/library/de...m_provider.asp
WDM Provider

or http://www.expresscomputeronline.com...chspace1.shtml
for an outside overview of how WMI works.

Draw your own conclusions! Maybe you should read the refs in reverse
order.

Mar 15 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by tom | last post: by
7 posts views Thread by Adele le Roux | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.