Put this code under a Basic Module
VB Code:
Option Explicit
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_CURRENT_CONFIG = &H80000005
Public Const HKEY_DYN_DATA = &H80000006
Private Const REG_SZ = 1 'Unicode nul terminated string
Private Const REG_BINARY = 3 'Free form binary
Private Const REG_DWORD = 4 '32-bit number
Private Const ERROR_SUCCESS = 0& 'unusually registry API functions return 0 on success
Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hkey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" _
Alias "RegCreateKeyA" (ByVal hkey As Long, ByVal lpSubKey _
As String, phkResult As Long) As Long
Private 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
Public Function RegSaveString(hkey As Long, strPath As String, _
strName As String, strData As String) As Boolean
Dim hCurKey&
If Right(strPath, 1) = "\" Then strPath = Left(strPath, Len(strPath) - 1)
Call RegCreateKey(hkey, strPath, hCurKey) 'create key/open handle if already exists
RegSaveString = (RegSetValueEx(hCurKey, strName, 0, REG_SZ, _
ByVal strData, Len(strData)) = ERROR_SUCCESS) 'set value
Call RegCloseKey(hCurKey) 'close registry handle
End Function
Call the function anywhere in app
VB Code:
'Add data into the registry
If RegSaveString(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "MyAPP", REG_SZ, "C:\1.exe") Then
MsgBox "Data saved"
Else
MsgBox "Fail to save"
End If
Note:
There different keys you can use are Run, runonce and RunService.
Run: Runs everytime Windows starts
runonce: Runs only once
RunService: Runs much before normal Apps start (usually Anti-Virus programs start here)