Visual Basic Development Bookmark and Share   
 Home > Visual Basic General > how to extract a serial number of a hard disk in VB or C or C++
 

how to extract a serial number of a hard disk in VB or C or C++

Can anyone explain with program how to extract the serial number or unique ID of the hard disk. i need a clear explained program. i m using some tools but icouldn'treallyunderstandhow they do it. i request some to explain me with a program please :)
EnigmaVJ  Tuesday, November 24, 2009 7:02 AM

Thank you Allfor your friendly help.

HiEnigmaVJ,

Welcometo MSDN forums!

Code sample:How to get Hard Disk serial number in VB.NET?
Firstly you need to Add Reference to
System.Management.dll.

ImportsSystem.Management

PublicClassForm1

PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click

'GetHarddiskSerialNumber

DimsearcherAsManagementObjectSearcher=NewManagementObjectSearcher("SELECT*FROMWin32_PhysicalMedia")

ForEachwmi_HDAsManagementObjectInsearcher.Get()

MessageBox.Show(wmi_HD("SerialNumber"))

Next

EndSub

EndClass


Check this thread for reference:
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/f393708f-d7e3-4aa3-a624-7e8c6662f343/
How to get harddisk/motherboard serial number and CUP ID withVB.NET?

http://www.codeproject.com/KB/cs/hard_disk_serialno.aspx
How to Retrieve the REAL Hard Drive Serial Number in C#




Best regards,
Martin Xie


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Martin Xie - MSFT  Monday, November 30, 2009 9:19 AM
Hi,

Be aware that not every disk is giving the serialnumber.

The program to use is easy, it is simply WMI but if your disk is not giving that number then you will never get it.

Mostly is this used in combination with copy protection of programs, be aware that this is a way a big Software company became broken when the disks did not give any number anymore (it was not a number, they used the extra track which is normally not reachable).

To get the code, simply look with Google

Her a sample of the processor ID

http://www.vb-tips.com/CpuID.aspx

With the disk it goes the same
Success
Cor
Cor Ligthert  Tuesday, November 24, 2009 7:35 AM
You need to add a reference to .Net's System.Management component firstly. Then you can use it to find the information you're after:

        'MediaType 12 is for "fixed hard disk media".  Full list of that and other WMI / WQLable stuff here:
        'http://msdn.microsoft.com/en-us/library/aa394173(VS.85).aspx

        Dim query As New System.Management.WqlObjectQuery("select * from Win32_LogicalDisk where MediaType = 12")
        For Each holdObject As System.Management.ManagementObject In New System.Management.ManagementObjectSearcher(query).Get()
            Console.WriteLine(String.Format("Drive: {0} - Volume Serial ID: {1}", holdObject("DeviceID"), holdObject("VolumeSerialNumber")))
        Next

This is easily ported to C# if you'd rather have it as your high level language. It's all .Net
syntaxeater  Tuesday, November 24, 2009 7:37 AM
If you use FAT32 on WinME, then you can use this code:
Private Declare Function GetVolumeInformation Lib _
   "kernel32.dll" Alias "GetVolumeInformationA" _
   (ByVal lpRootPathName As String, _
   ByVal lpVolumeNameBuffer As String, _
   ByVal nVolumeNameSize As Integer, _
   lpVolumeSerialNumber As Long, _
   lpMaximumComponentLength As Long, _
   lpFileSystemFlags As Long, _
   ByVal lpFileSystemNameBuffer As String, _
   ByVal nFileSystemNameSize As Long) As Long

Public Function DriveSerialNumber(ByVal Drive As String) As Long
    
    'usage: SN = DriveSerialNumber("C:")
 
    Dim lAns As Long
    Dim lRet As Long
    Dim sVolumeName As String, sDriveType As String
    Dim sDrive As String

    'Deal with one and two character input values
    sDrive = Drive
    If Len(sDrive) = 1 Then
        sDrive = sDrive & ":"
    ElseIf Len(sDrive) = 2 And Right(sDrive, 1) = ":" Then
        sDrive = sDrive & ""
    End If
  
    sVolumeName = String$(255, Chr$(0))
    sDriveType = String$(255, Chr$(0))
    
    lRet = GetVolumeInformation(sDrive, sVolumeName, _
    255, lAns, 0, 0, sDriveType, 255)

    DriveSerialNumber = lAns
End Function

Private Sub Command1_Click()
    MsgBox DriveSerialNumber(Drive1.Drive)
End Sub

Harrie KalaChakra  Tuesday, November 24, 2009 7:43 AM
i m using NTFS HDD. so how can i extract it??
EnigmaVJ  Tuesday, November 24, 2009 7:53 AM
i get the return value as 0, i hope as you said its because of FAT. how do i extract from NTFS?
EnigmaVJ  Tuesday, November 24, 2009 7:54 AM
Harrie KalaChakra  Tuesday, November 24, 2009 8:17 AM
Did you bother to give the code I included a shot?
syntaxeater  Tuesday, November 24, 2009 10:27 AM

Thank you Allfor your friendly help.

HiEnigmaVJ,

Welcometo MSDN forums!

Code sample:How to get Hard Disk serial number in VB.NET?
Firstly you need to Add Reference to
System.Management.dll.

ImportsSystem.Management

PublicClassForm1

PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click

'GetHarddiskSerialNumber

DimsearcherAsManagementObjectSearcher=NewManagementObjectSearcher("SELECT*FROMWin32_PhysicalMedia")

ForEachwmi_HDAsManagementObjectInsearcher.Get()

MessageBox.Show(wmi_HD("SerialNumber"))

Next

EndSub

EndClass


Check this thread for reference:
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/f393708f-d7e3-4aa3-a624-7e8c6662f343/
How to get harddisk/motherboard serial number and CUP ID withVB.NET?

http://www.codeproject.com/KB/cs/hard_disk_serialno.aspx
How to Retrieve the REAL Hard Drive Serial Number in C#




Best regards,
Martin Xie


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Martin Xie - MSFT  Monday, November 30, 2009 9:19 AM
thank you for your assistance but how to add thereferenceSystem.Management.dll.can you please explain me as i m new to this programming.
EnigmaVJ  21 hours 56 minutes ago
In the "Solution Explorer" on the (default view) right hand side of your IDE, find your project listed. Right click your project and look for "Add Reference..." in the resulting context menu. Click it, make sure the ".Net" tab is in focus and find a component called "System.Management." Select it and hit the "Ok" button.
syntaxeater  21 hours 46 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Passing Values & Receiving Events between Forms created on separate threads
• Setting focus on textbox when form loads
• Using a like statement to find a pattern.
• auto complete on vb 2008
• Advice needed: What DB to use?
• How to know when a user insert, update
• Caching Dataset
• OPC - I can read but I can't write
• How Create setup for a vb.net application with sql database.
• What is the .NET Framework needed for v.8.0.50727.1433