Add registration restrictions to your application

Source: Internet
Author: User

Is it good to add registration restrictions for your application, at least the current shared software is doing this. You have used Winzip! When you do not register an account, a damn dialog box will pop up every time you start it. This dialog box will disappear only after it is correctly registered. This is what we want to achieve this time. For this information, we store it in the registry. VB6.0 provides a function and a statement for reading and writing the Registry. They are GetSetting and SaveSetting. Unfortunately, we cannot use it to write the key value in the registry. location. Is there no other function? NO! Yes! That is, the Win32 API of Micro $ oft provides functions that allow us to read and write the registry at any location, including creating a key value, deleting a key value, and creating a project ....... Let's take a look at how to implement it!

Do not change the default name of the following controls.
1. Create two forms.
2. place two text boxes and two buttons on the first form (Form1.
3. place 5 tags on the second form (Form2)
4. Create a standard module
You can paste the code and run it.

Module code
Option Explicit
This module is used to read and write registry keywords.
Different from the internal Registry Access Method of VB, it can
Read and Write any registry keywords using the string value.
---------------------------------------------------------------
-Registry API declaration...
RegCloseKey is used to close an entry (or key) in the system registry)
RegCreateKeyEx is used to create a registry key.
RegOpenKeyEx is used to open the registry key.
RegQueryValueEx is used to obtain the set value of an item.
RegSetValueEx is used to set the value of a specified item.
---------------------------------------------------------------
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)
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
---------------------------------------------------------------
-Registry Api constant...
---------------------------------------------------------------
Reg Data Types...
Public Const REG_SZ = 1 Unicode empty ending string
Public Const REG_EXPAND_SZ = 2 Unicode null Terminator string
Public Const REG_DWORD = 4 32-bit number
Public Const REG_BINARY = 3
Registry creation type value...
Public Const REG_OPTION_NON_VOLATILE = 0 keywords retained when the system restarts
Registry keyword 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_policy = & H10
Public Const KEY_CREATE_LINK = & H20
Public Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + key_policy + 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 keyword root type...
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

---------------------------------------------------------------
-Registry Security Attribute type...
---------------------------------------------------------------
Private Type SECURITY_ATTRIBUTES
NLength As Long
LpSecurityDescriptor As Long
BInheritHandle As Boolean
End Type
Bytes -------------------------------------------------------------------------------------------------
This function creates a new entry and key value in the registry.
Sample usage-Debug. Print UpodateKey (HKEY_CLASSES_ROOT, "keyname", "newvalue ")
Bytes -------------------------------------------------------------------------------------------------
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 processes a registry keyword
Dim hDepth As Long
Dim lpAttr As SECURITY_ATTRIBUTES registry security type
LpAttr. nLength = 50 sets the Security attribute to the default value...
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/modify the keyword value...
To make RegSetValueEx () work, enter a space...
Create/modify keyword Value
-Disable 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) exited
Exit Function error handling
CreateKeyError:
UpdateKey = False
Rc = RegCloseKey (hkey) tries to disable the keyword
End Function

Bytes -------------------------------------------------------------------------------------------------
This function reads the key value in the registry.
Sample usage-Debug. Print GetKeyValue (HKEY_CLASSES_ROOT, "COMCTL. ListviewCtrl.1CLSID ","")
Bytes -------------------------------------------------------------------------------------------------
Public Function GetKeyValue (KeyRoot As Long, KeyName As String, SubKeyRef As String) As String
Dim I As Long cyclic counter
Dim rc As Long return code
Dim hkey As Long process opened registry keywords
Dim hDepth As Long
Dim sKeyVal As String
Dim lKeyValType As Long registry keyword Data Type
Dim tmpVal As String temporary storage for registry keywords
Dim KeyValSize As Long registry keyword variable size
Open the Registry keyword under KeyRoot {HKEY_LOCAL_MACHINE ...}
------------------------------------------------------------
Rc = RegOpenKeyEx (KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hkey) Open the Registry keyword
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError handling error...
TmpVal = String $ (1024, 0) Allocate variable space
KeyValSize = 1024 mark variable size
------------------------------------------------------------
Retrieve the value of the registry keyword...
------------------------------------------------------------
Rc = RegQueryValueEx (hkey, SubKeyRef, 0 ,_
LKeyValType, tmpVal, KeyValSize) Obtain/create a keyword Value
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError error handling
TmpVal = Left $ (tmpVal, InStr (tmpVal, Chr (0)-1)
------------------------------------------------------------
Determines the Conversion Type of the keyword value...
------------------------------------------------------------
Select Case lKeyValType search data type...
Case REG_SZ, REG_EXPAND_SZ string registry keyword Data Type
SKeyVal = tmpVal copy the string value
Case REG_DWORD four-byte registry keyword Data Type
For I = Len (tmpVal) To 1 Step-1 To convert each bit
SKeyVal = sKeyVal + Hex (Asc (Mid (tmpVal, I, 1) generate a value with one character

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.