Page 1 of 2

start an app when windows starts

Posted: Sun Feb 08, 2009 11:30 pm
by QuimV
:) Hi,
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.

Posted: Sun Feb 08, 2009 11:50 pm
by Trond
For all users it's stored here:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

Someone else may be able to tell you how to write to the registry (or use the search).

Posted: Mon Feb 09, 2009 12:23 am
by netmaestro
iirc, this code is by Joakim Christiansen, I use it and it seems to work well on all Windows platforms:

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 

Posted: Mon Feb 09, 2009 11:23 am
by graves
You can see any others locations, using Autoruns:

http://technet.microsoft.com/en-us/sysi ... 63902.aspx

Posted: Mon Feb 09, 2009 1:07 pm
by QuimV
:D Thanks a lot for your help!
:D

Posted: Mon Feb 09, 2009 4:12 pm
by ColBoy
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

Posted: Mon Feb 09, 2009 5:40 pm
by Trond
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
Which doesn't work on non-english windows versions.

Posted: Mon Feb 09, 2009 5:55 pm
by ts-soft
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
This works allways

For me, the shortcut in startup is more userfriendly. Start from Registry
only with prof. installer/deinstaller.

yust my two cents :wink:

Posted: Fri Mar 13, 2009 8:58 pm
by Michael Vogel
Hi,
just saw a small problem when using the registry to realize the auto start funcionality...

I was using a config file which stays in the same directory as the program itself, so I defined a simple...

Code: Select all

Global PreferenceFiles.s=#ProgramName+".ini"
...which worked fine and kept the program portable. But when the program gets started during the windows start, the ini file won't be found!

So this works fine for the compiled program:

Code: Select all

Global PreferenceFiles.s=GetPathPart(ProgramFilename())+#ProgramName+".ini"
:idea: Because this attempt did not work in the PB IDE, I made it like that now (Debug=0 when making the exe, Debug=1 while testing with the IDE):

Code: Select all

#Debug=0
 :
CompilerIf #Debug
	Global PreferencePath.s
CompilerElse
	Global PreferencePath.s=GetPathPart(ProgramFilename())
CompilerEndIf
 :
OpenPreferences(PreferencePath+#ProgramName+".ini")

Posted: Sat Mar 14, 2009 2:42 am
by Rescator
Don't forget that "all users" require elevation on Vista and Window 7 etc.

Re: start an app when windows starts

Posted: Mon Apr 01, 2024 5:51 pm
by Jeromyal
It seams this does not work with latest PureBasic and windows 10/11 ?

I need an application I am writing for my elderly mother, and need to be able for the app to enable or disable itself auto starting on user login.

via .lnk in GetUserDirectory(#PB_Directory_ProgramData) + "Microsoft\Windows\Start Menu\Programs\Startup\" preferred.
I just don't know how to write a link of ProgramFilename() to the forementioned startup directory.

Any help would be appreciated.

Re: start an app when windows starts

Posted: Mon Apr 01, 2024 6:40 pm
by HeX0R

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")

Re: start an app when windows starts

Posted: Mon Apr 01, 2024 7:51 pm
by charvista
Via the registry: (works on Windows 11)

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)

Re: start an app when windows starts

Posted: Mon Apr 01, 2024 8:02 pm
by mk-soft
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.

Re: start an app when windows starts

Posted: Mon Apr 01, 2024 8:18 pm
by charvista
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.
That's entirely true. I agree with you.