Hard disk serial number extraction error.

Hello I am trying to extract hard disk serial number using folowing code:

Form1.Caption = GetObject("WinMgmts::Win32_LogicalDisk='C:'").VolumeSerialNumber

On my computer this works just fine, but when I run this code on other computer it gives me error message:

Run-time error '-2147217375 (80041021)' :
Automation error

I tried to copy msvbvm60.dll on other computer but still nothing, what should I do?

Thanks.
damirselAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hesCommented:
Create a setup package using the Package and Deployment Wizard . This will contain all of the dependentcies of the program. There are more than just the vb6 runtime dll.
0
damirselAuthor Commented:

I tried and still nothing, again gives me the same message.
0
Yrag1Commented:
Try this

Private 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 Form_Load()
    Dim Serial As Long, VName As String, FSName As String
    'Create buffers
    VName = String$(255, Chr$(0))
    FSName = String$(255, Chr$(0))
    'Get the volume information
    GetVolumeInformation "C:\", VName, 255, Serial, 0, 0, FSName, 255
    'Strip the extra chr$(0)'s
    VName = Left$(VName, InStr(1, VName, Chr$(0)) - 1)
    FSName = Left$(FSName, InStr(1, FSName, Chr$(0)) - 1)
    MsgBox "The Volume name of C:\ is '" + VName + "', the File system name of C:\ is '" + FSName + "' and the serial number of C:\ is '" + Trim(Str$(Serial)) + "'", vbInformation + vbOKOnly, App.Title
End Sub

This should work just fine

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

damirselAuthor Commented:
Yes it works fine but this serial number will change after formating the drive, the code I am heaving problems with, returns the unique serial number of HDD.
- thanks anyway.
0
David LeeCommented:
If you want the actual serial number of the physical disk instead of the system created serial number, then try this.  The physical serial number should never change.

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PhysicalMedia",,48)
For Each objItem in colItems
    Wscript.Echo "SerialNumber: " & objItem.SerialNumber
Next
0
Yrag1Commented:
Well done BlueDevilFan it works on my pc now i could not get it working using microsoft examples from msdn.
The points should be yours!!!
0
David LeeCommented:
Thanks, Yrag1!
0
JR2003Commented:
I got the same runtime automation error you get on my XP machine.
The code below works on my machine though:

Private Sub Command1_Click()
    Set colDisks = GetObject("Winmgmts:").ExecQuery("Select * from Win32_LogicalDisk where DeviceID='C:'")
    For Each objDisk In colDisks
        Form1.Caption = objDisk.VolumeSerialNumber
    Next
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ArkCommented:
Hi

Check out my sample on getting low-level HDD information (including VendorID) without WMI at http://www.freevbcode.com/ShowCode.Asp?ID=3380
or, simplified version with VendorID only at
http://vbnet.mvps.org/code/disk/smartide.htm
0
damirselAuthor Commented:
OK BlueDevilFan I am heaving problems to run this code on my computer, if you could give me some detailed instruction, and is it possible to run it without For Each part of code?
0
damirselAuthor Commented:
JR2003 your code works just fine, but I need to wait for BlueDevilFans answer.
0
David LeeCommented:
damirsel,

Please describe what problems you're having.   The only problem I see is that I gave you the code as VBScript when I should have given it to you as straight VB.  Sorry about that.  I've corrected that mistake below and changed the code into a function.  Yes, it's possible to eliminate the For Each ... Next loop, but doing so complicates the code a lot.  You have to get the name of the computer and construct an object path.  It's far easier to leave the loop in.  And since I've modified the code to pass it the number of the physical drive you want the serial number of the code will never go through the loop more than once.

Usage: GetDriveSerialNumber(intDriveNumber)
           Where intDriveNumber is the number of the physical drive whose serial number you want.
           The first drive in the system is 0.  This should always be the C drive.

Example: strSerialNumber = GetDriveSerialNumber(0)
              This would return the serial number of the C drive.


Private Function GetDriveSerialNumber(intDriveNumber As Integer) As String
    Dim objWMIService As Object, _
        colItems As Object, _
        objItem As Object, _
        strDrive As String
    strDrive = "\\\\.\\PHYSICALDRIVE" & intDriveNumber
    On Error Resume Next
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_PhysicalMedia Where Tag = '" & strDrive & "'", , 16)
    'Set objItem = colItems.Item(strKey)
    For Each objItem In colItems
        GetDriveSerialNumber = Trim(objItem.SerialNumber)
    Next
    Set objItem = Nothing
    Set colItems = Nothing
    Set objWMIService = Nothing
End Function

Please remember that there's a difference between the physical serial number of a drive and the serial number of a logical drive.  The code I gave and the code Ark gave a link to return the physical drive serial number.  This number should never change.  The code JR2003 gave returns the serial number of the logical disk, which I believe can change if the drive is erased and repartitioned.
0
damirselAuthor Commented:
Ok now again I am heaving problems. This is the code (I have inserted your function also):

Private Sub Form_Load()
Form1.Caption = GetDriveSerialNumber(0)
End Sub

And I tried with many other numbers (1) (2) ... and the Form1.Caption is always empty string.
0
David LeeCommented:
What version of Windows are you using?  Have you tried stepping through the code using the debugger?  I'd be curious to see if the various objects are being instantiated.  I tried the code on four different machines before posting.  On a Windows 2000 Server I got no serial numbers.  All the other machines returned them as expected.  I haven't had time to figure out why but my theory is that the server's drive array is responsible.
0
hesCommented:
BDF,
I am sure that is because they are SCSI drives. On a machine with IDE & SCSI (no array) it returned the SN for the IDE drives but blank for the SCSI drives.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.