Tutorials to .com

Tutorials to .com » Dotnet » Winform » Your application for registration together with the restrictions

Your application for registration together with the restrictions

Print View , by: iSee ,Total views: 15 ,Word Count: 1885 ,Date: Sun, 23 Aug 2009 Time: 4:08 PM

Your application for registration together with the restriction is not very good, at least now sharing software to do so. We all used the software Winzip it! When the non-registration will be every time you start a bloody pop-up dialog box, only after the registration of the right of this dialog box will disappear. This is what we want to achieve this goal. For this information we will store in the registry provides a function of VB6.0 and a statement to read and write registry and GetSetting they SaveSetting regret is that we can not use it in the registry key to write anywhere. Is there no other function on it? NO! There is! Micro $ oft is the Win32 API function it provides will enable us to read and write arbitrary registry keys at any location, including new, delete key, new projects and so on ... .... No more nonsense that we first take a look at how to achieve it!

Control the use of the following please do not change the default name.
1, two new forms.
2, in the first form (Form1) to place the two text boxes and two buttons.
3, in the second form (Form2) to place 5 Tags
4, create a new standard module You can now paste the code to run the the past.

Module code
Option Explicit
''''The module for reading and writing registry keyword.
vb is different from the''internal''registry access method, it can
''''Through the string registry value to read and write any keyword.
''''---------------------------------------------- -----------------
API registry''''- statement ...
''''RegCloseKey to close the system registry for a key (or button)
''''RegCreateKeyEx used to create the registry key
''''RegOpenKeyEx to open the registry key
''''RegQueryValueEx to get the value of an item set
''''RegSetValueEx to set the value of the specified items
''''---------------------------------------------- -----------------
Public Declare Function RegCloseKey Lib "advapi32" (ByVal hkey As Long) As Long
Public Declare Function RegCreateKeyEx Lib "advapi32" Alias "RegCreateKeyExA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByRef phkResult As Long, ByRef lpdwDisposition As Long) As Long
Public Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
Public Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hkey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Public Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
''''---------------------------------------------- -----------------
Api registry''''- constant ...
''''---------------------------------------------- -----------------
''''Reg Data Types ...
Public Const REG_SZ = 1''''Unicode strings end space
Public Const REG_EXPAND_SZ = 2''''Unicode strings end space
Public Const REG_DWORD = 4''''32-bit digital
Public Const REG_BINARY = 3
''''Registry to create the type of value ...
Public Const REG_OPTION_NON_VOLATILE = 0''''When the system restarts, the keywords to be retained
Keywords''''registry security options ...
Public Const READ_CONTROL = & H20000
Public Const KEY_QUERY_VALUE = & H1
Public Const KEY_SET_VALUE = & H2
Public Const KEY_CREATE_SUB_KEY = & H4
Public Const KEY_ENUMERATE_SUB_KEYS = & H8
Public Const KEY_NOTIFY = & H10
Public Const KEY_CREATE_LINK = & H20
Public Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + READ_CONTROL
Public Const KEY_WRITE = KEY_SET_VALUE + KEY_CREATE_SUB_KEY + READ_CONTROL
Public Const KEY_EXECUTE = KEY_READ
Public Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL

''''Registry root type keywords ...
Public Const HKEY_CLASSES_ROOT = & H80000000
Public Const HKEY_CURRENT_USER = & H80000001
Public Const HKEY_LOCAL_MACHINE = & H80000002
Public Const HKEY_USERS = & H80000003
Public Const HKEY_PERFORMANCE_DATA = & H80000004

''''Return value ...
Public Const ERROR_NONE = 0
Public Const ERROR_BADKEY = 2
Public Const ERROR_ACCESS_DENIED = 8
Public Const ERROR_SUCCESS = 0

