Link to home
Start Free TrialLog in
Avatar of dwatling
dwatling

asked on

CreateProcess / GetExitCodeProcess cross-platform issues

I've written a program that executes a list of applications asychronously. These applications are both Windows- and DOS-based. To terminate these programs after they've finished I use GetExitCodeProcess and loop until the return isn't equal to STILL_ACTIVE. This method works for the DOS-based applications on the development platform (WinNT), but it doesn't work for the Windows-based applications. However, that isn't my problem. When I try to run my application on a Windows 95/98 machine, the above method doesn't work. In other words, all of the programs require the user to close them before the processing will continue.

I know if I add a "command.com /c " to the start of the string that is passed to CreateProcess it would solve my problems. But, it would also introduce another one since I'm executing both Windows and DOS applications. I also know that I could use SHGetFileInfo to determine the type of executable and add more code to fix the problems I would encounter. I'd prefer not to do it this way though.

My real question is this. How can I most easily determine if a DOS application has finished executing on both Windows NT and 95/98? What would be even better is if there were a way to determine if both DOS- and Windows-based applications have reached their end.
Avatar of Madshi
Madshi

Normally you should use CreateProcess, start all programs in their own CONSOLE (there's a parameter in CreateProcess for this purpose). Then you should simply call WaitForMultipleObjects with all the process handles. This should work perfectly for win9x and winNT, for dos and windows apps. At least I think so...   :-)

Regards, Madshi.
Avatar of dwatling

ASKER

Here's the code I'm using to determine if a process is running or not.

bool processRunning(void *id)
{
    bool retval = false;
    unsigned long exitcode;
    int ret;

    if (id == 0) return false;

    ret = WaitForSingleObject(id,INFINITE);

    if (ret == WAIT_TIMEOUT)
        retval = true;
    else {
        GetExitCodeProcess(id,&exitcode);

        if (exitcode == STILL_ACTIVE)
            retval = true;
    }

    return retval;
}

When I use WaitForSingleObject it locks up the parent until its child terminates. I don't want that to happen since I want the ability to cancel the child process at any time. If I replace INFINITE with a number, say 100, I still don't get the results I want. After compiling the program on NT and transfering it over to my Windows 95 machine, the DOS applications still won't terminate on their own.
Hi dwatling.


When I use WaitForSingleObject it locks up the parent until its child terminates. I don't want that to happen since I want the ability to cancel the child process at any time

What you do is you can use a flag say child_dead to indicate whether your child is existed or not.
Now run the following code inside a separate thread instead of main flow.

make child_dead =0 initially.

void processRunning(void *id)
{

    unsigned long exitcode;
    int ret;

    if (id == 0) return false;
   

    ret = WaitForSingleObject(id,INFINITE);
   
   child_dead=1;

}

Please llok at help of CreateThread() or _beginthreadex for more info about how to execute this function inside a thread. Now whenever you want to check the status of your child, check the value of child_dead variable.

If you want more info get back.
I tried tossing processRunning in its own separate thread, but it didn't work. It would still lock the parent application and the DOS apps on the Windows 95 machine wouldn't terminate.

ASKER CERTIFIED SOLUTION
Avatar of SamHobbs
SamHobbs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial