Page 1 of 1

Start your program with windows

Posted: Mon Mar 06, 2006 7:44 pm
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

Posted: Mon Jul 10, 2006 6:30 am
by rsts
Needed something like this and here it is :D

Works fine.

Thanks for the submission.

cheers

Posted: Mon Jul 10, 2006 7:25 am
by jqn
Hi,
Download "Autoruns" from http://www.sysinternals.com/Utilities/Autoruns.html

and see some others REG KEYS for auto-start.

Posted: Mon Jul 10, 2006 10:58 am
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”. :)

Posted: Mon Jul 10, 2006 11:37 am
by Henrik
#HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\RunServicesOnce"

make your program be one of the first programs running after windows start.

Posted: Mon Jul 10, 2006 11:45 am
by netmaestro
That's good work JC, thanks for sharing it!

Posted: Mon Jul 10, 2006 1:59 pm
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

Posted: Mon Jul 10, 2006 6:32 pm
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.

Posted: Mon Jul 10, 2006 7:23 pm
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

Posted: Fri Sep 22, 2006 5:49 pm
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:

Posted: Fri Sep 22, 2006 6:04 pm
by ts-soft
For PB4 a bit shorter :wink:

Code: Select all

Debug GetEnvironmentVariable("WINDIR")
Debug GetEnvironmentVariable("PROGRAMFILES")

Posted: Fri Sep 22, 2006 6:40 pm
by SoulReaper
@ts-soft

Thankyou for the Pointer :wink: :lol:

Posted: Fri Sep 22, 2006 9:24 pm
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.