Results 1 to 7 of 7

Thread: How to pick system information in VB

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    45

    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?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim physicalMedia As New ManagementClass("Win32_PhysicalMedia")
    2.  
    3. For Each physicalMedium As ManagementObject In physicalMedia.GetInstances()
    4.     If physicalMedium("SerialNumber") <> Nothing Then
    5.         MessageBox.Show(CStr(physicalMedium("SerialNumber")))
    6.     End If
    7. 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:
    1. Dim physicalMedia As New ManagementClass("Win32_PhysicalMedia")
    2. Dim physicalMedium As PhysicalMedia
    3.  
    4. For Each mo As ManagementObject In physicalMedia.GetInstances()
    5.     physicalMedium = New PhysicalMedia(mo)
    6.  
    7.     If physicalMedium.SerialNumber <> Nothing Then
    8.         MessageBox.Show(physicalMedium.SerialNumber))
    9.     End If
    10. Next mo
    Having said all that, if you're using VB6 then I haven't got a clue.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    New Member
    Join Date
    Feb 2005
    Posts
    4

    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?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to pick system information in VB

    Quote 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:
    1. Dim searcher As New Management.ManagementObjectSearcher("select * from Win32_PhysicalMedia")
    2.         Dim wmi_HD As Management.ManagementObject
    3.  
    4.         For Each wmi_HD In searcher.Get()
    5.             Debug.WriteLine(String.Format("Physical Media Serial Number: {0}", wmi_HD("SerialNumber")))
    6.         Next
    7.  
    8.         searcher = New System.Management.ManagementObjectSearcher("select * from Win32_DiskDrive")
    9.         For Each wmi_HD In searcher.Get()
    10.             Debug.WriteLine(String.Format("Disk Drive Model: {0}", wmi_HD("Model")))
    11.         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:
    1. Dim driveMedia As New ManagementClass("Win32_DiskDrivePhysicalMedia")
    2.             Dim driveMedium As DiskDrivePhysicalMedia
    3.             Dim drive As DiskDrive
    4.             Dim medium As PhysicalMedia
    5.             Dim interfaceType As String
    6.             Dim serialNumber As String
    7.             Dim driveIndex As Integer = 0
    8.  
    9.             'Retrieve the hard disk serial numbers.
    10.             For Each mo As ManagementObject In driveMedia.GetInstances()
    11.                 driveMedium = New DiskDrivePhysicalMedia(mo)
    12.                 drive = New DiskDrive(driveMedium.Dependent)
    13.                 medium = New PhysicalMedia(driveMedium.Antecedent)
    14.  
    15.                 interfaceType = drive.InterfaceType
    16.                 serialNumber = medium.SerialNumber
    17.  
    18.                 drive.Dispose()
    19.                 medium.Dispose()
    20.                 mo.Dispose()
    21.  
    22.                 If (interfaceType = "IDE" OrElse interfaceType = "SCSI") AndAlso _
    23.                    serialNumber <> Nothing Then
    24.                     requestData.AppendFormat("D{0}|{1}|", driveIndex, serialNumber)
    25.                     driveIndex += 1
    26.                 End If
    27.             Next mo
    28.  
    29.             driveMedia.Dispose()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    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:
    1. Public Declare Function GetVolumeInformation Lib "kernel32" _
    2.   Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, _
    3.   ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, _
    4.   lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, _
    5.   ByVal lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, _
    6.   ByVal nFileSystemNameSize As Long) As Long
    7.  
    8. Public Function getSerialNumber() As Long
    9. Dim N As Long
    10.     'TRY {
    11.         On Error GoTo catch
    12.         GetVolumeInformation Left(App.Path, 3), String(255, Chr$(0)), 255, N, 0, 0, String(255, Chr$(0)), 255
    13.         getSerialNumber = Abs(N)
    14.     '} CATCH {
    15. catch:  If Err.Number Then
    16.             msgbox "Error reading system serial number."
    17.         End If
    18.     '} END TRY
    19. 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.

  7. #7
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    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
  •  



Click Here to Expand Forum to Full Width