Start your program with windows

Share your advanced PureBasic knowledge/code with the community.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Start your program with windows

Post by Joakim Christiansen »

Code updated For 5.20+
I needed this for a project, and I didn't find any registry functions in PB and I didn't want to use a lib either...
So I did some searching, and found a great page with some registery functions for VB (which is easy to translate to PB):
http://www.skillreactor.org/cgi-bin/index.pl?registry

I just translated what I needed, and made a little example on how you could add and remove your program from startup:

Code: Select all

; Translated from:
; http://www.skillreactor.org/cgi-bin/index.pl?registry

Procedure SaveValue(hKey.l,strPath.s,strValue.s,strData.s)
  Protected hCurKey.l,lRegResult.l
  
	lRegResult = RegCreateKey_(hKey,@strPath,@hCurKey)
	lRegResult = RegSetValueEx_(hCurKey,@strValue,0,#REG_SZ,@strData,Len(strData))
  
	If lRegResult <> #ERROR_SUCCESS
		;there is a problem
	EndIf
  
	lRegResult = RegCloseKey_(hCurKey)
EndProcedure

Procedure DeleteValue(hKey.l,strPath.s,strValue.s)
  Protected hCurKey.l,lRegResult.l
  
	lRegResult = RegOpenKey_(hKey,@strPath,@hCurKey)
	lRegResult = RegDeleteValue_(hCurKey,@strValue)
	lRegResult = RegCloseKey_(hCurKey)
EndProcedure

SaveValue(#HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Run","MyProgram",Chr(34)+ProgramFilename()+Chr(34))
;DeleteValue(#HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Run","MyProgram")
EDIT:
Or a simpler way, didn't test it yet but it should work:

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 = "MyProgram" ;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
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Needed something like this and here it is :D

Works fine.

Thanks for the submission.

cheers
User avatar
jqn
User
User
Posts: 97
Joined: Fri Oct 31, 2003 3:04 pm

Post by jqn »

Hi,
Download "Autoruns" from http://www.sysinternals.com/Utilities/Autoruns.html

and see some others REG KEYS for auto-start.
MikeB
Enthusiast
Enthusiast
Posts: 183
Joined: Sun Apr 27, 2003 8:39 pm
Location: Cornwall UK

Post by MikeB »

Perhaps I'm missing something, but wouldn't it be easier to just put a shortcut to your program in “C:\Documents and Settings\All Users\Start Menu\Programs\Startup\” or substitute a given user for “All Users”. :)
Mike.
(I'm never going to catch up with the improvements to this program)
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

#HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\RunServicesOnce"

make your program be one of the first programs running after windows start.
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 »

That's good work JC, thanks for sharing it!
BERESHEIT
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

MikeB wrote:Perhaps I'm missing something, but wouldn't it be easier to just put a shortcut to your program in “C:\Documents and Settings\All Users\Start Menu\Programs\Startup\” or substitute a given user for “All Users”. :)
Except on my system it would be "r:" and on my wife's "e:", etc. And "r:" is not where I install programs, so it's not even the executable path. :)

This seemed to be just as easy and works fine.

cheers
MikeB
Enthusiast
Enthusiast
Posts: 183
Joined: Sun Apr 27, 2003 8:39 pm
Location: Cornwall UK

Post by MikeB »

rsts wrote:
MikeB wrote:Perhaps I'm missing something, but wouldn't it be easier to just put a shortcut to your program in “C:\Documents and Settings\All Users\Start Menu\Programs\Startup\” or substitute a given user for “All Users”. :)
Except on my system it would be "r:" and on my wife's "e:", etc. And "r:" is not where I install programs, so it's not even the executable path. :)

This seemed to be just as easy and works fine.

cheers
I don't see what difference that would make, you put the shortcut wherever the “Documents and Settings” is and make it to wherever the program is. My startup is in “C:\” and the programs are usually in “F:\”, always works.
Mike.
(I'm never going to catch up with the improvements to this program)
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

MikeB wrote:
rsts wrote:
MikeB wrote:Perhaps I'm missing something, but wouldn't it be easier to just put a shortcut to your program in “C:\Documents and Settings\All Users\Start Menu\Programs\Startup\” or substitute a given user for “All Users”. :)
Except on my system it would be "r:" and on my wife's "e:", etc. And "r:" is not where I install programs, so it's not even the executable path. :)

This seemed to be just as easy and works fine.

cheers
I don't see what difference that would make, you put the shortcut wherever the “Documents and Settings” is and make it to wherever the program is. My startup is in “C:\” and the programs are usually in “F:\”, always works.
Well, I guess I'm missing something because I can't see how one way is easier than the other :?

cheers
SoulReaper
Enthusiast
Enthusiast
Posts: 372
Joined: Sun Apr 03, 2005 2:14 am
Location: England

Post by SoulReaper »

Just done some looking and you can find which drive windows is installed too and program files by doing the following... :)

Code: Select all

ProgramFiles.s=Space(#MAX_PATH) 
GetEnvironmentVariable_("PROGRAMFILES", ProgramFiles.s, #MAX_PATH)
Debug ProgramFiles.s

Windows_Location.s=Space(#MAX_PATH) 
GetEnvironmentVariable_("WINDIR", Windows_Location.s, #MAX_PATH)
Debug Windows_Location.s
Regards
Kevin :wink:
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 »

For PB4 a bit shorter :wink:

Code: Select all

Debug GetEnvironmentVariable("WINDIR")
Debug GetEnvironmentVariable("PROGRAMFILES")
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
SoulReaper
Enthusiast
Enthusiast
Posts: 372
Joined: Sun Apr 03, 2005 2:14 am
Location: England

Post by SoulReaper »

@ts-soft

Thankyou for the Pointer :wink: :lol:
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

You may also need

Code: Select all

Procedure.s GetSpecialFolderPath(CSIDL.l)
  Protected Path.s = Space(#MAX_PATH), Library = OpenLibrary(#PB_Any,"shell32.dll")
  If Library
   CallFunctionFast(GetFunction(Library,"SHGetSpecialFolderPathA"),0,@Path,CSIDL,0)
   CloseLibrary(Library)
  EndIf
  ProcedureReturn Path
EndProcedure
since it will work with all the #CSIDL_ constants.
I like logic, hence I dislike humans but love computers.
Post Reply