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

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of