Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add/Remove startup items in MSConfig 1

Status
Not open for further replies.

SkennyR

Programmer
Mar 7, 2004
157
US
Ive been searching on here and on the net for a easy way to add my program to the startup list in msconfig, and to remove it, both from the program itself.
I finally came up with this:
I have one check box on the form.

Code:
Private sub Form_Load()
'This checks to see if the startup exists, and marks the_check box accordingly
On Error GoTo errman
 Check1.value = 1
 CreateObject("wscript.shell").RegDelete ("HKCU\Software\Microsoft\Windows\CurrentVersion\Run\" & "appname")
 CreateObject("wscript.shell").RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Run\" & "appname", CreateObject("scripting.filesystemobject").BuildPath(Chr(34) & app.path), "app.exename" & (Chr(34))
 Exit Sub
errman:
 Check1.value = 0
end sub

I then call one of two subs on exit, based on check value:

Code:
Private Sub makestartup()
'Place startup shortcut in msconfig
CreateObject("wscript.shell").RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Run\" & "app.exename", CreateObject("scripting.filesystemobject").BuildPath(Chr(34) & app.path, "app.exename" & Chr(34))
 Check1.value = 1
End Sub

Private Sub removestartup()
'Remove startup shortcut in msconfig
On Error GoTo errman
 CreateObject("wscript.shell").RegDelete ("HKCU\Software\Microsoft\Windows\CurrentVersion\Run\" & "appname")
 Check1.value = 0
Exit Sub
errman:
Check1.value = 0
End Sub

I call the subs in form_QueryUnload
Code:
If Check1.value = 1 Then makestartup
If Check1.value = 0 Then removestartup

This works just fine if I leave quote marks off the app.path and app.exename (chr(34)), but if the app.path has any spaces in it, it wont load on startup, even though it is in the msconfig list.
I fixed this by including the quotes (CHR(34))
But, now when my app starts, it removes the app from the msconfig list, regardless of the checkbox being checked.

I think it is a problem with how I am checking to see if the app is in the list (in form_load).
Is there an easier way to check to see if the app is in the list? Im using this method to set the checkbox or not or app start.



 
I think I have fixed this:
I added the following code:

Code:
Dim junk as string
dim junk2 as string

junk=chr(34)+app.path
junk2=app.exename+".exe"+chr(34)

CreateObject("wscript.shell").RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Run\" & "app.exename", CreateObject("scripting.filesystemobject").BuildPath(junk, junk2) ' <<<< changed here

What are your thoughts on doing this like this?
It seems to work good on my desktop, WinXP pro.
Is there any reason I shouldnt use this method?
Thanks
 
Chr(34) & CreateObject("scripting.filesystemobject").BuildPath(app.path, "app.exename") & Chr(34)
 
Hey thanks strongm! That is a much neater way of doing it.
Do you see any inherent problems with doing it this way?
 
You can also do it with APIs. This version adds it Automatically every time you run it if not already there.

Code:
'Delcarations
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal MyKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal MyKey As Long, ByVal lpSubKey As String, _
    ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal MyKey As Long, ByVal lpSubKey As String, _
    ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As Any, _
    phkResult As Long, lpdwDisposition As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal MyKey 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 RegCloseKey Lib "advapi32.dll" (ByVal MyKey As Long) As Long


Sub Form_Load()'or can be Add Start if you want it contoll it manually
AppPath = App.Path & IIf(Right(App.Path, 1) = "\", "", "\") & App.EXEName & ".exe Hidden"
    If RegCreateKeyEx(&H80000002, "Software\Microsoft\Windows\CurrentVersion\Run", 0&, vbNullString, 0, 983103, ByVal 0&, MyKey, nResult) = 0 Then
        Call RegSetValueEx(MyKey, App.EXEName, 0&, 1&, ByVal AppPath, Len(AppPath))
    End If
    Call RegCloseKey(MyKey)

'rest of form_load . . . 
End Sub


Sub RemoveStart()
    Dim MyKey As Long
    If RegOpenKeyEx(&H80000002, "Software\Microsoft\Windows\CurrentVersion\Run", 0&, 983103, MyKey) = 0 Then
        Call RegDeleteValue(MyKey, App.EXEName)
    End If
    Call RegCloseKey(MyKey)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top