|
-
May 22nd, 2009, 01:34 PM
#1
Thread Starter
Fanatic Member
Run on start up?
Hi everyone, i followed a thread on this forum on how to make my app start on windows start up.
the problem is it isnt working. the code is below. do i actually need to change or add anything. the code below as all exactly what i added.
it saves and keeps the check box ticked it i say i want it to run. but it actually dont start on start up :S
thanks!
Code:
Option Explicit
Private Sub Form_Load()
If WillRunAtStartup(App.EXEName) Then
Start_Up.Value = 1
Else
Start_Up.Value = 0
End If
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim SwW As Boolean
If Start_Up.Value = 1 Then
SwW = True
Else
SwW = False
End If
SetRunAtStartup App.EXEName, App.Path, SwW
End Sub
Bas Module
Code:
Option Explicit
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition 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 Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private 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
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 Const READ_CONTROL As Long = &H20000
Private Const KEY_SET_VALUE As Long = &H2
Private Const KEY_CREATE_SUB_KEY As Long = &H4
Private Const STANDARD_RIGHTS_WRITE As Long = (READ_CONTROL)
Private Const SYNCHRONIZE As Long = &H100000
Private Const KEY_WRITE As Long = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
Private Const STANDARD_RIGHTS_READ As Long = (READ_CONTROL)
Private Const KEY_ENUMERATE_SUB_KEYS As Long = &H8
Private Const KEY_NOTIFY As Long = &H10
Private Const KEY_QUERY_VALUE As Long = &H1
Private Const KEY_READ As Long = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Private Const ERROR_SUCCESS As Long = 0&
Private Const HKEY_CURRENT_USER As Long = &H80000001
Private Const REG_SZ As Long = 1
Public Function WillRunAtStartup(ByVal App_Name As String) As Boolean
Dim hKey As Long
Dim value_type As Long
If RegOpenKeyEx(HKEY_CURRENT_USER, _
"Software\Microsoft\Windows\CurrentVersion\Run", _
0, KEY_READ, hKey) = ERROR_SUCCESS _
Then
WillRunAtStartup = _
(RegQueryValueEx(hKey, App_Name, _
ByVal 0&, value_type, ByVal 0&, ByVal 0&) = _
ERROR_SUCCESS)
RegCloseKey hKey
Else
WillRunAtStartup = False
End If
End Function
Public 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
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
If run_at_startup Then
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
RegDeleteValue hKey, App_Name
End If
RegCloseKey hKey
exit_sub:
Exit Sub
SetStartupError:
MsgBox Err.Number & " " & Err.Description
Resume exit_sub
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|