How to run other programs with c++?

Hello.
How can I run other programs like *.exe program with c++?
Although I'll get scolded for saying this, you can use system() with the program's name to run it...
-scolds-
Don't need to use system().

I'm not so sure about the Windows way; I can work out roughly how to at least start a process in Windows. As for the UNIX way, I'm sure about that although it may not be the best.

Windows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
LPCTSTR lpApplicationName = "C:/Windows/System32/cmd.exe"; /* The program to be executed */

LPSTARTUPINFO lpStartupInfo;
LPPROCESS_INFORMATION lpProcessInfo;

memset(&lpStartupInfo, 0, sizeof(lpStartupInfo));
memset(&lpProcessInfo, 0, sizeof(lpProcessInfo));

CreateProcess(lpApplicationName,
              
/* Create the process */
if (!CreateProcess(lpApplicationName
                   NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL,
                   lpStartupInfo,
                   lpProcessInformation
                  )
   ) {
    std::cerr << "Uh-Oh! CreateProcess() failed to start program \"" << lpApplicationName << "\"\n";
    exit(1);
}

std::cout << "Started program \"" << lpApplicationName << "\" successfully\n";


http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx

UNIX:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
char* programPath = "/bin/bash";

pid_t pid = fork(); /* Create a child process */

switch (pid) {
    case -1: /* Error */
        std::cerr << "Uh-Oh! fork() failed.\n";
        exit(1);
    case 0: /* Child process */
        execl(programPath, NULL); /* Execute the program */
        std::cerr << "Uh-Oh! execl() failed!"; /* execl doesn't return unless there's an error */
        exit(1);
    default: /* Parent process */
        std::cout << "Process created with pid " << pid << "\n";
        int status;

        while (!WIFEXITED(status)) {
            waitpid(pid, status, 0); /* Wait for the process to complete */
        }

        std::cout << "Process exited with " << WEXITSTATUS(status) << "\n";

        return 0;
}


http://linux.die.net/man/3/fork
http://linux.die.net/man/3/exec
http://linux.die.net/man/3/waitpid

As you can see, the Linux/UNIX way is infinitely better takes less setting up and less lines of code. The windows example (which, admittedly, is an awful example) just starts the program, whereas the UNIX example starts the program and waits until it exits, then tells the user.
Last edited on
You made a couple of syntax errors in the Windows version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
LPCTSTR lpApplicationName = "C:/Windows/System32/cmd.exe"; /* The program to be executed */

LPSTARTUPINFO lpStartupInfo;
LPPROCESS_INFORMATION lpProcessInfo;

memset(&lpStartupInfo, 0, sizeof(lpStartupInfo));
memset(&lpProcessInfo, 0, sizeof(lpProcessInfo));
              
/* Create the process */
if (!CreateProcess(lpApplicationName,
                   NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL,
                   lpStartupInfo,
                   lpProcessInformation
                  )
   ) {
    std::cerr << "Uh-Oh! CreateProcess() failed to start program \"" << lpApplicationName << "\"\n";
    exit(1);
}

std::cout << "Started program \"" << lpApplicationName << "\" successfully\n";
Oh yeah :S

I should add, this question goes in the "Beginners" or "General C++" section.
Last edited on
I have to ask. Why you using memset() to zero pointers, and why are you passing zeroed pointers to a function?
And I'm not sure CreateProcess() allows its last two parameters to be zero.
I don't know, like I said, I tried my best to figure out what to do to give an example but I did say that there was a good chance it was wrong.

Are LPSTARTUPINFO and LPPROCESS_INFORMATION pointers? I assumed they were objects... I wanted to zero the contents of the structures; not the pointers that I didn't know existed.

I'd have skipped those parts but I thought you couldn't pass NULLs as the last two arguments like you said.
It's a convention used by the WinAPI. Anything that starts with LP is a pointer.
Ah. Well at least I'm reasonably confident my, albeit simple, UNIX example works...
Topic archived. No new replies allowed.