Page 1 of 1

Make Autorun selfprogram on Windows...

Posted: Tue Jul 27, 2021 12:02 am
by arma
Hello everybody;

I am looking purebasic code to make autorun application it self... After restart on Windows... Any body knows any sample?

Thanks a LOT!

Re: Make Autorun selfprogram on Windows...

Posted: Tue Jul 27, 2021 12:51 am
by skywalk
Search Windows Service. Or task scheduler automation.

Re: Make Autorun selfprogram on Windows...

Posted: Wed Jul 28, 2021 5:46 am
by AZJIO
I made an analog of my function (insufficiently tested)

Code: Select all

EnableExplicit

; https://www.purebasic.fr/english/viewtopic.php?p=61476#p61476
XIncludeFile "CreateLink.pbi"
; Protected result = 0, hres
; Protected ShellLink.IShellLinkA
; Protected PersistFile.IPersistFile

; https://www.purebasic.fr/english/viewtopic.php?f=12&t=56204
XIncludeFile "Registry.pbi"

UseModule registry

Declare StartUp(flag = 0, sName$ = "", sPathRun$ = "", sArgs$ = "", sDesc$ = "", sIcon$ = "", iIconNum = 0, state = #SW_SHOWNORMAL)

StartUp(0, "test", "C:\Windows\system32\calc.exe", "", "", "C:\Windows\system32\calc.exe", 0) ; Shortcut
StartUp(3, "test", "C:\Windows\system32\calc.exe") ; Registry

Procedure StartUp(flag = 0, sName$ = "", sPathRun$ = "", sArgs$ = "", sDesc$ = "", sIcon$ = "", iIconNum = 0, state = #SW_SHOWNORMAL)
	Protected iPos$, sKeyName$, iRes$, sPathLnk$, HK
	If Not Asc(sPathRun$)
		sPathRun$ = ProgramFilename()
	EndIf
	If Not Asc(sName$)
		sName$ =GetFilePart(ProgramFilename(), #PB_FileSystem_NoExtension)
	EndIf
	If flag & 1
		If flag & 2
			HK = #HKEY_LOCAL_MACHINE
		Else
			HK = #HKEY_CURRENT_USER
		EndIf
; 		If #OSArch = "X64"
; 			sKeyName$ + "64"
; 		EndIf
		sKeyName$ + "\Software\Microsoft\Windows\CurrentVersion\Run"
		If flag & 4
			sKeyName$ + "Once"
		ElseIf flag & 8
			sKeyName$ + "OnceEx"
		EndIf
		If flag & 32
			ProcedureReturn DeleteKey(HK, sKeyName$ + "\" + sName$)
		EndIf
		If ReadValue(HK, sKeyName$, sName$) And flag & 16 ; если существует и флаг 16, т.е. не перезаписывать, то выпрыг
			ProcedureReturn 0
		EndIf
		If sArgs$
			sArgs$ = " " + sArgs$
		EndIf
; 		iRes$ = RegWrite(sKeyName$, sName$, "REG_SZ", """ + sPathRun$ + """ + sArgs$)
		If WriteValue(HK, sKeyName$, sName$, Chr(34) + sPathRun$ + Chr(34) + sArgs$, #REG_SZ)
			ProcedureReturn 1
		EndIf
	Else
		If flag & 2
			sPathLnk$ = GetUserDirectory(#PB_Directory_AllUserData) + "Microsoft\Windows\Start Menu\Programs\StartUp\" + sName$ + ".lnk"
		Else
			sPathLnk$ = GetUserDirectory(#PB_Directory_ProgramData) + "Microsoft\Windows\Start Menu\Programs\Startup\" + sName$ + ".lnk"
		EndIf
		If flag & 32
			ProcedureReturn DeleteFile(sPathLnk$)
		EndIf
		If flag & 16 And FileSize(sPathLnk$) > 0
			ProcedureReturn 1
		EndIf
		ProcedureReturn CreateLink(sPathRun$, sPathLnk$, sArgs$, sDesc$, "", state, 0, sIcon$, iIconNum)
	EndIf
EndProcedure

Re: Make Autorun selfprogram on Windows...

Posted: Fri Aug 06, 2021 9:22 pm
by arma
I found very easy way... It works on command prompt, but in the purebasic code :( It doesnt work :(
Anybody can find the problem on this code? :( Doesint work :(

a$="%allusersprofile%\Microsoft\Windows\"+Chr(34)+"Start Menu"+Chr(34)+"\Programs\StartUp\test.exe C:\Users\Purebasic\AppData\Local\Temp\PureBasic_Compilation0.exe"
RunProgram("mklink",a$,"")

This code doesnt work :( But if i use this code on below; on command prompt... This works fine... What is the wrong? :(

mklink %allusersprofile%\Microsoft\Windows\"Start Menu"\Programs\StartUp\test.exe C:\Users\Purebasic\AppData\Local\Temp\PureBasic_Compilation0.exe

Re: Make Autorun selfprogram on Windows...

Posted: Fri Aug 06, 2021 9:31 pm
by infratec
I think you need to run cmd and your mklink as parameter :wink:

Re: Make Autorun selfprogram on Windows...

Posted: Fri Aug 06, 2021 9:50 pm
by arma
I did try :( still doesnt work :(
if i crate .bat file... And if i put the same code inside... Then double click the .bat file... it works fine on .bat file... But doesnt work on purebasic code :(
I couldnt find the problem :(

Re: Make Autorun selfprogram on Windows...

Posted: Fri Aug 06, 2021 10:10 pm
by infratec
As I told you already:

You need to run cmd:

Code: Select all

RunProgram("cmd", "/c mklink ...")

Re: Make Autorun selfprogram on Windows...

Posted: Fri Aug 06, 2021 10:12 pm
by arma
Thanks a LOT!
Solved...
Thank you again...

Re: Make Autorun selfprogram on Windows...

Posted: Fri Aug 06, 2021 10:28 pm
by Axolotl
Maybe this could be helpful
viewtopic.php?f=7&t=45791