The Windows 98 system registry contains important information about system configuration and operation. This document uses the KEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run sub-key branch as an example to describe how to modify the registry using Win32 API functions in Visual Basic.
Declare Win32 API functions
Visual Basic 6.0 does not provide any function to create, enable, disable, or delete the "key" in the system registry. Therefore, first, declare the following Win32 API functions at the beginning of the program:
Const REG_SZ = 1 Global Const HKEY_LOCAL_MACHINE = & H80000002 Declare Function RegOpenKey Lib "advapi32" Alias "RegOpenKeyA" (ByVal hkey As Long, ByVal lpszSubKey As String, phkResult As Long) As Long 'This function is used to open an existing key in the system registry. Function return value: if the key is successfully opened, 0 is returned; otherwise, non-0 is returned, PhkResult is set as the handle of the key. Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hkey Long, ByVal lpszValueName As String, ByVal DwReserved As Long, ByVal fdwType As Long, LpbData As Any, ByVal cbData As Long) As Long 'Add the key name and key value to the key specified in the system registry. Function return value: if the key name or value is successfully added, 0 is returned; otherwise, non-0 is returned. Declare Function RegCloseKey Lib "advapi32" Alias "RegCloseKey" (ByVal hkey As Long) As Long 'This function is used to close the key opened in the system registry. Function return value: if the key is disabled successfully, 0 is returned; otherwise, non-0 is returned.
|
Compile Function
Function oRegOpenKey (ByVal hkey As Long, ByVal lpszSubKey As String, phkResult As Long) As Boolean Dim lResult As Long On Error GoTo 0' close Error trap LResult = RegOpenKey (hkey, lpszSubKey, phkResult) If lResult = 0 Then ORegOpenKey = True Else oRegOpenKey = False End If End Function Function RegSetStringValue (ByVal hkey As Long, ByVal strValueName As String, ByVal strData As String, Optional ByVal flog) As Boolean Dim lResult As Long On Error GoTo 0 LResult = RegSetValueEx (hkey, strValueName, 0 &, REG_SZ, ByVal strData, LenB (StrConv (strData, VbFromUicode) + 1) 'Strconv is a function provided by Visual Basic, Returns the Variant (String) converted by the specified type ). 'Vbfromuicode is the system constant of Visual Basic. |
In the above program, StrConv (strData, vbFromUnicode) is used to convert a string to Unicode based on the system's default code.
If lResult = 0 Then RegSetStringValue = True Else RegSetStringValue = False End If End Function Write the following code in the Clik event Private sub Commandl_Clik () Dim hkey As Long Dim MyReturn As Long MyReturn = oRegOpenkey (HKEY_LOCAL_MACHINE, "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", hkey) If hkey = 0 Then MsgBox "the primary key you requested to open is not created, Or your input is incorrect. Check carefully and run the program again !" Exit Sub End If MyReturn = RegSetStringValue (hkey, "MyAcess", "d: \ programfiles \ office97 \ Office \ mymsaccess.exe ", False) 'If the flag is lost or is True, this action will Is recorded in the log file, and if you select Delete an installed application. This value is deleted by the application. If MyReturn Then MsgBox "your program has been successfully added When Windows 98 is started The system will automatically run your program !", VbExclamation, "Special prompt" Else MsgBox "Your code contains some errors, Please check carefully !", VbExclamation, "Special prompt" End If RegCloseKey (hkey) End Sub
|