I know two ways of doing it. Which one would be the best or are there more?
- Automatically place a shortcut in the autostart folder that points to the current executable
- Create entry in the registry that points to the current executable
Code: Select all
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
Code: Select all
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Code: Select all
Procedure StartWithWindows(State.b) ; by Joakim Christiansen
Protected Key.l = #HKEY_CURRENT_USER ;or #HKEY_LOCAL_MACHINE for every user on the machine
Protected Path.s = "Software\Microsoft\Windows\CurrentVersion\Run"
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
Yes, that is the intention of this thread. I'm asking which method is best for a portable application.ts-soft wrote:A Portable App should not use the registry! There is no uninstaller.
Code: Select all
Procedure CreateShellLink(PATH$, LINK$, Argument$, DESCRIPTION$, WorkingDirectory$, ShowCommand.l, HotKey.l, IconFile$, IconIndexInFile.l)
Protected psl.IShellLinkA,ppf.IPersistFile,*mem,len,hres,res.s
CoInitialize_(0)
If CoCreateInstance_(?CLSID_ShellLink,0,1,?IID_IShellLink,@psl.IShellLinkA) = 0
;Set_ShellLink_preferences:
psl\SetPath(PATH$)
psl\SetArguments(Argument$)
psl\SetWorkingDirectory(WorkingDirectory$)
psl\SetDescription(DESCRIPTION$)
psl\SetShowCmd(ShowCommand)
psl\SetHotkey(HotKey)
psl\SetIconLocation(IconFile$, IconIndexInFile)
;ShellLink_SAVE:
If psl\QueryInterface(?IID_IPersistFile,@ppf.IPersistFile) = 0
*mem = AllocateMemory(1000)
len = MultiByteToWideChar_(#CP_ACP, 0, LINK$, -1, *mem, 1000)
res.s = PeekS(*mem,len,#PB_Unicode)
hres = ppf\Save(res,#True)
FreeMemory(*mem)
ppf\Release()
EndIf
psl\Release()
EndIf
CoUninitialize_()
ProcedureReturn hres ! 1
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_IShellLink:
; 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_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
Procedure.s GetSpecialFolderLocation(Value.l)
Protected Folder_ID,SpecialFolderLocation.s
If SHGetSpecialFolderLocation_(0, Value, @Folder_ID) = 0
SpecialFolderLocation = Space(#MAX_PATH*2)
SHGetPathFromIDList_(Folder_ID, @SpecialFolderLocation)
If SpecialFolderLocation
If Right(SpecialFolderLocation, 1) <> "\"
SpecialFolderLocation + "\"
EndIf
EndIf
CoTaskMemFree_(Folder_ID)
EndIf
ProcedureReturn SpecialFolderLocation.s
EndProcedure
#CSIDL_STARTUP = $7
#CSIDL_APPDATA = $1A
Procedure AddtoStartMenu()
Protected tpath.s = GetSpecialFolderLocation(#CSIDL_STARTUP) + "MyApp.lnk"
CreateShellLink(ProgramFilename(),tpath,"","MyApp","",0,0,ProgramFilename(),0)
EndProcedure
I was about to post the same code here but when I tested it it wouldn't work on the XP SP3 computer I used.idle wrote:To add to the start up menu.
Code: Select all
Macro DEFINE_GUID(Name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
CompilerIf Defined(Name, #PB_Variable)
If SizeOf(Name) = SizeOf(GUID)
Name\Data1 = l
Name\Data2 = w1
Name\Data3 = w2
Name\Data4[0] = b1
Name\Data4[1] = b2
Name\Data4[2] = b3
Name\Data4[3] = b4
Name\Data4[4] = b5
Name\Data4[5] = b6
Name\Data4[6] = b7
Name\Data4[7] = b8
EndIf
CompilerEndIf
EndMacro
Procedure ShortcutCreate(Path.s, Link.s, WorkingDir.s="", Argument.s="", ShowCommand=#SW_SHOWNORMAL, Description.s="", HotKey=#Null, IconFile.s="|", IconIndex=0)
; Path: "C:\PureBasic\purebasic.exe"
; Link: "C:\Documents and Settings\User\Desktop\PureBasic.lnk"
; WorkingDir: "C:\PureBasic\"
; Argument: "%1"
; ShowCommand: #SW_SHOWNORMAL, #SW_SHOWMAXIMIZED or #SW_SHOWMINIMIZED
; Description: "Start PureBasic"
; HotKey: Shortcut of keys for the link
; IconFile: "C:\PureBasic\purebasic.exe"
; IconIndex: 0
Protected CLSID_ShellLink.GUID, IID_IShellLink.GUID, IID_IPersistFile.GUID
CompilerIf #PB_Compiler_Unicode
Protected psl.IShellLinkW
CompilerElse
Protected psl.IShellLinkA
CompilerEndIf
Protected ppf.IPersistFile
Protected Result = #False
DEFINE_GUID(CLSID_ShellLink, $00021401, $0000, $0000, $C0, $00, $00, $00, $00, $00, $00, $46)
CompilerIf #PB_Compiler_Unicode
DEFINE_GUID(IID_IShellLink, $000214F9, $0000, $0000, $C0, $00, $00, $00, $00, $00, $00, $46)
CompilerElse
DEFINE_GUID(IID_IShellLink, $000214EE, $0000, $0000, $C0, $00, $00, $00, $00, $00, $00, $46)
CompilerEndIf
DEFINE_GUID(IID_IPersistFile, $0000010B, $0000, $0000, $C0, $00, $00, $00, $00, $00, $00, $46)
If Len(WorkingDir) = 0 : WorkingDir = GetPathPart(Path) : EndIf
If IconFile = "|" : IconFile = Path : EndIf
CoInitialize_(0)
If CoCreateInstance_(@CLSID_ShellLink, 0, 1, @IID_IShellLink, @psl) = #S_OK
With psl
\SetPath(Path)
\SetArguments(Argument)
\SetWorkingDirectory(WorkingDir)
\SetDescription(Description)
\SetShowCmd(ShowCommand)
\SetHotkey(HotKey)
\SetIconLocation(IconFile, IconIndex)
EndWith
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
EndProcedure
No I didn't test it as posted, my VM wasn't running at the time.Joakim Christiansen wrote:I was about to post the same code here but when I tested it it wouldn't work on the XP SP3 computer I used.idle wrote:To add to the start up menu.![]()
Did you test it? (you probably did)
Code: Select all
Procedure MakePortable(MyApp.s)
strm.s = "Select the USB drive to make " + MyApp + " portable on"
MessageRequester(MyApp,strm)
dir.s = PathRequester(strm,"*.*")
If dir <> ""
CopyFile(MyApp + ".exe",dir+ MyApp + ".exe")
fn = CreateFile(#PB_Any,dir+"autorun.inf")
If fn
WriteData(fn,?auto,?eauto-?auto)
CloseFile(fn)
EndIf
fn = CreateFile(#PB_Any,dir+MyApp+".ico")
If fn
WriteData(fn,?ico,?eico-?ico)
CloseFile(fn)
SetFileAttributes(dir+MyApp+".ico",#PB_FileSystem_Hidden)
EndIf
EndIf
DataSection
Ico: IncludeBinary "icon.ico"
eIco:
Auto:IncludeBinary "autorun.inf"
eAuto:
EndDataSection
EndProcedure
;Save as autorun.inf
;[autorun]
;OPEN=MyApp.exe
;ICON=MyApp.ico
;ACTION=MyApp
;LABEL=MyApp