How do I get HD serial number

  • Thread starter Thread starter Kurt
  • Start date Start date
K

Kurt

In VB4, VB6 it's done using

Public Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer AsString, ByVal nVolumeNameSize As Long, ByVal lpVolumeSerialNumber As Long,ByVal lpMaximumComponentLength As Long, ByVal lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long

but I don't know and can't find online how to do it within VB.NET 2005 express
any and all help is appreciated. Thank you.
 
I’m trying to learn on VB 2005 Express and none of the examples given above seem to work does anyone have something that will read serial number of HD. Using this VB 2005?
 
I’m trying to learn on VB 2005 Express and none of the examples given above seem to work does anyone have something that will read serial number of HD. Using this VB 2005?
 
Copy the code from that page (which I linked to earlier) and it should work. If not, then you need to be more specific than just saying that "none of the examples given above seem to work". They certainly wouldn't have the examples on the web site if they didn't work for _someone_. State clearly and precisely what isn't working for you, including any error messages that are shown. Pete
--------------------------------------------------------------------------------

Hi Peter, thanks for your efforts and patience.
I’ve copied the code that said VB.NET and the one that says VB from the link you provided,
And now am trying to work with the one that says VB.NET.
As soon as I pasted the private function in I get
“Type ‘System.Management.ManagementObject’ is not defined.”
“Type ‘System.Management.PropertyData’ is not defined.”

And because this VB.NET is new to me, I don’t know what or how to read and fix these errors.
Please have faith and patience I will / can learn this in a bit of time with some continued help. Thank you very much for your help thus far.
 
Copy the code from that page (which I linked to earlier) and it should work. If not, then you need to be more specific than just saying that "none of the examples given above seem to work". They certainly wouldn't have theexamples on the web site if they didn't work for _someone_. State clearly and precisely what isn't working for you, including any error messages thatare shown. Pete
---------------------------------------------------------------------------------

Hi Peter, and thanks for your efforts and patience.
I’ve copied the code that said VB.NET and the one that says VB from the link you provided,
And now am trying to work with the one that says VB.NET.

Private Function GetVolumeSerial(ByVal DriveLetter As String) As String

'Check for valid drive letter argument.
Dim ValidDriveLetters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
If ValidDriveLetters.IndexOf(DriveLetter) <> -1 Then
If DriveLetter.Length = 1 Then
Dim Disk As New System.Management.ManagementObject("Win32_LogicalDisk.DeviceID=""" & DriveLetter & ":""")
Dim DiskProperty As System.Management.PropertyData
For Each DiskProperty In Disk.Properties
If DiskProperty.Name = "VolumeSerialNumber" Then
Return DiskProperty.Value.ToString '.ToString 'Return the volume serial number.
End If
Next DiskProperty
End If
End If
Return Nothing 'Invalid drive letter.


End Function

As soon as I pasted the private function in I get
“Type ‘System.Management.ManagementObject’ is not defined.”
“Type ‘System.Management.PropertyData’ is not defined.”

And because this VB.NET is new to me, I don’t know what or how to read and fix these errors.
Please have faith and patience I will / can learn this in a bit of time with some continued help. Thank you very much for your help thus far.
 
The code below works fine in VB4, VB6, but when I run it using VB.NET it comes back with
serial_number = 0
I don’t get any errors, but it’s not giving me the serial number of thedrive either.
Can someone tell me what they think is the problem? I've tried everything Ican think of. Thank you.

Declarations;
Public Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, ByVal lpVolumeSerialNumber As Long, ByVal lpMaximumComponentLength As Long, ByVal lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long


Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim volume_name As String
Dim file_system_name As String
Dim serial_number As Long
Dim max_component_length As Long
Dim file_system_flags As Long
Dim s As Object

volume_name = Space$(256)
file_system_name = Space$(256)