''''---------------------------------------------- -----------------
The type of security attributes''''- registry ...
''''---------------------------------------------- -----------------
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End Type
''''---------------------------------------------- -------------------------------------------------- --
''''Of the function in the registry key and create a new key
''''sample usage - Debug.Print UpodateKey (HKEY_CLASSES_ROOT, "keyname", "newvalue")
''''---------------------------------------------- -------------------------------------------------- --
Public Function UpdateKey (KeyRoot As Long, KeyName As String, SubKeyName As String, SubReg As Long, SubKeyValue As String, IngNumber As Long) As Long
Dim rc As Long''''return code
Dim hkey As Long''''dealing with a registry keyword
Dim hDepth As Long''''
Dim lpAttr As SECURITY_ATTRIBUTES''''registry security type
lpAttr.nLength = 50''''set the security attributes for the default values ...
lpAttr.lpSecurityDescriptor = 0''''...
lpAttr.bInheritHandle = True''''...
''''---------------------------------------------- --------------
''''- Create / open the registry keyword ...
''''Create / open / / KeyRoot / / KeyName
Error handling ...''''
''''---------------------------------------------- --------------
rc = RegCreateKeyEx (KeyRoot, KeyName, 0, "", 0, KEY_WRITE, lpAttr, hkey, hDepth)
If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError
''''---------------------------------------------- --------------
''''- Create / edit keyword value ...
''''Let RegSetValueEx () needs to enter a space ...
''''Create / modify the keywords value
Close the registry''''- keyword ...
''''---------------------------------------------- --------------
Select Case SubReg
Case REG_SZ
rc = RegSetValueEx (hkey, SubKeyName, 0, SubReg, SubKeyValue, IngNumber)
If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError
End Select
rc = RegCloseKey (hkey )'''' from the
Exit Function''''error-handling
CreateKeyError:
UpdateKey = False''''set the error return code
rc = RegCloseKey (hkey )'''' try to close the keyword
End Function

''''---------------------------------------------- -------------------------------------------------- --
''''Of the function keys in the registry to read
''''sample usage - Debug.Print GetKeyValue (HKEY_CLASSES_ROOT, "COMCTL.ListviewCtrl.1 \ CLSID", "")
''''---------------------------------------------- -------------------------------------------------- --
Public Function GetKeyValue (KeyRoot As Long, KeyName As String, SubKeyRef As String) As String
Dim I As Long''''cycle counter
Dim rc As Long''''return code
Dim hkey As Long''''to deal with open registry keyword
Dim hDepth As Long''''
Dim sKeyVal As String
Dim lKeyValType As Long''''registry data type keyword
Dim tmpVal As String''''temporary memory registry keyword
Dim KeyValSize As Long''''keyword registry variable size
''''In KeyRoot (HKEY_LOCAL_MACHINE ...) under the open registry keyword
''''---------------------------------------------- --------------
rc = RegOpenKeyEx (KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hkey)''''open registry keyword
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError''''to deal with error ...
tmpVal = String $ (1024, 0)''''the distribution of the variable space
KeyValSize = 1024''''tag variable size
''''---------------------------------------------- --------------
''''The value of the keyword search the registry ...
''''---------------------------------------------- --------------
rc = RegQueryValueEx (hkey, SubKeyRef, 0, _
lKeyValType, tmpVal, KeyValSize )'''' access / create the value of keywords
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError''''error-handling
tmpVal = Left $ (tmpVal, InStr (tmpVal, Chr (0)) - 1)
''''---------------------------------------------- --------------
Decision''''type conversion keyword value ...
''''---------------------------------------------- --------------
Select Case lKeyValType''''search data type ...
Case REG_SZ, REG_EXPAND_SZ''''registry string data type keyword
sKeyVal = tmpVal''''copy of the value string
Case REG_DWORD''''four-byte data type registry keyword
For I = Len (tmpVal) To 1 Step -1''''conversion each and every
sKeyVal = sKeyVal + Hex (Asc (Mid (tmpVal, I, 1)))''''one character value to generate a character.
Next
sKeyVal = Format $ ( "& h" + sKeyVal)''''for the four-byte string conversion
End Select

GetKeyValue = sKeyVal''''return value
rc = RegCloseKey (hkey )'''' closed registry keyword
Exit Exit Function''''
GetKeyError :'''' error occurred after the removal of ...
GetKeyValue = vbNullString''''set the return value is empty string
rc = RegCloseKey (hkey )'''' closed registry keyword
End Function

Form 1 (Form1) code
Private Sub Command1_Click ()
Dim password As String
If Text2.Text = "19811127" Then
UpdateKey HKEY_LOCAL_MACHINE, "software \ Programming prodigal son", "Name", REG_SZ, Text1.Text, LenB (Text1.Text)
UpdateKey HKEY_LOCAL_MACHINE, "software \ Programming prodigal," "registration code", REG_SZ, Text2.Text, 8
MsgBox "Thank you for your support of the return of a prodigal programming, please visit our Web site." & VbCrLf & "Http: / / vbchina.chinahot.com", vbOKOnly + vbInformation, "Thank you, programming your prodigal son"
Form2.Show
Unload Me
Else
MsgBox "Sorry! Registration code error, please visit Http: / / vbchina.chinahot.com access code!.", VbOKOnly + vbExclamation, "registration error"
Text2.SetFocus
Text2.SelStart = 0
Text2.SelLength = Len (Text2.Text)
End If
End Sub

Private Sub Command2_Click ()
Form2.Show
Unload Me
End Sub

Private Sub Form_Load ()
Command1.Caption = "OK"
Command2.Caption = "trial"
Dim password As String
password = GetKeyValue (HKEY_LOCAL_MACHINE, "software \ Programming prodigal son", "registration password")
If password = "198119811127" Then
Form2.Show
Unload Me
End If
End Sub

Form 2 (Form2) code
Private Sub Form_Load ()
Dim password As String, Name As String
password = GetKeyValue (HKEY_LOCAL_MACHINE, "software \ Programming prodigal son", "registration password")
Name = GetKeyValue (HKEY_LOCAL_MACHINE, "software \ Programming prodigal son", "name")
If password = "198119811127" Then
Label1.Caption = "Here is your registration information:"
Label2.Caption = "The software registered to the"
Label3.Caption = "Name:" & Name
Label4.Caption = "Company:" & Corp
Label5.Caption = "registration password:" & password
Else
Label1.Caption = "not registered"
Label2.Caption = "The software registered to the"
Label3.Caption = "Name: Jiang Jian"
Label4.Caption = "Company: Programming prodigal"
Label5.Caption = "registration password: Http: / / vbchina.chinahot.com"
End If
End Sub



.Net Windows development Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.