G
Guest
Thank you in advance for any and all assistance. Here is my problem. I have a
program that will create a registry key, however it is not and here is the
code and also the error that follows:
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Imports System.Net
Imports Microsoft.VisualBasic.Conversion
Imports System.Management
Imports Microsoft.Win32
Public Class License
Private szRegKey As String
Private szKeyValueName As String
Private szDateValueName As String
Public Property KeyValueName() As String
Get
Return szKeyValueName
End Get
Set(ByVal value As String)
szKeyValueName = value
End Set
End Property
Public Property DateValueName() As String
Get
Return szDateValueName
End Get
Set(ByVal value As String)
szDateValueName = value
End Set
End Property
Public Property RegKey() As String
Get
Return szRegKey
End Get
Set(ByVal value As String)
szRegKey = value
End Set
End Property
Public Function CheckValidity(ByVal szRegMainKey As String, ByVal
szKeyBaseString As String, ByVal nHours As Integer) As Boolean
Dim keyArray() As String
Dim rKey As RegistryKey = Nothing
Dim uB As Long
Dim szMasterKey As String
Dim i As Long
Dim szKeyEnc As String
Dim szKey As String
Dim szDate As String
Dim szDateEnc As String
Dim szDateD As Date
If szRegKey = "" Then Return False
If szKeyValueName = "" Then Return False
If szDateValueName = "" Then Return False
keyArray = szRegKey.Split("\")
uB = UBound(keyArray)
szMasterKey = ""
For i = 1 To uB
szMasterKey = szMasterKey & keyArray(i) & "\"
Next
szMasterKey = szMasterKey.Substring(0, szMasterKey.Length - 1)
Try
Select Case keyArray(0)
Case "HKEY_CLASSES_ROOT"
rKey = Registry.ClassesRoot.OpenSubKey(szMasterKey, True)
Case "HKEY_CURRENT_USER"
rKey = Registry.CurrentUser.OpenSubKey(szMasterKey, True)
Case "HKEY_LOCAL_MACHINE"
rKey = Registry.LocalMachine.OpenSubKey(szMasterKey, True)
Case "HKEY_USERS"
rKey = Registry.Users.OpenSubKey(szMasterKey, True)
Case "HKEY_CURRENT_CONFIG"
rKey = Registry.CurrentConfig.OpenSubKey(szMasterKey,
True)
End Select
Catch ex As Exception
Return False
End Try
szKeyEnc = rKey.GetValue(szKeyValueName).ToString()
szDateEnc = rKey.GetValue(szDateValueName).ToString()
rKey.Close()
If szKeyEnc = "" Then Return False
If szDateEnc = "" Then Return False
szKey = DecryptKey(szKeyEnc)
If szKey <> szKeyBaseString Then
Return False
End If
szDate = DecryptKey(szDateEnc)
If IsDate(szDate) = False Then Return False
szDateD = Convert.ToDateTime(szDate)
If DateDiff(DateInterval.Hour, szDateD, Now()) >= nHours Then
Return False
End If
Return True
End Function
Public Function SaveKeyToRegistry(ByVal szKeyValue As String) As Boolean
Dim keyArray() As String
Dim rKey As RegistryKey = Nothing
Dim uB As Long
Dim szMasterKey As String
Dim i As Long
Dim szDate As String
Dim szDateEnc As String
If szRegKey = "" Then Return False
If szKeyValueName = "" Then Return False
If szDateValueName = "" Then Return False
keyArray = szRegKey.Split("\")
uB = UBound(keyArray)
szMasterKey = ""
For i = 1 To uB
szMasterKey = szMasterKey & keyArray(i) & "\"
Next
szMasterKey = szMasterKey.Substring(0, szMasterKey.Length - 1)
Try
Select Case keyArray(0)
Case "HKEY_CLASSES_ROOT"
rKey = Registry.ClassesRoot.OpenSubKey(szMasterKey, True)
Case "HKEY_CURRENT_USER"
rKey = Registry.CurrentUser.OpenSubKey(szMasterKey, True)
Case "HKEY_LOCAL_MACHINE"
rKey = Registry.LocalMachine.OpenSubKey(szMasterKey, True)
Case "HKEY_USERS"
rKey = Registry.Users.OpenSubKey(szMasterKey, True)
Case "HKEY_CURRENT_CONFIG"
rKey = Registry.CurrentConfig.OpenSubKey(szMasterKey,
True)
End Select
Catch ex As Exception
Return False
End Try
szDate = Day(Now) & "/" & Month(Now) & "/" & Year(Now) & " " &
Hour(Now) & ":" & Minute(Now)
szDateEnc = GenerateKey(szDate)
Try
rKey.SetValue(szDateValueName, szDateEnc)
rKey.SetValue(szKeyValueName, szKeyValue)
rKey.Close()
Catch ex As Exception
rKey.Close()
Return False
End Try
Return True
End Function
Public Function GenerateKey(Optional ByVal szBaseString As String = "")
As String
Dim szOutput As String
Dim sHex As String
Dim sLen As Integer
Dim i As Integer
Dim nRet As String = ""
If szBaseString = "" Then szBaseString = GetBiosID()
szOutput = des.Encrypt(szBaseString)
sLen = szOutput.Length
For i = 0 To sLen - 1
sHex = Convert.ToString(Hex(Asc(szOutput.Substring(i, 1))))
If sHex.Length < 2 Then sHex = "0" & sHex
nRet = nRet & sHex
Next
If nRet.EndsWith("3D3D") Then nRet = nRet.Substring(0, nRet.Length -
4)
Return nRet
End Function
Public Function DecryptKey(ByVal szBaseString As String) As String
Dim nRet As String = ""
Dim sChar As String
Dim sLen As Integer
Dim i As Integer
sLen = szBaseString.Length
For i = 0 To sLen - 1 Step 2
sChar = Chr("&H" & szBaseString.Substring(i, 2))
nRet = nRet & sChar
Next
Return des.Decrypt(nRet)
End Function
Public Function GetBiosID() As String
Dim searcher As Object = New ManagementObjectSearcher("SELECT * FROM
Win32_BIOS")
Dim info As ManagementObject
Dim nRet As String = ""
For Each info In searcher.Get()
nRet += info("manufacturer").ToString() &
info("serialnumber").ToString()
Next
Return nRet
End Function
When I run this code in an application I get this:
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="LicenseLib"
StackTrace:
at LicenseLib.License.SaveKeyToRegistry(String szKeyValue)
at Sample_Ins.frmMain.InstallRegisterNewKey(String szClientName,
Boolean isMachineLinked, Int64 nHours) in
E:\LicenseCode\Sample_Ins\Sample_Ins\frmMain.vb:line 95
at Sample_Ins.frmMain.btnSample_Click(Object sender, EventArgs e) in
E:\LicenseCode\Sample_Ins\Sample_Ins\frmMain.vb:line 50
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Sample_Ins.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
When I attempt to debug, I never even get a registry value created. HELP
PLEASE!
program that will create a registry key, however it is not and here is the
code and also the error that follows:
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Imports System.Net
Imports Microsoft.VisualBasic.Conversion
Imports System.Management
Imports Microsoft.Win32
Public Class License
Private szRegKey As String
Private szKeyValueName As String
Private szDateValueName As String
Public Property KeyValueName() As String
Get
Return szKeyValueName
End Get
Set(ByVal value As String)
szKeyValueName = value
End Set
End Property
Public Property DateValueName() As String
Get
Return szDateValueName
End Get
Set(ByVal value As String)
szDateValueName = value
End Set
End Property
Public Property RegKey() As String
Get
Return szRegKey
End Get
Set(ByVal value As String)
szRegKey = value
End Set
End Property
Public Function CheckValidity(ByVal szRegMainKey As String, ByVal
szKeyBaseString As String, ByVal nHours As Integer) As Boolean
Dim keyArray() As String
Dim rKey As RegistryKey = Nothing
Dim uB As Long
Dim szMasterKey As String
Dim i As Long
Dim szKeyEnc As String
Dim szKey As String
Dim szDate As String
Dim szDateEnc As String
Dim szDateD As Date
If szRegKey = "" Then Return False
If szKeyValueName = "" Then Return False
If szDateValueName = "" Then Return False
keyArray = szRegKey.Split("\")
uB = UBound(keyArray)
szMasterKey = ""
For i = 1 To uB
szMasterKey = szMasterKey & keyArray(i) & "\"
Next
szMasterKey = szMasterKey.Substring(0, szMasterKey.Length - 1)
Try
Select Case keyArray(0)
Case "HKEY_CLASSES_ROOT"
rKey = Registry.ClassesRoot.OpenSubKey(szMasterKey, True)
Case "HKEY_CURRENT_USER"
rKey = Registry.CurrentUser.OpenSubKey(szMasterKey, True)
Case "HKEY_LOCAL_MACHINE"
rKey = Registry.LocalMachine.OpenSubKey(szMasterKey, True)
Case "HKEY_USERS"
rKey = Registry.Users.OpenSubKey(szMasterKey, True)
Case "HKEY_CURRENT_CONFIG"
rKey = Registry.CurrentConfig.OpenSubKey(szMasterKey,
True)
End Select
Catch ex As Exception
Return False
End Try
szKeyEnc = rKey.GetValue(szKeyValueName).ToString()
szDateEnc = rKey.GetValue(szDateValueName).ToString()
rKey.Close()
If szKeyEnc = "" Then Return False
If szDateEnc = "" Then Return False
szKey = DecryptKey(szKeyEnc)
If szKey <> szKeyBaseString Then
Return False
End If
szDate = DecryptKey(szDateEnc)
If IsDate(szDate) = False Then Return False
szDateD = Convert.ToDateTime(szDate)
If DateDiff(DateInterval.Hour, szDateD, Now()) >= nHours Then
Return False
End If
Return True
End Function
Public Function SaveKeyToRegistry(ByVal szKeyValue As String) As Boolean
Dim keyArray() As String
Dim rKey As RegistryKey = Nothing
Dim uB As Long
Dim szMasterKey As String
Dim i As Long
Dim szDate As String
Dim szDateEnc As String
If szRegKey = "" Then Return False
If szKeyValueName = "" Then Return False
If szDateValueName = "" Then Return False
keyArray = szRegKey.Split("\")
uB = UBound(keyArray)
szMasterKey = ""
For i = 1 To uB
szMasterKey = szMasterKey & keyArray(i) & "\"
Next
szMasterKey = szMasterKey.Substring(0, szMasterKey.Length - 1)
Try
Select Case keyArray(0)
Case "HKEY_CLASSES_ROOT"
rKey = Registry.ClassesRoot.OpenSubKey(szMasterKey, True)
Case "HKEY_CURRENT_USER"
rKey = Registry.CurrentUser.OpenSubKey(szMasterKey, True)
Case "HKEY_LOCAL_MACHINE"
rKey = Registry.LocalMachine.OpenSubKey(szMasterKey, True)
Case "HKEY_USERS"
rKey = Registry.Users.OpenSubKey(szMasterKey, True)
Case "HKEY_CURRENT_CONFIG"
rKey = Registry.CurrentConfig.OpenSubKey(szMasterKey,
True)
End Select
Catch ex As Exception
Return False
End Try
szDate = Day(Now) & "/" & Month(Now) & "/" & Year(Now) & " " &
Hour(Now) & ":" & Minute(Now)
szDateEnc = GenerateKey(szDate)
Try
rKey.SetValue(szDateValueName, szDateEnc)
rKey.SetValue(szKeyValueName, szKeyValue)
rKey.Close()
Catch ex As Exception
rKey.Close()
Return False
End Try
Return True
End Function
Public Function GenerateKey(Optional ByVal szBaseString As String = "")
As String
Dim szOutput As String
Dim sHex As String
Dim sLen As Integer
Dim i As Integer
Dim nRet As String = ""
If szBaseString = "" Then szBaseString = GetBiosID()
szOutput = des.Encrypt(szBaseString)
sLen = szOutput.Length
For i = 0 To sLen - 1
sHex = Convert.ToString(Hex(Asc(szOutput.Substring(i, 1))))
If sHex.Length < 2 Then sHex = "0" & sHex
nRet = nRet & sHex
Next
If nRet.EndsWith("3D3D") Then nRet = nRet.Substring(0, nRet.Length -
4)
Return nRet
End Function
Public Function DecryptKey(ByVal szBaseString As String) As String
Dim nRet As String = ""
Dim sChar As String
Dim sLen As Integer
Dim i As Integer
sLen = szBaseString.Length
For i = 0 To sLen - 1 Step 2
sChar = Chr("&H" & szBaseString.Substring(i, 2))
nRet = nRet & sChar
Next
Return des.Decrypt(nRet)
End Function
Public Function GetBiosID() As String
Dim searcher As Object = New ManagementObjectSearcher("SELECT * FROM
Win32_BIOS")
Dim info As ManagementObject
Dim nRet As String = ""
For Each info In searcher.Get()
nRet += info("manufacturer").ToString() &
info("serialnumber").ToString()
Next
Return nRet
End Function
When I run this code in an application I get this:
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="LicenseLib"
StackTrace:
at LicenseLib.License.SaveKeyToRegistry(String szKeyValue)
at Sample_Ins.frmMain.InstallRegisterNewKey(String szClientName,
Boolean isMachineLinked, Int64 nHours) in
E:\LicenseCode\Sample_Ins\Sample_Ins\frmMain.vb:line 95
at Sample_Ins.frmMain.btnSample_Click(Object sender, EventArgs e) in
E:\LicenseCode\Sample_Ins\Sample_Ins\frmMain.vb:line 50
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Sample_Ins.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
When I attempt to debug, I never even get a registry value created. HELP
PLEASE!