how to start an exe with arguments?

BlitzMax Forums/BlitzMax Programming/how to start an exe with arguments?

Midimaster(Posted 2011) [#1]
Could someone please help me?

How can I manage to start a second exe with sending arguments and close the first exe?

I want to build an auto update function in my programs. the function will first fetch its own update as a file from my server, then restart.

That the way i planned to do it: "Game.Exe" downloads the update to the harddisk as "game.bin". Then it starts "Restart.Exe". This copies "game.bin" to "game.exe" and starts again "game.Exe". I need this for Win-7 and OsX.

my first atempt:
BlitzMax:
'inside game.exe:
	Function NewStart()
		Local process:TProcess=CreateProcess("Restart.exe  -game.Exe")
		TProcess.ProcessList.Remove process
	End Function


with this the "Restart.Exe" starts and the "game.Exe" quits, but I get no arguments in "Restart.Exe" an. What do I do wrong?

Last edited 2011


GfK(Posted 2011) [#2]
Try:
	Function NewStart()
		Local process:TProcess=CreateProcess("~qRestart.exe  -game.Exe~q")
		TProcess.ProcessList.Remove process
	End Function


You could also try OpenURL() instead of CreateProcess().

Last edited 2011


Midimaster(Posted 2011) [#3]
thank you for the quick answer. I will check this immediately. I did already try the OpenURL()... But with following arguments it did not open the EXE, but the Browser...?!? Perhaps the same behavior, because of the <"> ?

*** EDIT ****

...CreateProcess:
Sorry... no solution for me: got "EXCEPTION_ACCESS_VIOLATION".

...OpenURL()
Sorry... also no solution: no starting of the RESTART.EXE

with this it will start:
OpenURL (AppDir + "/Restart.exe")


but with this the broswer starts:
OpenURL (AppDir + "/Restart.exe -abc")


Last edited 2011


jsp(Posted 2011) [#4]
I doubt you can use TProcess to start the Restart.exe as TProcess terminates the child process when the main program quits and thus killing Restart.exe

Anyway, do you really need to feed the new name into the Restart.exe? Isn't the downloaded file not enough to know what to do with it?


Midimaster(Posted 2011) [#5]
I found this code (in post #1) in the german forum and I also was suprised , that it really stopps the game.exe and not the restart.exe!

I also test the System_() function, but also here I get no arguments:

here ist the restart.exe:
SuperStrict
Graphics 300,200

Print "Number of Args:" + AppArgs.length

For Local cmd:String = EachIn AppArgs
      Print "ARGS: !" + cmd + "!"
Next

Print AppDir
'...





GfK(Posted 2011) [#6]
Maybe you could try executing a batch file?


Rixarn(Posted 2011) [#7]
I didn't succeed to start an exe file from another one and close the second, but I did managed to send arguments to the second exe file by using the System_() Function:

system_("yourfile.exe -" + arg1+ " " + arg2 + " " + arg3)


But actually your little piece of code works like a charm :) I'll have a use for it too... This is how i did it:

'parent.bmx
SuperStrict
Local process:TProcess = CreateProcess("child.exe arg1 arg2 arg3")
TProcess.ProcessList.Remove process


'child.bmx
SuperStrict
Local args:String
For Local s:String = EachIn AppArgs
	args:+s + "~n"
Next

Notify args
End


If you run this you'll get the Notify window with each of the arguments passed down to the child, and amazingly the parent is no longer in memory.. nice!

Haven't tested it in the mac, but the system_() call I did in the Mac Version for my program was

system_("open yourfile.app --args arg1 arg2 arg3"


Last edited 2011

Last edited 2011


Midimaster(Posted 2011) [#8]
yes, i noticed too, that it already works! Someone in the german forum found the reason for the problem I had first:

It seems to be a bug in BMax: If you use a PRINT command in the receivers code, it will not work in combination with a commandline coming from a sender using CreateProcess(). strange....

thanks to all!


this code works:

sender:
Function NewStart()
	' calls RECEIVER.EXE and sends Parameters 
	Local process:TProcess = CreateProcess("receiver.exe -abc")
	
	' forget the binding to RECEIVER.EXE"
	If process Then
		TProcess.ProcessList.Remove process
	EndIf
End Function


receiver:
NumberOfArguments = AppArgs.length
DrawText NumberOfArguments,0,Y
Y:+15
For Local arg:String = EachIn AppArgs
	DrawText arg,0,Y
	Y:+15
Next



Sub_Zero(Posted 2011) [#9]
For linux:
Local process:TProcess = CreateProcess("./receiver.exe -abc", HIDECONSOLE)


Last edited 2011