Calling an OS (Shell) command

BlitzMax Forums/BlitzMax Programming/Calling an OS (Shell) command

AarbronBeast(Posted 2005) [#1]
Hi all,

Is there a way in BlitzMax to call an operating system (shell) command? Such as opening Notepad/TextEdit, or running a BAT file or script?

I can' seem to find any Blitz command that does it. I know we can probably import some system library, but I'd like to find something that is platform independant.

Thanks.


BlitzSupport(Posted 2005) [#2]
This should be considered unofficial, but there is a command in pub/stdc.mod called system_ which does work on Windows, and probably on the other OSes too (can't check right now):

system_ "notepad.exe"


Should be easy enough to replace if Mark decides to add an official command of this nature. Something like the below would be even easier to replace, given that an official command may have separate executable path, parameters, launch flags, etc...

' Quotes added automatically...

Function RunProgram (exe$, param$)
	Return system_ ("~q" + exe$ + "~q" + " " + "~q" + param$ + "~q")
End Function

' Assuming BlitzMax is in "C:\Program Files" here...

RunProgram "notepad.exe", "C:\Program Files\BlitzMax\versions.html"



N(Posted 2005) [#3]
It should work on Windows and Linux as system() is available on both (as far as I know, considering system() worked with my Linux setup I'm inclined to believe I'm right). As for MacOS, that'll be up to someone who enjoys using a mac to find out.


AarbronBeast(Posted 2005) [#4]
Hey thanks! That does the trick. The following works on my Mac:

system_ "open /Applications/Calculator.app"



Mirko(Posted 2005) [#5]
system_ does only work with about 95% of windows applications.

This is becaus it uses an infinite wait and does not handle windows messages while waiting.
There is an article about that topic on msdn.

If you for example try to start Travelbook5 using system_ both applications, the bm app and travelbook will hang after a few seconds.

I don't know if that can be considered to be a bug, its just not 100% correct ;-)


AarbronBeast(Posted 2005) [#6]
Gotcha. Thanks for the warning. I wonder if there is a similar problem on Mac OS X.


Rambo_Bill(Posted 2005) [#7]
I've been doing this, don't know which is better.



AarbronBeast(Posted 2005) [#8]
Rambo_Bill, that's what I was referring to when I said "importing a library", but this is not platform independant (won't work on my Mac, for example). It probably also hangs the way Mirko says it might. By the way, which file do you import that your extern references?


Rambo_Bill(Posted 2005) [#9]
Uhm, that just works. I don't need to use any import. I have a WinAPI book and saw some other people who made API calls using the extern, so I tried it and it worked. I'm not sure how or why. It'd be great if I understood it better because I have some good stuff written using nothing but API calls that I could convert.

Alas, like most of Blitzmax stuff,it's undocumented.


Mirko(Posted 2005) [#10]
The problem is not how you call the program, it is how you wait for the programs end.

About 99% of all routines i found in the internet waited for the programs end with a infinite waitforsingleobject.
So about 99% of all routines do it like mark did it in his source.

I guess normally that is ok, as i really did find only one program (travelbook5) where it did not work.

I tried every possible solution to start an external programm using createprocess winexec, shellexecute, shellexecuteex etc.

Until i found out that the problem did not relay on the way you call the program but on the way you wait for it's end.

I finally wrote my own routine to call external programs which handles window messages and so it even works with travelbook5 ;-)

But as i don't care for multi-platform compatibility, as i use windows only, and have no intend to ever release one of my programs ;-), i did not make the code public, yet.

Mirko


Rambo_Bill(Posted 2005) [#11]
Actually, my preferred behavior for what I'm using it for would not wait. So I need to modify it to not wait.