devxlogo

Auto Start an Application from the Registry

Auto Start an Application from the Registry

Here is an example of how to make your application auto start from the registry.The code also shows how to query and clear this.

 'Module: Module1    Option Explicit        Private Const REG_SZ As Long = 1        Private Const HKEY_CURRENT_USER As Long = &H80000001    Private Const HKEY_LOCAL_MACHINE As Long = &H80000002        Private Const KeyName As String = _        "SoftwareMicrosoftWindowsCurrentVersionRun"        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 RegDeleteValue Lib "advapi32.dll" _        Alias "RegDeleteValueA" (ByVal hKey As Long, _            ByVal lpValueName As String) As Long    Private Declare Function RegOpenKey Lib "advapi32.dll" _        Alias "RegOpenKeyA" (ByVal hKey As Long, _            ByVal lpSubKey As String, phkResult As Long) As Long    Private Declare Function RegQueryValueEx Lib "advapi32.dll" _        Alias "RegQueryValueExA" (ByVal hKey As Long, _            ByVal lpValueName As String, ByVal lpReserved As Long, _            lpType As Long, lpData As Any, lpcbData 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 SetAutoStart(CurrentUser As Boolean, _            AppTitle As String, FileName As String) As Boolean    Dim hKey As Long        If CurrentUser Then            RegCreateKey HKEY_CURRENT_USER, KeyName, hKey        Else            RegCreateKey HKEY_LOCAL_MACHINE, KeyName, hKey        End If        If hKey Then            SetAutoStart = (RegSetValueEx(hKey, AppTitle, 0, _				REG_SZ, ByVal FileName, Len(FileName)) = 0)                    RegCloseKey hKey        End If    End Function        Public Function GetAutoStart(CurrentUser As Boolean, _            AppTitle As String) As Boolean    Dim hKey As Long    Dim lResult As Long    Dim lValueType As Long    Dim lDataBufSize As Long        If CurrentUser Then            RegOpenKey HKEY_CURRENT_USER, KeyName, hKey        Else            RegOpenKey HKEY_LOCAL_MACHINE, KeyName, hKey        End If        If hKey Then            lResult = RegQueryValueEx(hKey, AppTitle, 0, _				lValueType, ByVal 0, lDataBufSize)            GetAutoStart = (lDataBufSize > 0)            RegCloseKey hKey        End If    End Function        Public Function ClearAutoStart(CurrentUser As Boolean, _            AppTitle As String) As Boolean    Dim hKey As Long        If CurrentUser Then            RegOpenKey HKEY_CURRENT_USER, KeyName, hKey        Else            RegOpenKey HKEY_LOCAL_MACHINE, KeyName, hKey        End If        If hKey Then            RegDeleteValue hKey, AppTitle            RegCloseKey hKey        End If    End Function'Form: Form1'Add three CommandButtons to Form1    Option Explicit    Private Sub Command1_Click()        SetAutoStart True, App.Title, """" & App.Path & "" & App.EXEName & ".exe"""    End Sub    Private Sub Command2_Click()        If GetAutoStart(True, App.Title) Then            MsgBox "AutoStart: True"        Else            MsgBox "AutoStart: False"        End If    End Sub    Private Sub Command3_Click()        ClearAutoStart True, App.Title    End Sub

size=3>

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist