This example shows you how to create a registry key, ie. in the Form_load statement of a form so your app starts with windows every time the user reboots the machine. Run the code as such;
Call SetBootRegKey(HKEY_LOCAL_MACHINE, _
"YourAppName", _
App.Path & "\" & App.EXEName & ".exe", _
"Software\Microsoft\Windows\CurrentVersion\Run")
'use HKEY_CURRENT_USER for single/current user only
'use HKEY_LOCAL_MACHINE for all users is hKey
To remove the value temporarily without deleting the key from the registry simply call it as such...
Call SetBootRegKey(HKEY_LOCAL_MACHINE, _
"YourAppName", _
"", _
"Software\Microsoft\Windows\CurrentVersion\Run")
Place the following in a module in your project!
Option Explicit
'THIS MODLUE PERFORMS SPECIFIC API RELATED FUNCTIONS
'SYSTEM DEFINED CONSTANTS, TYPES AND VARIABLES
'=====================================================================
'FOR REG KEY SETTINGS
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const KEY_WRITE = &H20006
Public Const REG_SZ = 1
'END FOR REG KEY SETTINGS
'FOR REGISTRY SETTINGS
Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
(ByVal hKey As Long, ByVal lpSubKey As String, _
ByVal ulOptions As Long, _
ByVal samDesired As Long, _
phkResult As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, _
ByVal Reserved As Long, _
ByVal dwType As Long, _
lpData As Any, _
ByVal cbData As Long) As Long
'END FOR REG SETTINGS
'RUN APP AT BOOT ALL THE TIME WITH THIS CODE.
'RUN PREFERABLY ON FIRST START OF PROGRAM.
'=====================================================================
Function SetBootRegKey(ByVal hKey As Long, _
ByVal MinorKey As String, _
ByVal strKeyValue As String, _
ByVal subkey As String)
Dim hregkey As Long, stringbuffer As String ', subkey As String,
Dim retval As Long ', strKeyValue
'strKeyValue = App.Path & "\" & App.EXEName & ".exe"
'subkey = "Software\Microsoft\Windows\CurrentVersion\Run"
'use HKEY_CURRENT_USER for single/current user only
'use HKEY_LOCAL_MACHINE for all users is hKey
'MinorKey = the value that appears for run command
retval = RegOpenKeyEx(hKey, subkey, 0, _
KEY_WRITE, hregkey)
If retval <> 0 Then
Debug.Print "Can't open the subkey"
'OPTIONALLY ADD ERROR CODE HERE
Exit Function
End If
stringbuffer = strKeyValue & vbNullChar
retval = RegSetValueEx(hregkey, MinorKey, 0, REG_SZ, _
ByVal stringbuffer, Len(stringbuffer))
RegCloseKey hregkey
End Function
Comments