I'm trying to write a code where I call a number of programs, with each program's output being the input for the next program.

WinExec(run11, SW_SHOWNORMAL);       //to execute program1

WinExec (replace2, SW_SHOWNORMAL);  //to execute program2

Now the problem is, program2 starts running before program1 finishes, and therefore the input for program2 still isn't available. The obvious solution is to insert a delay, but program1 could take anywhere between 5 seconds and an hour to finish running. So is there a way to detect when the first program finishes running?



Note This function is provided only for compatibility with 16-bit Windows. Applications should use the CreateProcess function.

WinExec() is a cheap "fire and forget" approach.
What you should be using (namely CreateProcess) gives you a lot more control over what is happening, and provides you with the necessary information to wait until the called process finishes.

i think there is a win API function to see what programs are running but i cant remember. have you thought about multithreading instead?

Thanks for your suggestions!
I've been trying to work with CreateProcess but I can't seem to get it right.

LPTSTR run_t11 = _tcsdup(TEXT("C:\\run_t.exe /C:\\images C:\\ttt.exe -L -S")); 
//run_t.exe is the program I want to run. 
//C:\\images and C:\\ttt.exe are the arguments for it.  
    if( !CreateProcess( NULL, run_t11, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi )) 
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return 1;

The program I want to run run_t.exe starts but I immediately get an error "run_t.exe has stopped working" and I think it's a problem with the arguments, so does the format look right?