Solved

Hard disk serial number extraction error.

Posted on 2004-10-31
1,632 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
Question by:damirsel
    15 Comments
     
    LVL 24

    Expert Comment

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

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

    Expert Comment

    by:Yrag1
    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
     

    Author Comment

    by:damirsel
    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
    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
    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
    Thanks, Yrag1!
    0
     
    LVL 18

    Accepted Solution

    by:
    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 27

    Expert Comment

    by:Ark
    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
    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
    JR2003 your code works just fine, but I need to wait for BlueDevilFans answer.
    0
     
    LVL 76

    Assisted Solution

    by:David Lee
    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
    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
    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 24

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

     Java Android Coding Bundle

    Whether you're an Apple user or Android addict, learning to code for the Android platform is an extremely valuable, in-demand skill. It all starts with Java, the language behind the apps and games that make Android the top platform it is today.

    Suggested Solutions

    Title # Comments Views Activity
    XML template problem 4 50
    how to open Waze.com/livemap from address saved in DB? 26 71
    DIR issue 7 32
    VBA color chart bars 12 38
    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    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…
    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…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now