Call GetVolumeInformation("C:\", volume_name, Len(volume_name), serial_number, max_component_length, file_system_flags, file_system_name, Len(file_system_name))
s = Format((serial_number), "000000000000")


Me.Label1.Text = serial_number
End Sub
 
Peter, I'm sorry to keep bugging you, but man I just am not getting it, I'm back to this code,
Private Function GetVolumeSerial(ByVal DriveLetter As String) As String

'Check for valid drive letter argument.
Dim ValidDriveLetters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
If ValidDriveLetters.IndexOf(DriveLetter) <> -1 Then
If DriveLetter.Length = 1 Then
Dim Disk As New System.Management.ManagementObject("Win32_LogicalDisk.DeviceID=""" & DriveLetter & ":""")
Dim DiskProperty As System.Management.PropertyData
For Each DiskProperty In Disk.Properties
If DiskProperty.Name = "VolumeSerialNumber" Then
Return DiskProperty.Value.ToString '.ToString 'Return the volume serial number.
End If
Next DiskProperty
End If
End If
Return Nothing 'Invalid drive letter.


End Function

I put that in my declarations but now I don't know how to call it, nothing I've done is working. I you can could you please see if you can assist. Thank you in advance for any more efforts you are willing to give.

Here's one more piece of code maybe it was above the code I posted above from the bottom of the page link you gave the other day.

System.Management.ManagementObject("Win32_LogicalDisk.DeviceID=" & DriveLetter & ":")
 
Dim DiskProperty As System.Management.PropertyData For Each DiskProperty In Disk.Properties If DiskProperty.Name = "VolumeSerialNumber" Then Return DiskProperty.Value.ToString '

I use GetVolumeSerial("C") and it goes through the function but never finds
DiskProperty.Name = "VolumeSerialNumber"

I did MessageBox.Show(DiskProperty.Name) and it never showd that, and changeing
If DiskProperty.Name = "VolumeSerialNumber" to something that did come up, it still comes back "False" no serial number.
Can you think of anything that I'm not doing right?
 
I cannot fathom, not without a concise-but-complete code example that reliably demonstrates the problem. I have tested the technique myself and haveverified that it works fine.
 
--------------------------------------------------------------------------------Damn,
I did an update for this VB.NET 2005 and now it's working. it's wierd thow it's a completely differnt serial number then what I get with vb4, or vb6. but it's using different lib. I believe.
Thanks Peter for your help, if I could buy you a beer or lunch I would. have a good Monday.
 
-------------------------------------------------------------------------------
Private Function GetVolumeSerial(ByVal DriveLetter As String) As String

'Check for valid drive letter argument.
Dim ValidDriveLetters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
If ValidDriveLetters.IndexOf(DriveLetter) <> -1 Then
If DriveLetter.Length = 1 Then
Dim Disk As New System.Management.ManagementObject("Win32_LogicalDisk.DeviceID=""" & DriveLetter & ":""")
Dim DiskProperty As System.Management.PropertyData
For Each DiskProperty In Disk.Properties
If DiskProperty.Name = "VolumeSerialNumber" Then
Return DiskProperty.Value.ToString '.ToString 'Return the volume serial number.
End If
Next DiskProperty
End If
End If
Return Nothing 'Invalid drive letter.


End Function

The problme was It was pasted as a private function but it was in a Module seperat from the request and I was not re-diming. it's all working now Thank you.

-------------------------------------------------------------------------------
Pete p.s. You don't say why you even want this information. Maybe it's justan intellectual exercise, which is fine.
-------------------------------------------------------------------------------
I've been using this method of using HD serial numberss for years to make the registration numbers for my softwares when a user installes the program it will use the orig serial number rearange the digits add some to it and then mails me the corrected order, them I e-mail it back to the buyer and they paste it into the form that comes up on their PC and if it matches the program will run. this keeps them from buying my programs and then giving itto their buddies.
-------------------------------------------------------------------------------

the "VolumeSerialNumber" property of the Win32_LogicalDisk class is not unique to the hardware, nor stable for a given installation (i.e. it could change without any change to the hardware).

I've never had it change on me yet. except now because this is capturing itin a slitely different way. so now since I'm re-writting my software now in VB.NET I will have to use the same method in both the program and the PHPfiles that I have online to corispond.

You have been very kind and a big help thank you for your services. Now I will more on the the next issies. lol : ) I just started 4 days ago rewritting this software and I will have to say there is deffentently a learning curve.
 
Back
Top