[RESOLVED] Make my application run at startup - Windows Seven Problem
I tried to run this code to make my application run at startup, and it works in XP (in dev environment and compiled), although when I tried to use it on windows seven it works only when I am running it in the development environment, not when it is compiled which I find strange... Actually the returned error code from the function is 5 which in winerror.h is "access denied" (http://msdn.microsoft.com/en-us/library/ms819773.aspx) although I am logged in as a user that has full admin rights?
Here is the code:
0. In a new Project
1. Add a module:
Code:
Option Explicit
Public Const REG_SZ = 1 ' Unicode nul terminated string
Public Const REG_BINARY = 3 ' Free form binary
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_DYN_DATA = &H80000006
Private Const ERROR_SUCCESS = 0&
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 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
Public Function RegistrySaveString(hKey As Long, strPath As String, strValue As String, strData As String) As String
Dim h&, r
'Create a new key, or open the key already existing in that location, and return the handle to into h
r = RegCreateKey(hKey, strPath, h)
If r <> ERROR_SUCCESS Then RegistrySaveString = r: Exit Function
'create a subkey beneath the key just created/opened and store a string in it.
r = RegSetValueEx(h, strValue, 0, REG_SZ, ByVal strData, Len(strData))
If r = ERROR_SUCCESS Then RegistrySaveString = "Success" Else RegistrySaveString = r
'close the key
r = RegCloseKey(h)
End Function
Public Function RegistryDeleteString(hKey As Long, strPath As String, strValue As String) As String
Dim h&, r
'Create a new key
r = RegCreateKey(hKey, strPath, h)
If r <> ERROR_SUCCESS Then RegistryDeleteString = r: Exit Function
'Delete the key's value
r = RegDeleteValue(h, strValue)
If r = ERROR_SUCCESS Then RegistryDeleteString = "Success" Else RegistryDeleteString = r
'close the key
RegCloseKey h
End Function
Public Function MakeThisAppRunAtStartup() As String
MakeThisAppRunAtStartup = RegistrySaveString(HKEY_LOCAL_MACHINE, "SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN", App.EXEName, App.Path & "\" & App.EXEName & ".exe")
End Function
Public Function UnMakeThisAppRunAtStartup() As String
UnMakeThisAppRunAtStartup = RegistryDeleteString(HKEY_LOCAL_MACHINE, "SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN", App.EXEName)
End Function
' Errors from these function are in winnerror.h
' 1 is invalid function
' 2 is specified file is not found
' 3 is specified path is not found
' 4 is too many open files
' 5 is access denied
' Most used keys to write to
'"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run"
'"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run"
' CAUTION: make sure the registry setting is deleted when your application is removed.
2. Change the project-> Properties -> Make -> Title
To a custom title which will appear in the registry
3. Add two command buttons and a label to a form
4. Add this code in the form
Code:
Private Sub Command1_Click()
MsgBox MakeThisAppRunAtStartup
End Sub
Private Sub Command2_Click()
MsgBox UnMakeThisAppRunAtStartup
End Sub
Private Sub Form_Load()
Me.Label1 = "Registry Run at Startup" & vbCrLf & App.EXEName & vbCrLf & App.Path & "\" & App.EXEName & ".exe"
Me.Label1.BackColor = &HFFFFFF
Me.Command1.Caption = "Run at Startup"
Me.Command2.Caption = "Do Not Run at Startup"
End Sub
Re: Make my application run at startup - Windows Seven Problem
in windows 7 you need to elevate to run as administrator (even if an admin user is logged in), to write to hklm, you may be able to set values on a per user basis in hkcu, then each user can choose whether to run the program at start up
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Re: Make my application run at startup - Windows Seven Problem
Rather than code, right-click on your programs shortcut, and click "Run as administrator".
You can make that permanent (but still interactive) by altering the properties of the shortcut, or by using code to run another executable (which can be the same one).
Re: Make my application run at startup - Windows Seven Problem
Thanks si_the_geek, now I remember setting the advanced properties of the shortcut to VB6 on windows 7 to run as administrator so that I could set references and components in my VB6 projects.
So in order to be able to access:
"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run"
I need to elevate my application's priviledges to "run as administrator" even when I am already logged in with administrator rights - genius.
I found a solution:
1. Open a text file and paste this code in:
2. Change the Application Name (coloured red above) to your Application's Name.
3. Save the text file as YourCompiledApplication'sName.exe.manifest eg Project1.exe.manifest.
4. Put the saved text file (called a manifest file) in the same directory as the compiled exe.
That's it, just 1 Little text file, problem solved.
I tried adding it as a VB6 custom resource although it didn't work, perhaps there are other resource editors that can do it...