VB 6.0 - How can we find Hard Disk Drive serial number using vb6.0

Asked By Janardhan Bonthu on 10-Oct-08 10:57 PM
end of post

solution

Perry replied to Janardhan Bonthu on 10-Oct-08 11:04 PM
Hi,
You can use FileSystemObject to get your required info.
Private Sub Command4_Click()
Dim fso As New FileSystemObject
Dim mydrive As Drive
Dim path As String
'Initialize path.
path = "C:\"
'Get object.
Set mydrive = fso.GetDrive(path)
'Check for success.
MsgBox mydrive.DriveLetter 'displays "C"
End Sub
See http://support.microsoft.com/kb/q186118/ for details.

Alternatively you can use HardDriveInfo.DLL. It is an ActiveX COM DLL to read or get hard disk information. You can use HardDriveInfo.DLL to read the real serial number of hard disk easily. HardDriveInfo.DLL does not depend on the "support" libraries to get or fetch the details. Each Hard Disks have Unique Serial Number, some time it is necessary to get the hard disk serial number. So HardDriveInfo.DLL helps us to get the Hard disk serial number easily and efficiently, you can use this serial number to create an machine id or encrypt number.

The call to this dll as below:

Declare Function GetHDDSerialId Lib "HardDiskInfoV1.dll" () As String
Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long

Public function SetHardDiskID()as string
dim gHardDiskId as string
Dim flNumber As Long = LoadLibrary ("HardDiskInfoV1.dll")
gHardDiskId = GetHDDSerialId

If Asc(Right(gHardDiskId, 1)) = 0 Then
gHardDiskId = Trim(Mid(gHardDiskId, 1, Len(gHardDiskId) - 1))
Else
gHardDiskId = Trim(gHardDiskId)
End If
return gHardDiskId

see http://www.diskserialnumber.com/ and http://addressof.com/blog/archive/2004/02/13/386.aspx for details.

Regards,
Megha

Alternate solution

Perry replied to Janardhan Bonthu on 10-Oct-08 11:07 PM
Hi,

You can also acomplish this with the "GetVolumeInformation" API:

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

Public Function GetSerialNumber(Optional sDrive As String = "C:\") As Long
       GetVolumeInformation "C:\", vbNullString, 0, GetSerialNumber, 0, 0, vbNullString, 0
End Function

Please let me know if my posts are helpful to you.

Regards,
Megha

find hard disk drive sr no. using vb6

C_A P replied to Janardhan Bonthu on 11-Oct-08 02:11 AM

The Code

First, let's create a class to store information about a hard drive:

class HardDrive
{
private string model = null;
private string type = null;
private string serialNo = null;
public string Model
{
get {return model;}
set {model = value;}
}
public string Type
{
get {return type;}
set {type = value;}
}
public string SerialNo
{
get {return serialNo;}
set {serialNo = value;}
}
}

Next, add a reference to your project. Scroll down to System.Management under ComponentName. This reference is not provided by default, so you need to add it.

Add a "using System.Management;" at the top of your source code. System.Management allows us to access WMI objects. Put simply, WMI contains a lot information about your hardware.

Now there's a problem: the hard drive model is found in the Win32_DiskDrive class and the serial number is found in the Win32_PhysicalMedia class. Thus, we need to query twice and integrate the information into our HardDrive class.

Let's first create an ArrayList to store our HardDrive objects: ArrayList hdCollection = new ArrayList();. Next, we query the Win32_DiskDrive class:

   ManagementObjectSearcher searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

foreach(ManagementObject wmi_HD in searcher.Get())
{
HardDrive hd = new HardDrive();
hd.Model = wmi_HD["Model"].ToString();
hd.Type = wmi_HD["InterfaceType"].ToString();
hdCollection.Add(hd);
}

Now we need to extract the serial number from the Win32_PhysicalMedia class:

   searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

int i = 0;
foreach(ManagementObject wmi_HD in searcher.Get())
{
// get the hard drive from collection
// using index
HardDrive hd = (HardDrive)hdCollection[i];

// get the hardware serial no.
if (wmi_HD["SerialNumber"] == null)
hd.SerialNo = "None";
else
hd.SerialNo = wmi_HD["SerialNumber"].ToString();

++i;
}

Luckily, the WMI objects from Win32_DiskDrive are in the same order as from Win32_PhysicalMedia, so the simple code above works. Otherwise, we will have to match them using their unique DeviceID. Start a thread in this article if you think Win32_DiskDrive objects are not the same order as Win32_PhysicalMedia objects returned.

We are done! Now we display our hard drive's information:

   // Display available hard drives
foreach(HardDrive hd in hdCollection)
{
Console.WriteLine("Model\t\t: " + hd.Model);
Console.WriteLine("Type\t\t: " + hd.Type);
Console.WriteLine("Serial No.\t: " + hd.SerialNo);
Console.WriteLine();
}
find Hard Disk Drive serial number
Binny ch replied to Janardhan Bonthu on 11-Oct-08 02:43 AM
class HardDrive
{
private string model = null;
private string type = null;
private string serialNo = null;
public string Model
{
get {return model;}
set {model = value;}
}
public string Type
{
get {return type;}
set {type = value;}
}
public string SerialNo
{
get {return serialNo;}
set {serialNo = value;}
}
}
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

foreach(ManagementObject wmi_HD in searcher.Get())
{
HardDrive hd = new HardDrive();
hd.Model = wmi_HD["Model"].ToString();
hd.Type = wmi_HD["InterfaceType"].ToString();
hdCollection.Add(hd);

}
searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

int i = 0;
foreach(ManagementObject wmi_HD in searcher.Get())
{
// get the hard drive from collection
// using index
HardDrive hd = (HardDrive)hdCollection[i];

// get the hardware serial no.
if (wmi_HD["SerialNumber"] == null)
hd.SerialNo = "None";
else
hd.SerialNo = wmi_HD["SerialNumber"].ToString();

++i;

}
foreach(HardDrive hd in hdCollection)
{
Console.WriteLine("Model\t\t: " + hd.Model);
Console.WriteLine("Type\t\t: " + hd.Type);
Console.WriteLine("Serial No.\t: " + hd.SerialNo);
Console.WriteLine();
}

Go through this link for more assistance:
http://www.codeproject.com/KB/cs/hard_disk_serialno.aspx?fid=33442&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=51
haroon replied to Janardhan Bonthu on 03-Jun-11 05:53 AM
It Works fine, thank you