|
-
Oct 8th, 2008, 10:24 AM
#1
Thread Starter
Addicted Member
[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
-
Oct 8th, 2008, 10:46 AM
#2
Fanatic Member
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
-
Oct 8th, 2008, 10:46 AM
#3
Re: [2008] Checking Hard Disk Serial Number
I'd like to know what you mean by "code collaspe".
-
Oct 8th, 2008, 11:02 AM
#4
Thread Starter
Addicted Member
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
-
Oct 8th, 2008, 11:10 AM
#5
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
-
Oct 8th, 2008, 11:14 AM
#6
Thread Starter
Addicted Member
Re: [2008] Checking Hard Disk Serial Number
Hi,
To: GaryMazzone
Look my replay before
-
Oct 8th, 2008, 11:14 AM
#7
Thread Starter
Addicted Member
Re: [2008] Checking Hard Disk Serial Number
Hi,
To: GaryMazzone
Look my replay before
-
Oct 8th, 2008, 11:17 AM
#8
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
-
Oct 8th, 2008, 11:22 AM
#9
Thread Starter
Addicted Member
Re: [2008] Checking Hard Disk Serial Number
Hi,
To: GaryMazzone
Look my replay before and Pleas try the code.
-
Oct 8th, 2008, 12:52 PM
#10
Fanatic Member
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.
-
Oct 8th, 2008, 12:57 PM
#11
Thread Starter
Addicted Member
Re: [2008] Checking Hard Disk Serial Number
Hi,
In my computer the program is runing OK.
-
Oct 8th, 2008, 01:06 PM
#12
Fanatic Member
Re: [2008] Checking Hard Disk Serial Number
vb.net Code:
Imports System.Runtime.InteropServices
Imports ManagedWinapi
Imports WindowsHookLib
Imports System.Management
Public Class Form1
#Region " Form1 "
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Me.CheckHDSN() Then
'ITS A MATCH
Else
'ITS NOT A MATCH
Application.Exit()
'or
'Me.Close
'if this is the only form
End If
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
Private Function CheckHDSN() As Boolean
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
Return True
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
Return True
End If
Next
Catch ex As Exception
MessageBox.Show("An error occurred while querying for WMI data: VolumeSerialNumber " & ex.Message)
Return False
End Try
Me.Cursor = Cursors.Default
MessageBox.Show("Hard Disk Serial Number Not Matching !!!" & vbNewLine & vbNewLine & "Asta La Vista Baby !!!")
Return False
End Function
End Class
-
Oct 10th, 2008, 02:27 AM
#13
Thread Starter
Addicted Member
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.
-
Oct 10th, 2008, 06:03 AM
#14
Re: [2008] Checking Hard Disk Serial Number
Did the install the application? You realize they should right?
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Oct 10th, 2008, 08:46 AM
#15
Thread Starter
Addicted Member
Re: [2008] Checking Hard Disk Serial Number
Hi,
To: GaryMazzone
I do not understand what you mean.
Can you expand more the description ?
-
Oct 10th, 2008, 09:01 AM
#16
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
-
Oct 10th, 2008, 10:02 PM
#17
Fanatic Member
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.
-
Oct 11th, 2008, 01:05 AM
#18
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|