# Quick Question

## Recommended Posts

I'm writing a front end for a project i'm working on and need to launch programs from it. I totaly do not remember howto do this(or know how to do this, as long as this doesn't start a flame war). So can anyone please post on howto run executables in windows and/or linux? THANKS,

##### Share on other sites
See function:

ShellExecute

that's for windows by the way. linky

As for linux, I dunno, but maybe the "system()" function?
[/edit]

##### Share on other sites
try to use ShellExecuteEx on win32, as this allows you to get
alot more flexible information on the created process
(saves alot of hassel later on)

##### Share on other sites
For the sake of completeness, if you are trying to accomplish this under UNIX based operating systems the function you are most likely looking for is:

int execve(const char *path, char *const argv[], char *const envp[]);

located in unistd.h

regards.

##### Share on other sites
actually you would probably want to use spawn.. instead of exec.., exec replaces the current progran, spawn creates a new process. Both work on windows too.

##### Share on other sites
thank you all for answering this helped a great deal ^_^

##### Share on other sites
Quote:
 Original post by twanvlactually you would probably want to use spawn.. instead of exec.., exec replaces the current progran, spawn creates a new process. Both work on windows too.

Under windows ShellExecute/ShellExecuteEx can be used to start processes, however CreateProcess gives much more fine grained control.

Execve will do what he needs to do, however he must note that it will not return to the original calling process as it replaces the current process image with a new one. In order to mitigate this to create a multi-use application launcher you would have to fork the process before using execve.

e.g.:

#include <unistd.h>int main(int argc, char **argv){        int x;        /* you need to provide the parameters to pass to the         * application you are executing.  The first parameter     * should be the name of the application you are execve'ing     * and corresponds to argv[0] in the new process.     */        char *c[] = { "ls", "-l", 0 };        /* you can also provide an environment as follows:     * I have included sample values based on my systems     */        char *e[] = { "HOME=/cluster5/home/yboily", "HOST=cluster1", 0 };        x = fork();        /* fork is wierd; it returns "twice"; when you are writing your     * code it is important to process the return value for fork     * a return value of -1 indicates that an error occured     * a return value of 0 indicates that you are in the child process     * a return value other than this is the PID of the original process     *     * what we want to do is execve in the child process to launch our     * new process while leaving the orginal intact.     */        switch(x)        {                case -1:                        printf("error, unable to fork process!\n");                        break;                case 0:                        printf("This is the forked process!\n");                        execve ("/bin/ls", c, e);                        /* should not be reached */                        printf("Error executing process!\n");                        break;                default:                        printf("This is the original process!\n");        }}

This results in the following:

Script started on Thu Sep  2 11:30:43 2004yboily@yboily:~/demo> gcc -o exectest test.cyboily@yboily:~/demo> ./exectestThis is the original process!This is the forked process!yboily@yboily:~/demo> total 20-rwxr-xr-x  1 yboily  yboily  5122 Sep  2 11:30 exectest-rw-r--r--  1 yboily  yboily  1320 Sep  2 11:26 test.c-rw-r--r--  1 yboily  yboily    43 Sep  2 11:30 typescriptyboily@yboily:~/demo> exitexitScript done on Thu Sep  2 11:31:01 2004

For the record, I was unable to find documentation on spawn() as a means to create a process under Win32 Platform SDK, the FreeBSD man pages, or Red Hat Linux man pages

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628305
• Total Posts
2981967

• 9
• 13
• 11
• 12
• 11