|
-
Sep 30th, 2005, 04:43 PM
#1
Thread Starter
Member
How to pick system information in VB
hi guys!
first of all, i want to say sorry, i didn't find any appropriate forum to post this topic so i am posting it here.
i want to get the system information, like i want to get the HardDisk unique number in VB, how can i?
-
Oct 11th, 2005, 05:16 PM
#2
Re: How to pick system information in VB
Which flavor of VB?
I would suggest you re-post this question (or ask a mod to move it), to the forum of that version of VB, either classic or .NET. You will get many more eyes seeing the question.
My usual boring signature: Nothing
 
-
Oct 22nd, 2005, 10:31 AM
#3
Re: How to pick system information in VB
In VB.NET you would use the System.Management. There are other similar ways, but here's a simple one if all you want is the HDD serial number:
VB Code:
Dim physicalMedia As New ManagementClass("Win32_PhysicalMedia")
For Each physicalMedium As ManagementObject In physicalMedia.GetInstances()
If physicalMedium("SerialNumber") <> Nothing Then
MessageBox.Show(CStr(physicalMedium("SerialNumber")))
End If
Next physicalMedium
Note that there is a Win32_DiskDrive class as well but it contains information about the drive, while the Win32_PhysicalMedia class contains information about the disk(s). There is also a Win32_DiskDrivePhysicalMedia class that relates the two.
You can also use the mgmtclassgen.exe utility included with VS.NET to generate a .NET class that corresponds to WMI class. To do that you would open the VS.NET command prompt, navigate to your source folder and enter "mgmtclassgen Win32_PhysicalMedia /l vb". That would create a PhysicalMedia.vb file that you can then add to your project. Your code would then look like this:
VB Code:
Dim physicalMedia As New ManagementClass("Win32_PhysicalMedia")
Dim physicalMedium As PhysicalMedia
For Each mo As ManagementObject In physicalMedia.GetInstances()
physicalMedium = New PhysicalMedia(mo)
If physicalMedium.SerialNumber <> Nothing Then
MessageBox.Show(physicalMedium.SerialNumber))
End If
Next mo
Having said all that, if you're using VB6 then I haven't got a clue.
-
Dec 21st, 2005, 11:42 AM
#4
New Member
Re: How to pick system information in VB
Not sure where to put this --- I'll start here.
Hoping to obtain HD serial number in a web application.. I'm using the following code in global.asax.vb :
Dim searcher As New Management.ManagementObjectSearcher("select * from Win32_PhysicalMedia")
Dim wmi_HD As Management.ManagementObject
For Each wmi_HD In searcher.Get()
Response.Write(wmi_HD("SerialNumber"))
Next
searcher = New System.Management.ManagementObjectSearcher("select * from Win32_DiskDrive")
For Each wmi_HD In searcher.Get()
Response.Write(wmi_HD("Model"))
Next
The first loop works fine and iterates through 2 management objects (I have 2 harddrives) - but the second loop is never entered. Apparently no objects were returned by the "PhysicalMedia" query. My question is "Why?" Every answer that occurs to me would also seem to necessitate the failure of the first loop as well. Any ideas?
-
Dec 21st, 2005, 08:03 PM
#5
Re: How to pick system information in VB
 Originally Posted by jaybousquet
Not sure where to put this --- I'll start here.
Hoping to obtain HD serial number in a web application.. I'm using the following code in global.asax.vb :
Dim searcher As New Management.ManagementObjectSearcher("select * from Win32_PhysicalMedia")
Dim wmi_HD As Management.ManagementObject
For Each wmi_HD In searcher.Get()
Response.Write(wmi_HD("SerialNumber"))
Next
searcher = New System.Management.ManagementObjectSearcher("select * from Win32_DiskDrive")
For Each wmi_HD In searcher.Get()
Response.Write(wmi_HD("Model"))
Next
The first loop works fine and iterates through 2 management objects (I have 2 harddrives) - but the second loop is never entered. Apparently no objects were returned by the "PhysicalMedia" query. My question is "Why?" Every answer that occurs to me would also seem to necessitate the failure of the first loop as well. Any ideas?
I tried that code with a minor variation to get it to display in a WinForms app while debugging and it worked fine:
VB Code:
Dim searcher As New Management.ManagementObjectSearcher("select * from Win32_PhysicalMedia")
Dim wmi_HD As Management.ManagementObject
For Each wmi_HD In searcher.Get()
Debug.WriteLine(String.Format("Physical Media Serial Number: {0}", wmi_HD("SerialNumber")))
Next
searcher = New System.Management.ManagementObjectSearcher("select * from Win32_DiskDrive")
For Each wmi_HD In searcher.Get()
Debug.WriteLine(String.Format("Disk Drive Model: {0}", wmi_HD("Model")))
Next
If you actually want to relate DiskDrive objects to PhysicalMedia objects, I strongly suggest that you use the Win32_DiskDrivePhysicalMedia class. I did it myself and it's not particularly difficult, although it took a little bit of wiggling. I used the MgmtClassGen.exe tool to create .NET classes, as I suggested earlier, and my code ended up looking like this:
VB Code:
Dim driveMedia As New ManagementClass("Win32_DiskDrivePhysicalMedia")
Dim driveMedium As DiskDrivePhysicalMedia
Dim drive As DiskDrive
Dim medium As PhysicalMedia
Dim interfaceType As String
Dim serialNumber As String
Dim driveIndex As Integer = 0
'Retrieve the hard disk serial numbers.
For Each mo As ManagementObject In driveMedia.GetInstances()
driveMedium = New DiskDrivePhysicalMedia(mo)
drive = New DiskDrive(driveMedium.Dependent)
medium = New PhysicalMedia(driveMedium.Antecedent)
interfaceType = drive.InterfaceType
serialNumber = medium.SerialNumber
drive.Dispose()
medium.Dispose()
mo.Dispose()
If (interfaceType = "IDE" OrElse interfaceType = "SCSI") AndAlso _
serialNumber <> Nothing Then
requestData.AppendFormat("D{0}|{1}|", driveIndex, serialNumber)
driveIndex += 1
End If
Next mo
driveMedia.Dispose()
-
Dec 29th, 2005, 12:42 AM
#6
Fanatic Member
Re: How to pick system information in VB
We use the computer's serial number for our licensing keys (people call in for keys).
We use this code:
VB Code:
Public 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, _
ByVal lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, _
ByVal nFileSystemNameSize As Long) As Long
Public Function getSerialNumber() As Long
Dim N As Long
'TRY {
On Error GoTo catch
GetVolumeInformation Left(App.Path, 3), String(255, Chr$(0)), 255, N, 0, 0, String(255, Chr$(0)), 255
getSerialNumber = Abs(N)
'} CATCH {
catch: If Err.Number Then
msgbox "Error reading system serial number."
End If
'} END TRY
End Function
Ironically, I found a bug in the code while copying it here. Thank god the function always worked (old error code would have ended up in an infinite loop or crashed the program, take your pick!!).
Last edited by alkatran; Dec 29th, 2005 at 01:01 AM.
Don't pay attention to this signature, it's contradictory.
-
Dec 29th, 2005, 02:02 PM
#7
Re: How to pick system information in VB
alright is there a similar code to find out the serial number for a cd/dvd when inserted
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|