Option Explicit
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const ERROR_SUCCESS = 0&
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_NOTIFY = &H10
Private Const KEY_CREATE_LINK = &H20
Private Const SYNCHRONIZE = &H100000
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private 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
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey 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 EjecutarAlInicio(Byval Descripcion as string, Byval CmdLine As string) As Boolean
Dim lhKey1 As Long, lhKey2 As Long, lRc As Long
lRc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software", 0&, KEY_QUERY_VALUE, lhKey1)
If lRc <> ERROR_SUCCESS Then Exit Function
lRc = RegOpenKeyEx(lhKey1, "Microsoft", 0&, KEY_QUERY_VALUE, lhKey2)
RegCloseKey lhKey1
If lRc <> ERROR_SUCCESS Then Exit function
lRc = RegOpenKeyEx(lhKey2, "Windows", 0&, KEY_QUERY_VALUE, lhKey1)
RegCloseKey lhKey2
If lRc <> ERROR_SUCCESS Then Exit function
lRc = RegOpenKeyEx(lhKey1, "CurrentVersion", 0&, KEY_QUERY_VALUE, lhKey2)
RegCloseKey lhKey1
If lRc <> ERROR_SUCCESS Then Exit Function
lRc = RegOpenKeyEx(lhKey2, "Run", 0&, KEY_ALL_ACCESS, lhKey1)
RegCloseKey lhKey2
If lRc <> ERROR_SUCCESS Then Exit Function
CmdLine = CmdLine & Chr(0)
lRc = RegSetValueEx(lhKey1, Descripcion, 0&, 1&, ByVal CmdLine, Len(CmdLine))
EjecutarAlInicio = (lRc = ERROR_SUCCESS)
End Sub