Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleMake a program run at startup
Keywordsstartup, boot, login, logon, start, run, execute
CategoriesAPI, Windows
 
Thanks to Neil Crosby.

To make the program run at startup, open this registry key:

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

Create a subkey named after the application with value giving the complete path to the executable. Note that the executable must exist or it won't start (obviously).

The WillRunAtStartup function opens the registry and looks for this key. It returns True if the key is present.

Subroutine SetRunAtStartup modifies the registry to make the application either run at startup or not. It begins by using RegCreateKeyEx to open the key HKEY_CURRENT_USER\...\Run. This API function opens the key, creating it if it doesn't already exist.

Next SetRunAtStartup creates or deletes the necessary key as appropriate.

 
' Return True if the program is set to run at startup.
Private Function WillRunAtStartup(ByVal app_name As String) _
    As Boolean
Dim hKey As Long
Dim value_type As Long

    ' See if the key exists.
    If RegOpenKeyEx(HKEY_CURRENT_USER, _
        "Software\Microsoft\Windows\CurrentVersion\Run", _
        0, KEY_READ, hKey) = ERROR_SUCCESS _
    Then
        ' Look for the subkey named after the application.
        WillRunAtStartup = _
            (RegQueryValueEx(hKey, app_name, _
                ByVal 0&, value_type, ByVal 0&, ByVal 0&) = _
                    _
            ERROR_SUCCESS)

        ' Close the registry key handle.
        RegCloseKey hKey
    Else
        ' Can't find the key.
        WillRunAtStartup = False
    End If
End Function
' Clear or set the key that makes the program run at
' startup.
Private Sub chkRun_Click()
    If m_IgnoreEvents Then Exit Sub

    SetRunAtStartup App.EXEName, App.Path, _
        (chkRun.Value = vbChecked)
End Sub
Private Sub Form_Load()
    ' See if the program is set to run at startup.
    m_IgnoreEvents = True
    If WillRunAtStartup(App.EXEName) Then
        chkRun.Value = vbChecked
    Else
        chkRun.Value = vbUnchecked
    End If
    m_IgnoreEvents = False
End Sub

' Determine whether the program will run at startup.
' To run at startup, there should be a key in:
' HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
' named after the program's executable with value
' giving its path.
Private Sub SetRunAtStartup(ByVal app_name As String, ByVal _
    app_path As String, Optional ByVal run_at_startup As _
    Boolean = True)
Dim hKey As Long
Dim key_value As String
Dim status As Long

    On Error GoTo SetStartupError

    ' Open the key, creating it if it doesn't exist.
    If RegCreateKeyEx(HKEY_CURRENT_USER, _
        "Software\Microsoft\Windows\CurrentVersion\Run", _
        ByVal 0&, ByVal 0&, ByVal 0&, _
        KEY_WRITE, ByVal 0&, hKey, _
        ByVal 0&) <> ERROR_SUCCESS _
    Then
        MsgBox "Error " & Err.Number & " opening key" & _
            vbCrLf & Err.Description
        Exit Sub
    End If

    ' See if we should run at startup.
    If run_at_startup Then
        ' Create the key.
        key_value = app_path & "\" & app_name & ".exe" & _
            vbNullChar
        status = RegSetValueEx(hKey, App.EXEName, 0, _
            REG_SZ, _
            ByVal key_value, Len(key_value))

        If status <> ERROR_SUCCESS Then
            MsgBox "Error " & Err.Number & " setting key" & _
                _
                vbCrLf & Err.Description
        End If
    Else
        ' Delete the value.
        RegDeleteValue hKey, app_name
    End If

    ' Close the key.
    RegCloseKey hKey
    Exit Sub

SetStartupError:
    MsgBox Err.Number & " " & Err.Description
    Exit Sub
End Sub
 
Formatted by Neil Crosby
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated