RunProgramAtStartup

Share your advanced PureBasic knowledge/code with the community.
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

RunProgramAtStartup

Post by Droopy »

Easy Add/Delete software @ Windows Startup

Code: Select all

; WichUser = 1 : All Users / 0 Current User
; AddOrDel = 1 : Add the "Software" in the Run Key / 0 Remove
; Return 1 if success / 0 if fail

Procedure RunProgramAtStartup(WichUser.l,AddOrDel.l,Software.s)

  File.s=GetFilePart(Software)
  Extension.s=GetExtensionPart(File)
  Cle.s=Mid(File,1,Len(File)-Len(Extension)-1)
  
  
  If WichUser=1
    Key.s="HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run"
  Else
    Key="HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run"
  EndIf
  
  If AddOrDel
    Retour=RegSetValue(Key,Cle,Software,#REG_SZ ,".")
  Else
    Retour=RegDeleteValue(Key,Cle,".")
  EndIf

  ProcedureReturn Retour
EndProcedure

;/ Test
; Add Regedit for all user @ Startup
Debug RunProgramAtStartup(1,1,"c:\windows\regedit.exe")
You need the Droopy lib
User avatar
HeX0R
Addict
Addict
Posts: 1205
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Post by HeX0R »

I made something similar a while ago, but as some of us aren't friends of Userlibs (like me), this is without any need of them.

If this was meant to advertise your lib Droopy, i appologize for "hijacking" your thread and will delete my code asap!

Code: Select all

;What is #Source_Path ?
;  While testing your code,
;  the path will be ..../purebasic/compilers/
;  instead of the real path, so ExePath() would
;  return a wrong value!
;  If you never test your code, forgot about it
;
;  Example:
#Source_Path = "C:\Program Files\Purebasic\My_Own_Codes\"

Procedure.s ExePath(Mode.b)
  ;Mode = 0 returns the Path to the current Exe
  ;Mode = 1 returns the Filename
  Protected ExePath.s
  Protected ExeName.s
  
  Static ExePath.s
  Static ExeName.s
  
  If ExePath = ""
    ExePath = Space(1000)
    GetModuleFileName_(0, @ExePath, 1000)
    ExeName = GetFilePart(ExePath)
    ExePath = GetPathPart(ExePath)
    If Right(ExePath, 11) = "\Compilers\"
      ExePath = #Source_Path
    EndIf
  EndIf
  
  If Mode
    ProcedureReturn ExeName
  EndIf
  ProcedureReturn ExePath
  
EndProcedure

Procedure.b SetWinStart(Mode.b, lpValueName.s)
  ;Mode:
  ;Bit 0 = 1 -> SET RegKey
  ;Bit 0 = 0 -> DEL RegKey
  ;Bit 1 = 1 -> For All Users
  ;Bit 1 = 0 -> For Current User
  
  ;-----------------------------
  If Mode & 2
    Handle.l = RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", 0, #KEY_ALL_ACCESS, @hKey.l)
  Else
    Handle.l = RegOpenKeyEx_(#HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", 0, #KEY_ALL_ACCESS, @hKey.l)
  EndIf
  Result.b = #False
  If Handle = #ERROR_SUCCESS
    If Mode & 1
      lpData.s = ExePath(0) + ExePath(1)
      Handle   = RegSetValueEx_(hKey, @lpValueName, 0, #REG_SZ, @lpData, Len(lpData) + 1)
    Else
      Handle = RegDeleteValue_(hKey, @lpValueName)
    EndIf
    If Handle = #ERROR_SUCCESS
      Result = #True
    EndIf
  EndIf
  RegCloseKey_(hKey)
  ProcedureReturn Result
EndProcedure

;Debug SetWinStart(3, "Test") ;<- Bit 0 = 1 -> SET Key  Bit 1 = 1 -> For All Users
;Debug SetWinStart(2, "Test") ;<- Bit 0 = 0 -> DEL Key  Bit 1 = 1 -> For All Users
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

Ah, nice non-libbed
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Nice code HeX0R :D
User avatar
jqn
User
User
Posts: 97
Joined: Fri Oct 31, 2003 3:04 pm

Post by jqn »

2Droopy
Nice piece!.

Code: Select all

  If WichUser=1
    Key.s="HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run"
  Else
    Key="HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run"
  EndIf 
see others Reg Locations with:

http://www.sysinternals.com/utilities/autoruns.html

(free software)

JOAQUIN
Post Reply