Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » shell to executable

Credits go to Elverion, Goalie Ca, kikabo, and nonnus29 for helping out!
This thread is locked; no one can reply to it. rss feed Print
shell to executable
BamBam
Member #7,499
July 2006

[center]I need my program to open a executable in the same directory.[/center]
How can i do this. I am running windows xp and
using mingw32 and allegro. Some source code would be
nice.

Thank you in advance...

kikabo
Member #3,679
July 2003
avatar

One way is to change to that directory and the execute the executable. There's probably a nicer way but this should work (I think) :-

char buf[200];

get_executable_name(buf, sizeof(buf));   // get the app full path
replace_filename(buf, buf, "", sizeof(buf)); // strip everything but the path
chdir(buf);
system("my.exe"); // or shell_exec("my.exe");

edit:
or just

replace_filename(buf, buf, "my.exe", sizeof(buf));
system(buf);

but you probably want the chdir to read other relative filenames

nonnus29
Member #2,606
August 2002
avatar

This is at least the fourth time I've posted this, I wonder what everyones working on.... :P

Elverion
Member #6,239
September 2005
avatar

Or #include <windows.h> and do this:

ShellExecute(NULL, "open", "c:/path_to_exe/program.exe", NULL, "c:/path_to_execute_from/", SW_SHOWNORMAL);

You should use get_executable_name for the full path (3rd argument), and a combination of get_executable_name and replace_filename for the path to execute from (5th argument) to work as I expect you want it to. See kikabo's post for that.

You could also use WinExec("path", SW_SHOWNORMAL) if you do not need it to be executed from the path. I still recommend ShellExecute, though.

--
SolarStrike Software - MicroMacro home - Automation software.

Goalie Ca
Member #2,579
July 2002
avatar

#include <stdlib.h>

int main(int argc, char** argv)
{
 if (argc != 2)
    return EXIT_FAILURE;

  system(argv[1]) ; //file name to execute (relative to the "working"directory aka this directory...
 // see also exec() etc. 
}

-------------
Bah weep granah weep nini bong!

BamBam
Member #7,499
July 2006

Many thanks to you all for your help.
Problem solved...

Go to: