Restart
App At Windows Startup
Did you ever want your VB app to automatically start at
boot up, only if it was running at shut down and not every time you boot. This will do it.
Option Explicit
' Declare Constants.
Const REG_SZ = 1
Const HKEY_CURRENT_USER = &H80000001
' Declare Registry API calls.
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 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
Private Sub Form_QueryUnload(Cancel As
Integer, UnloadMode As Integer)
' The trick is to enter the app in the Run Once
key in the Registry
' at Unload only if it is being terminated by Windows.
' Dimension local variables.
Dim hKey As Long
Dim strRunCmd As
String
' Test to see what is causing the app to
terminate.
If UnloadMode = vbAppWindows Then
strRunCmd = App.Path & "\" &
App.EXEName & ".EXE"
' Create the RunOnce Key in the
registry.
Call
RegCreateKey(HKEY_CURRENT_USER, _
"Software\Microsoft\Windows\CurrentVersion\RunOnce", hKey)
' Insert your applications EXE
name into the new key.
Call RegSetValueEx(hKey,
"AppName", 0&, REG_SZ, ByVal strRunCmd, _
Len(strRunCmd) + 1)
' Very important step.....
Close the registry.
Call RegCloseKey(hKey)
End If
End Sub
|