Date: 02-16-2022
Return to Index
created by gbSnippets
Run your Program at Each Startup
Category:
Registry
Type:
Snippets
Difficulty:
Intermediate
Author:
Andrew Chin
Version Compatibility: Visual Basic 6, Visual Basic 5
More information:
This piece of code places creates an entry for the running application in the registry key Software\Microsoft\Windows\CurrentVersion\Run, so the application will run every time the current user logs on.
Instructions: Copy the declarations and code below and paste directly into your VB project.
'PUT THIS IN A .BAS MODULE, OR CHANGE DECLARATIONS TO PRIVATE
Public 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
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal _
hKey As Long) As Long
Public 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 Const HKEY_CURRENT_USER = &H80000001
Public Const KEY_WRITE = &H20006
Public Const REG_SZ = 1
'THIS CODE MAKES YOUR APPLICATION RUN WHEN THE MACHINE IS BOOTED.
'RUN WHEN YOU WANT TO, SUCH AS FIRST TIME THE USER LOADS YOUR
'APPLICATION
Dim hregkey As Long
Dim subkey As String
Dim stringbuffer As String
subkey = "Software\Microsoft\Windows\CurrentVersion\Run"
retval = RegOpenKeyEx(HKEY_CURRENT_USER, subkey, 0, _
KEY_WRITE, hregkey)
If retval <> 0 Then
Debug.Print "Can't open the subkey"
Exit Sub
End if
stringbuffer = App.Path & "\" & App.EXEName & ".exe" & vbNullChar
retval = RegSetValueEx(hregkey, "My App", 0, REG_SZ, _
ByVal stringbuffer, Len(stringbuffer))
RegCloseKey hregkey
'gbs_01207
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm