Results 1 to 18 of 18

Thread: [2008] Checking Hard Disk Serial Number

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    [2008] Checking Hard Disk Serial Number

    Hi,

    VB2008

    Checking Hard Disk Serial Number

    The code below Checking the Serial Number of the Hard Disk: FROM Win32_PhysicalMedia
    And immediately after it Checking the Serial Number of the Hard Disk: FROM Win32_LogicalDisk
    If there is matching the program continue to run, If ther is no matching the program stop to run.

    In my computer the program is runing OK.

    I checked the code at other computers (Vista and XP).

    In Vista when there is a match the code run OK, when there is no match the code collapse.

    In XP if there is match or there is no match the code collapse.

    To run the code below needs Reference to System.Management

    Can someone check the code ?

    Thenks in advance
    Code:
    #Region " Imports "
    Imports System
    Imports System.IO
    Imports System.Management
    Imports System.Windows.Forms
    #End Region
    
    Public Class Form1
        '
    #Region " Form1 "
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            CheckHDSN()
        End Sub
        '
        Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
            MessageBox.Show("Hard Disk Serial Number Matching")
        End Sub
    #End Region
        '
    #Region " Private Sub "
        Private Sub CheckHDSN()
            Me.Cursor = Cursors.WaitCursor
            Try
                Dim Searcher_P As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PhysicalMedia")
                For Each queryObj As ManagementObject In Searcher_P.Get()
                    If queryObj("SerialNumber").ToString.Trim = "Y2S0RKFE" Then
                        Me.Cursor = Cursors.Default
                        Exit Sub
                    End If
                Next
            Catch ex As Exception
                MessageBox.Show("An error occurred while querying for WMI data: Win32_PhysicalMedia " & ex.Message)
            End Try
            '
            Try
                Dim Searcher_L As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_LogicalDisk WHERE DeviceID = 'C:'")
                For Each queryObj As ManagementObject In Searcher_L.Get()
                    If queryObj("VolumeSerialNumber").ToString.Trim = "226C1A0B" Then
                        Me.Cursor = Cursors.Default
                        Exit Sub
                    End If
                Next
            Catch ex As Exception
                MessageBox.Show("An error occurred while querying for WMI data: VolumeSerialNumber " & ex.Message)
            End Try
            Me.Cursor = Cursors.Default
            MessageBox.Show("Hard Disk Serial Number Not Matching !!!" & vbNewLine & vbNewLine & "Asta La Vista Baby !!!")
            End
        End Sub
    #End Region
        '
    End Class

  2. #2
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Re: [2008] Checking Hard Disk Serial Number

    I am not sure but maybe its the Exit Sub. If you have to use an Exit which I dont really like as it breaks the flow of operation, then shouldnt you do Exit For?

    edit: just noticed: what is the " End" doing before the "End Sub" statement

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2008] Checking Hard Disk Serial Number

    I'd like to know what you mean by "code collaspe".

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2008] Checking Hard Disk Serial Number

    Hi,

    What is the "End" doing before the "End Sub" statement?
    if there is match then exit sub else End

    I'd like to know what you mean by "code collapse".
    Error

    Pleas try the code

  5. #5
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2008] Checking Hard Disk Serial Number

    The END Statement Just before the End Sub Statement is an issue. After the messagebox saying No match you just told the application to END (or quit or exit)
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2008] Checking Hard Disk Serial Number

    Hi,

    To: GaryMazzone
    Look my replay before

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2008] Checking Hard Disk Serial Number

    Hi,

    To: GaryMazzone
    Look my replay before

  8. #8
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2008] Checking Hard Disk Serial Number

    END means EXIT THE APPLICATION.... If mean Exit the sub then put Exit Sub If you mean End If then put End If
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2008] Checking Hard Disk Serial Number

    Hi,

    To: GaryMazzone
    Look my replay before and Pleas try the code.

  10. #10
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Re: [2008] Checking Hard Disk Serial Number

    You dont make sense.
    What is that you need to do?
    END exits the program and thats where your probably getting unexpected results.

    Why dont you just let the sub finish naturally by it self? after the method is done, then close your application using Application.Exit.

    If you want your application to ONLY run when their is a match then your approching this the wrong way. But I can tell you that this method should not exit.
    The application.exit() should be placed in the method that calls this method.

    And I can go ahead and say to make this into a function. It returns true on match, otherwise false. Now you dont have to do the exit subs, and end statements.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2008] Checking Hard Disk Serial Number

    Hi,

    In my computer the program is runing OK.

  12. #12
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Re: [2008] Checking Hard Disk Serial Number

    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2. Imports ManagedWinapi
    3. Imports WindowsHookLib
    4. Imports System.Management
    5.  
    6.  
    7. Public Class Form1
    8. #Region " Form1 "
    9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.         If Me.CheckHDSN() Then
    11.             'ITS A MATCH
    12.         Else
    13.             'ITS NOT A MATCH
    14.             Application.Exit()
    15.             'or
    16.             'Me.Close
    17.             'if this is the only form
    18.         End If
    19.    
    20.     End Sub
    21.     '
    22.     Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    23.         MessageBox.Show("Hard Disk Serial Number Matching")
    24.     End Sub
    25. #End Region
    26.  
    27.     Private Function CheckHDSN() As Boolean
    28.  
    29.         Me.Cursor = Cursors.WaitCursor
    30.         Try
    31.             Dim Searcher_P As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PhysicalMedia")
    32.             For Each queryObj As ManagementObject In Searcher_P.Get()
    33.                 If queryObj("SerialNumber").ToString.Trim = "Y2S0RKFE" Then
    34.                     Me.Cursor = Cursors.Default
    35.                     Return True
    36.                 End If
    37.             Next
    38.         Catch ex As Exception
    39.             MessageBox.Show("An error occurred while querying for WMI data: Win32_PhysicalMedia " & ex.Message)
    40.         End Try
    41.         '
    42.         Try
    43.             Dim Searcher_L As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_LogicalDisk WHERE DeviceID = 'C:'")
    44.             For Each queryObj As ManagementObject In Searcher_L.Get()
    45.                 If queryObj("VolumeSerialNumber").ToString.Trim = "226C1A0B" Then
    46.                     Me.Cursor = Cursors.Default
    47.                     Return True
    48.                 End If
    49.             Next
    50.         Catch ex As Exception
    51.             MessageBox.Show("An error occurred while querying for WMI data: VolumeSerialNumber " & ex.Message)
    52.             Return False
    53.         End Try
    54.         Me.Cursor = Cursors.Default
    55.         MessageBox.Show("Hard Disk Serial Number Not Matching !!!" & vbNewLine & vbNewLine & "Asta La Vista Baby !!!")
    56.  
    57.         Return False
    58.     End Function
    59.  
    60. End Class

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2008] Checking Hard Disk Serial Number

    Hi,

    To: masfenix
    You did a nice job, changing the Sub to Function.

    In my computer it's working Ok

    I sent it to a friend (other computer (Vista)) he got a message:
    Object reference not set to an instance of an object.

    I sent to him Serial Numbers not matching. It's the same problem.

    Thanks for your will to help.

  14. #14
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2008] Checking Hard Disk Serial Number

    Did the install the application? You realize they should right?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2008] Checking Hard Disk Serial Number

    Hi,

    To: GaryMazzone

    I do not understand what you mean.
    Can you expand more the description ?

  16. #16
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2008] Checking Hard Disk Serial Number

    No it is pretty plain. Did you have them install the application through a set up package
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  17. #17
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Re: [2008] Checking Hard Disk Serial Number

    They need to give you the line number and/or stack trace. That message could occur in almost any place in the code. We need something specific.

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2008] Checking Hard Disk Serial Number

    Hi,

    I'm working on another way to Checking Hard Disk Serial Number.
    If I'll succeed I'll let you know.

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