Hello Friendz,

Suppose I have a program like

// A.cpp
int main()
{
     if(some_condition)
          return 1;
     
     return 0;
}

Now from another program I want to run the above program, like

//B.cpp
int main()
{
     ...
     ...
     system("A");
     ...
     ...
     return 0;
}

Here how can I catch the return value of program A from the program B.


Amit

Recommended Answers

What OS? If Windows try CreateProcess() followed by GetExitCodeProcess():

STARTUPINFO si;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    PROCESS_INFORMATION pi;
    BOOL bRes = CreateProcess(
        NULL, const_cast<LPSTR>(commandLine), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
    if (bRes) 
    {
        WaitForSingleObject(pi.hProcess, INFINITE);
        DWORD dwExitCode;
        GetExitCodeProcess(pi.hProcess, &dwExitCode);
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread ); …
Jump to Post

check your man pages for spawn family of functions.

Jump to Post

link and see wait4(). I haven't used these in over 20 years, so you will have to test them out to see how they work.

Jump to Post

All 9 Replies

Member Avatar

What OS? If Windows try CreateProcess() followed by GetExitCodeProcess():

STARTUPINFO si;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    PROCESS_INFORMATION pi;
    BOOL bRes = CreateProcess(
        NULL, const_cast<LPSTR>(commandLine), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
    if (bRes) 
    {
        WaitForSingleObject(pi.hProcess, INFINITE);
        DWORD dwExitCode;
        GetExitCodeProcess(pi.hProcess, &dwExitCode);
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
    }

What OS? If Windows try CreateProcess() followed by GetExitCodeProcess():

I am using Solaris.

check your man pages for spawn family of functions.

check your man pages for spawn family of functions.

Thanx Dragon.
But there is no manual entry for spawn family of functions.

-bash-3.00$ man spawnl
No manual entry for spawnl.
-bash-3.00$

Can u pls suggest some other way to do it.

Amit

link and see wait4(). I haven't used these in over 20 years, so you will have to test them out to see how they work.

> system("A");
You may be in luck. Some implementations do the right thing with result = system("A"); The POSIX way would be to use fork(), execl() and wait(), something like

if ( fork() == 0 ) {
  execl()  // run A
} else {
  wait() // wait for A, and get the exit status
}

You'll have to read the manual pages for those functions for actual usage.

> system("A");
You may be in luck. Some implementations do the right thing with result = system("A");

Hi Salem,
In my system it is giving 256 times of the actual result.
I am using Studio 11 in Solaris 10 environment.
Can u pls explain it.

Thanx,
Amit

You mean the manual page for system() DOESN'T explain it?

This is one of those perennial questions...

system() returns the exit code of the command shell (probably bash --or if your system is really old, sh [check your /usr/bin/sh to see if it is a link to bash or not if you want to know]).
Only if your libc was made in an abnormal way will system() execute the program directly.

I've answered this before with a little module. (Alas, it is on another forum.)
Duoas's ExecProcess class
Post size is limited there, so keep scrolling for both files.

If you are new to C++ in general the following link may also be good reading.
Call external program in C++ (Linux) by Hannahlv

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.