start an app when windows starts

Just starting out? Need help? Post your questions and find answers here.
QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

start an app when windows starts

Post 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.
QuimV
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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).
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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 
BERESHEIT
User avatar
graves
Enthusiast
Enthusiast
Posts: 160
Joined: Wed Oct 03, 2007 2:38 pm
Location: To the deal with a pepper

Post by graves »

You can see any others locations, using Autoruns:

http://technet.microsoft.com/en-us/sysi ... 63902.aspx
QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

Post by QuimV »

:D Thanks a lot for your help!
:D
QuimV
ColBoy
Enthusiast
Enthusiast
Posts: 143
Joined: Fri Feb 13, 2004 2:37 pm
Location: Ottawa, Canada
Contact:

Post 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
Colin
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post 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:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post 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")
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Don't forget that "all users" require elevation on Vista and Window 7 etc.
Jeromyal
Enthusiast
Enthusiast
Posts: 216
Joined: Wed Jul 17, 2013 8:49 am

Re: start an app when windows starts

Post 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.
User avatar
HeX0R
Addict
Addict
Posts: 1187
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: start an app when windows starts

Post 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")
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: start an app when windows starts

Post 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)
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
mk-soft
Always Here
Always Here
Posts: 6201
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: start an app when windows starts

Post 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.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: start an app when windows starts

Post 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.
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Post Reply