Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Hard disk serial number extraction error.

Posted on 2004-10-31
15
Medium Priority
?
1,675 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:damirsel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +3
15 Comments
 
LVL 20

Expert Comment

by:hes
ID: 12456481
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
 

Author Comment

by:damirsel
ID: 12456561

I tried and still nothing, again gives me the same message.
0
 
LVL 1

Expert Comment

by:Yrag1
ID: 12456849
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:damirsel
ID: 12457305
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
 
LVL 76

Expert Comment

by:David Lee
ID: 12458581
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
 
LVL 1

Expert Comment

by:Yrag1
ID: 12458674
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
 
LVL 76

Expert Comment

by:David Lee
ID: 12459186
Thanks, Yrag1!
0
 
LVL 18

Accepted Solution

by:
JR2003 earned 120 total points
ID: 12459347
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
 
LVL 28

Expert Comment

by:Ark
ID: 12459482
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
 

Author Comment

by:damirsel
ID: 12461728
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
 

Author Comment

by:damirsel
ID: 12461766
JR2003 your code works just fine, but I need to wait for BlueDevilFans answer.
0
 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 80 total points
ID: 12464486
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
 

Author Comment

by:damirsel
ID: 12468295
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
 
LVL 76

Expert Comment

by:David Lee
ID: 12468569
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
 
LVL 20

Expert Comment

by:hes
ID: 12472000
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

636 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