start an app when windows starts
Posted: Sun Feb 08, 2009 11:30 pm

I need to tell windows that start my app when windows starts, using the registry.
I would appreciate information about an easy way to do that.
Thanks in advanced.
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
Procedure StartWithWindows(State.b)
Protected Key.l = #HKEY_CURRENT_USER ;or #HKEY_LOCAL_MACHINE for every user on the machine
Protected Path.s = "Software\Microsoft\Windows\CurrentVersion\Run" ;or RunOnce if you just want to run it once
Protected Value.s = "GmailEnhancer" ;Change into the name of your program
Protected String.s = Chr(34)+ProgramFilename()+Chr(34) ;Path of your program
Protected CurKey.l
If State
RegCreateKey_(Key,@Path,@CurKey)
RegSetValueEx_(CurKey,@Value,0,#REG_SZ,@String,Len(String))
Else
RegOpenKey_(Key,@Path,@CurKey)
RegDeleteValue_(CurKey,@Value)
EndIf
RegCloseKey_(CurKey)
EndProcedure
Which doesn't work on non-english windows versions.ColBoy wrote:Another option, that doesn't use the registry is to create a shortcut and place it here :
\Documents and Settings\[username]\Start Menu\Programs\Startup
Where user name is the currently logged in user, or All Users for everyone.
Colin
Trond wrote:Which doesn't work on non-english windows versions.
Code: Select all
Procedure.s GetSpecialFolder(CSIDL.l)
Protected *itemid.ITEMIDLIST
Protected location.s = Space(#MAX_PATH)
If SHGetSpecialFolderLocation_ (0, CSIDL, @*itemid) = #NOERROR
If SHGetPathFromIDList_(*itemid, @location)
CoTaskMemFree_(*itemid)
If Right(location, 1) <> "" : location + "" : EndIf
ProcedureReturn location
EndIf
EndIf
EndProcedure
Debug GetSpecialFolder(#CSIDL_COMMON_STARTUP) ; AllUsers
Debug GetSpecialFolder(#CSIDL_STARTUP) ; Actuell User
Code: Select all
Global PreferenceFiles.s=#ProgramName+".ini"
Code: Select all
Global PreferenceFiles.s=GetPathPart(ProgramFilename())+#ProgramName+".ini"
Code: Select all
#Debug=0
:
CompilerIf #Debug
Global PreferencePath.s
CompilerElse
Global PreferencePath.s=GetPathPart(ProgramFilename())
CompilerEndIf
:
OpenPreferences(PreferencePath+#ProgramName+".ini")
Code: Select all
Procedure.s GetSpecialFolder(CSIDL)
Protected *itemid.ITEMIDLIST
Protected location.s = Space(#MAX_PATH)
If SHGetSpecialFolderLocation_ (0, CSIDL, @*itemid) = #NOERROR
If SHGetPathFromIDList_(*itemid, @location)
CoTaskMemFree_(*itemid)
If Right(location, 1) <> "\" : location + "\" : EndIf
EndIf
EndIf
ProcedureReturn Trim(location)
EndProcedure
Procedure CreateShellLink(FileName$, Link$, Argument$, Description$, WorkingDirectory$ = "")
Protected ppf.IPersistFile, Result, R2
CompilerIf #PB_Compiler_Unicode
Protected psl.IShellLinkW
CompilerElse
Protected psl.IShellLinkA
CompilerEndIf
CoInitialize_(0)
CompilerIf #PB_Compiler_Unicode
R2 = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLinkW, @psl)
CompilerElse
R2 = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLinkA, @psl)
CompilerEndIf
If R2 = #S_OK
psl\SetPath(FileName$)
psl\SetArguments(Argument$)
psl\SetWorkingDirectory(WorkingDirectory$)
psl\SetDescription(Description$)
psl\SetShowCmd(#SW_SHOWNORMAL)
psl\SetHotkey(#Null)
psl\SetIconLocation(FileName$, 0)
If psl\QueryInterface(?IID_IPersistFile, @ppf) = #S_OK
If ppf\Save(Link$, #True) = #S_OK
Result = #True
EndIf
ppf\Release()
EndIf
psl\Release()
EndIf
CoUninitialize_()
ProcedureReturn Result
DataSection
CLSID_ShellLink:
;00021401-0000-0000-C000-000000000046
Data.l $00021401
Data.w $0000, $0000
Data.b $C0, $00, $00, $00, $00, $00, $00, $46
IID_IShellLinkA:
;DEFINE_SHLGUID(IID_IShellLinkA, 0x000214EEL, 0, 0);
;C000-000000000046
Data.l $000214EE
Data.w $0000, $0000
Data.b $C0, $00, $00, $00, $00, $00, $00, $46
;IID_IShellLinkW {000214F9-0000-0000-C000-000000000046
IID_IShellLinkW:
Data.l $000214F9
Data.w $0000, $0000
Data.b $C0, $00, $00, $00, $00, $00, $00, $46
IID_IPersistFile:
;0000010b-0000-0000-C000-000000000046
Data.l $0000010b
Data.w $0000, $0000
Data.b $C0, $00, $00, $00, $00, $00, $00, $46
EndDataSection
EndProcedure
CreateShellLink(ProgramFilename(), GetSpecialFolder(#CSIDL_STARTUP) + "MyTool.lnk", "", "A startup tool")
Code: Select all
Procedure.s zRegKeyDelete(MainKey.i,SubKey.s,KeySet.s)
Protected.i hKey
Protected.s Result
If RegOpenKey_(MainKey,SubKey,@hKey)=#ERROR_SUCCESS
If RegDeleteValue_(hKey,KeySet)=#ERROR_SUCCESS
Result="DELETED"
EndIf
RegCloseKey_(hKey)
EndIf
ProcedureReturn Result
EndProcedure
Procedure.s zRegKeyRead(MainKey.i,SubKey.s,KeySet.s)
Protected.i DataSize,hKey
Protected.s KeyValue
KeyValue.s=Space(255)
DataSize.i=255
If RegOpenKeyEx_(MainKey,SubKey,0,#KEY_READ,@hKey)
Debug "*ERROR: Unable to read Registry"
KeyValue=""
Else
If RegQueryValueEx_(hKey,KeySet,0,0,@KeyValue,@DataSize)
Debug "*ERROR: Unable to read Key"
KeyValue=""
Else
KeyValue=Left(KeyValue,DataSize-1)
EndIf
RegCloseKey_(hKey)
EndIf
ProcedureReturn KeyValue
EndProcedure
Procedure.s zRegKeyWrite(MainKey.i,SubKey.s,KeySet.s,KeyValue.s)
Protected.i DataSize,hKey
Protected.s Result
hKey.i=0
If RegCreateKey_(MainKey,SubKey,@hKey)=#ERROR_SUCCESS
Result="KEY-CREATED ONLY"
DataSize.i=Len(KeyValue)*SizeOf(Character)
If RegSetValueEx_(hKey,KeySet,0,#REG_SZ,@KeyValue,DataSize)=#ERROR_SUCCESS
Result="VALUE-SET"
EndIf
RegCloseKey_(hKey)
EndIf
ProcedureReturn Result
EndProcedure
Procedure.s zStartWithWindows(State.b)
Protected MainKey.i = #HKEY_CURRENT_USER ;or #HKEY_LOCAL_MACHINE for every user on the machine
Protected SubKey.s = "Software\Microsoft\Windows\CurrentVersion\Run" ;or RunOnce if you just want to run it once (Path in the Registry)
Protected KeySet.s = "PBM-Software" ;Change into the name of your program
Protected KeyValue.s = Chr(34)+"C:\+ROOT\+PBM\+PROGRAM\pbm.exe"+Chr(34)+" /i" ;Path of your program
Protected.s R
Select State
Case 0
R=zRegKeyDelete(MainKey.i,SubKey.s,KeySet.s)
Case 1
R=zRegKeyRead(MainKey.i,SubKey.s,KeySet.s)
Case 2
R=zRegKeyWrite(MainKey.i,SubKey.s,KeySet.s,KeyValue.s)
EndSelect
ProcedureReturn R
EndProcedure
Debug zStartWithWindows(2)
That's entirely true. I agree with you.mk-soft wrote: Mon Apr 01, 2024 8:02 pm I don't like it when something is written to the registry without setup and uninstall.
If the programme is deleted, rubbish remains in the registry.