Calling a exe from another exe?

Blitz3D Forums/Blitz3D Programming/Calling a exe from another exe?

Happy Sammy(Posted 2006) [#1]
Hi all,

Assuming exe1 and exe2 are executable files compiled by
Blitz3D:

(1) Is it possible to call a exe2 from another exe1?
After exe2 is finished, it returns controls to exe1 and continue running. (similar to procedure/function)

(2)If possible, could we return some results from exe2
to exe1 ?

Thanks in advance.
Sammy
:)


Beaker(Posted 2006) [#2]
(1) Yes, you can call an exe from another exe.
You can't tell whether the exe has finished, unless you use a userlib to do this.

(2) See 2nd point above.


Picklesworth(Posted 2006) [#3]
Here is a userlib to do it:
http://www.blitzbasic.com/toolbox/toolbox.php?tool=151
Edit: Okay, it's updated.


You can use the Windows API to send WM_COPYDATA messages between your two programs. (Just send program 2 your window handle through the command line, and things should be sorted).

If you need help with that Windows API stuff, feel free to ask.
A search in these forums should reveal everything you need, though -- I know that there is at least one very big post about it. (By me). Then there's the person from whom I learned it, who is also on these very forums...


Pongo(Posted 2006) [#4]
You could return results by simply writing a small data file, and then reading it in when the 1st .exe is run.


Happy Sammy(Posted 2006) [#5]
Thanks a lot.
I will try it.

Sammy
:)


jfk EO-11110(Posted 2006) [#6]
there's a problem when exe 1 is running in fullscreen mode. Running exe 2 will minimize exe 1. While minimized the execution of exe 1 is paused. After terminating exe2 exe 1 will not be maximised automaticly, but the user needs to click it on the taskbar.

Exe 2 may re-maximize exe 1 before it ends using Api commands, but it still needs to give focus to exe 1, where the common api commands didn't seem to work, so I had to emulate a mouseclick - needless to say, a criminal dirty hack.

A better solution is welcome.


Picklesworth(Posted 2006) [#7]
It is possible, using the source of the RunProgram DLL (the link that I posted above), to change the properties of an application's window before it is launched.
Note that sometimes this does not work with various attributes (for example, launching Notepad with an altered title doesn't work, because it changes back to the filename after the program boots). However, it is able to work in many other cases, such as with cmd.exe.
There is a pretty good chance, though, that a Blitz program would not work with this.
...Although you could possibly experiment with not calling the Graphics command, which may stop it from ever setting its own window properties. We'd probably have to ask Mark about that, though.

Thus, it could be possible to set a WS_MINIMIZED, or maybe WS_HIDDEN property (sorry if those are the wrong flags, I'm too lazy to look them up) to the newly created application to avoid it minimizing the other window.


jfk EO-11110(Posted 2006) [#8]
Thanks Mr. Picklesworth. So let's see if the Runprogramm dll can prevent the fullscreen app from freezing. There must be ways since some tasks are running in the background, like the firewall etc.


Picklesworth(Posted 2006) [#9]
If it works, here's a bit of encouragement that will last forever:
Your app is now undeniably smarter than AVG Antivirus.


John Blackledge(Posted 2006) [#10]
Just a bit more info for those thinking of doing this... On my last gfx card causing a full-screen blitz exe to lose focus/minimize would cause the exe to mav - every time. But not my current card.
So please don't assume that coz you can do it then it's ok to do it. You might get some stern complaints from some users.
The only tidy way to do this is for exe 1 to be windowed in the first place.


Picklesworth(Posted 2006) [#11]
Is it possible to start windowed, boot exe 2, then go to fullscreen?


jfk EO-11110(Posted 2006) [#12]
If it MAVed in that case, your system probably wasn't 100% stabile. It's the first time I hear of this. Sounds more like a bug in the cards drivers to me.

Chanching from windowd to fullscreen or from fullscreen to windowed is no option IMHO, because it requires to reload all graphics.

For Apps that never use fullscreen this isn't an issue.


Happy Sammy(Posted 2006) [#13]
Hi all,

I test with the following programs:

exe1.bb:
Graphics3D 800,600
SetBuffer BackBuffer() 
camera=CreateCamera() 
light=CreateLight() 
RotateEntity light,90,0,0 

; Create red sphere 
sphere1=CreateSphere() 
PositionEntity sphere1,0,0,20 
EntityColor sphere1,255,0,0
While Not KeyDown( 1 ) 
	TurnEntity sphere1, 0,0.5,0.5
	RenderWorld 
	Flip 
Wend 

ExecFile("exe2.exe")

; Create green sphere 
sphere2=CreateSphere() 
PositionEntity sphere2,0,10,20
EntityColor sphere2,0,255,0
While Not KeyDown( 1 ) 
	TurnEntity sphere1, 0,0.5,0.5
	TurnEntity sphere2, 0,0.5,0.5
	RenderWorld 
	Flip 
Wend 
End 


exe2.bb:
Graphics3D 800,600
SetBuffer BackBuffer() 
camera=CreateCamera() 
light=CreateLight() 
RotateEntity light,90,0,0 

; Create cube 
cube=CreateCube() 
PositionEntity cube,0,0,5 
While Not KeyDown( 1 ) 
	TurnEntity cube, 0,0.5,0.5
	RenderWorld 
	Flip 
Wend 
End  


Originally, I use api as mentioned and get MAV.
Now, I use execfile and MAV go away.
However, I encounter two problems:
1. After exe2 finish running,
it goes back to exe1 but exe1 was MINIMIZED!
2. When exe1 switch to exe2 or exe2 back to exe1,
the screen flash (because windows maximize and minimize)
and this switching could be seen.

Could we prevent these two problems?

edit:
I have tried "Real Maximize of B3D Window by KuRiX"
http://www.blitzbasic.com/codearcs/codearcs.php?code=1370
Adding the 4 statements after execfile("exe2.exe"), it
does not work.

Thanks in advance.
Sammy
:)


John Blackledge(Posted 2006) [#14]
"Could we prevent these two problems?"
- No, because this is how Windows handles full-screen programs.

And as I said earlier, you're lucky if that happens - my last graphics card used to just crash.

Basically, if you're going to need to call another exe you must do it from a windowed app.


Picklesworth(Posted 2006) [#15]
Don't be so pessimistic, John. Of course it's possible!
How do you think all those background processes work? Windows is too big for something simple like this to be impossible :)

Of course, such a thing may need to be written in a different language... but there could be a way.

Have you tried using a startup window cleanup utility to remove the little window that pops up when Blitz starts?
Since exe2 is running in the background, I can safely assume that you don't need to call any graphics commands, so Graphics does not need to be called. In that case, it will (hopefully) never create a window. I have succcessfully created an invisible app before with Blitz, but it's gone now (complete accident, actually) and I don't know how it treats fullscreen apps.



May I ask why you need two exes?
There is a chance that such a thing is unnecessary. Besides, with Blitz3d, if there is anything much going on in the other program they'll both run incredibly slowly since the language has a very system hogging nature.


Happy Sammy(Posted 2006) [#16]
Hi all,

I just want to protect the source codes of all exe.
eg. some exes are unchanged, some will be updated.
(Usually, in other language, main exe unchanged,
only dll will be updated, download time is lesser.
Not all users have broadband connection.
As Blitz3D could not build dll, so exe is used instead,
each exe have to use Blitz3D commands.)

(1) Any more ideas that could achieve the same purpose?
(may be better than exe1 call exe2) :)

(2) BTW, I once come across on internet, there exists
a Blitz3D wrapper (dll) that could be called by
C language. That author want to use the advantages
of Blitz3D and c language, instead of building the
engine from scratch. I lost that link.
Do you think this will help???

Thanks in advance
Sammy
:)


John Blackledge(Posted 2006) [#17]
Sorry Mr Pick - yes you're right. I didn't realise that he needed a background task.
As you say, if it's handled externally.... well, your the man for that.


jfk EO-11110(Posted 2006) [#18]
there was a small hack for the runtime.dll.
http://www.blitzbasic.com/Community/posts.php?topic=37093#407848

(bot builders source, about in the middle of the thread)
But be careful, save a copy of the original runtime.dll


Picklesworth(Posted 2006) [#19]
Here is an archive with a compiled version of BotBuilder's program... featuring a file dialogue :)
http://crumbsoftware.f2o.org/files/StartWindowRemover.zip


jfk EO-11110(Posted 2006) [#20]
BTW: Is it still working with Blitz3D 1.96? Thought there where some changes in the linker.


Picklesworth(Posted 2006) [#21]
Now that you mention it...
I just tested it... don't think it does :(

Oh well. It was fun while it lasted :P

Looks like we'll need to try something else unless you're okay with compiling using an older version of Blitz.


Happy Sammy(Posted 2006) [#22]
Hi all,

I found the link...
http://www.ezblitz.com/sub_index.htm
It allows Blitz3d to be used in a C++ application.

Sammy
:)