View Full Version : Executing external program
Isengard
07-30-2002, 06:49 AM
What I want to do is this :
When a presses "yes" in program A, I want program B, say Opera ( The webbrowser ) to execute. What would the code to get this done look like ? Preferrably code that can be used in both Linux and Windows.
( No .. I`m not writing a virus ;) )
l01yuk
07-30-2002, 09:16 AM
The call you want is system(), prototype is:
#include<stdlib.h>
int system(const char *string);
It is a standard c function so it should be completely portable, (assuming you have a complient compiler).
Bradmont
07-30-2002, 03:33 PM
it won't be totally portable if you provide the whole path to the executable, though, since different platforms use different formats for paths.
Bradmont
07-30-2002, 03:43 PM
Why did I just post something so blatantly obvious?
Strike
07-30-2002, 07:49 PM
Why did you just post a reply to your own post?
Isengard
07-31-2002, 03:40 AM
:) I know .. I`m using this in a tiny project of mine, where I will develop the program to run both under windows and Linux, and yes I do know path`s are different ;)
l01yuk
07-31-2002, 04:50 AM
That is where you need to coditionally compile the program. I don't think you could get away with just naming the program in windows since it doesn'tuse a path in such a comprehensive way. Always a bit dodgy to do that anyway.
Something like this:
#ifdef _ISWINDOWS
system(..
#endif
#ifdef _ISLINUX
system(..
#endif
Well, if you're going to be using macros to separate one platform from another, you might as well use the win32 api for windows... in that case, you can use:
CreateProcess(
LPCSTR lpApplicationName,
LPSTR lpCommandLine,
LPSECURITY_ATTRIBUTES
lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCSTR lpCurrentDirectory,
LPSTARTUPINFOA lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
);
you can also use the following if you need it.
TerminateProcess(
HANDLE hProcess,
UINT uExitCode
);
stuka
08-01-2002, 11:30 AM
And if ya still wanna use system(), just do something like this:#ifdef WIN32
#define EXT_PROG "C:\\Program Files\\Foo.exe"
#else
#define EXT_PROG "/bin/foo"
#endif
system(EXT_PROG)Just cuz it's cleaner.
Isengard
08-04-2002, 07:53 AM
Thanks for the reply folks! :plot:
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.