Make Autorun selfprogram on Windows...

Just starting out? Need help? Post your questions and find answers here.
arma
User
User
Posts: 57
Joined: Sun Jul 24, 2016 11:54 pm

Make Autorun selfprogram on Windows...

Post 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!
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Make Autorun selfprogram on Windows...

Post by skywalk »

Search Windows Service. Or task scheduler automation.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
AZJIO
Addict
Addict
Posts: 1319
Joined: Sun May 14, 2017 1:48 am

Re: Make Autorun selfprogram on Windows...

Post 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
arma
User
User
Posts: 57
Joined: Sun Jul 24, 2016 11:54 pm

Re: Make Autorun selfprogram on Windows...

Post 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
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Make Autorun selfprogram on Windows...

Post by infratec »

I think you need to run cmd and your mklink as parameter :wink:
arma
User
User
Posts: 57
Joined: Sun Jul 24, 2016 11:54 pm

Re: Make Autorun selfprogram on Windows...

Post 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 :(
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Make Autorun selfprogram on Windows...

Post by infratec »

As I told you already:

You need to run cmd:

Code: Select all

RunProgram("cmd", "/c mklink ...")
arma
User
User
Posts: 57
Joined: Sun Jul 24, 2016 11:54 pm

Re: Make Autorun selfprogram on Windows...

Post by arma »

Thanks a LOT!
Solved...
Thank you again...
Axolotl
Enthusiast
Enthusiast
Posts: 435
Joined: Wed Dec 31, 2008 3:36 pm

Re: Make Autorun selfprogram on Windows...

Post by Axolotl »

Maybe this could be helpful
viewtopic.php?f=7&t=45791
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home
Post Reply