I'm writing a small app to do automated testing of a console application. It needs to repeatedly run the app with various options and handle when the app being tested crashes. I started with system(), moved on to spawn() and have since been trying CreateProcess(). It doesn't lockup when the process crashes but I can't seem to kill the process. How do I kill the process or is there a different approach I should consider? ExitProcess() seems to be the recommended function but it didn't work.

Thanks,

Greg

Windows XP
TinyCC

#include <windows.h>
#include <stdio.h>

int main(void) {

  int retCode = 0;
  PROCESS_INFORMATION pi;        /* filled in by CreateProcess */
  STARTUPINFO si;                /* startup info for the new process */

  /* print out our process ID */
  printf("Process %d reporting for duty\n",GetCurrentProcessId());

  /* Get startup info for current process, we will use this
     as the startup info for the new process as well... */
  GetStartupInfo(&si);

  /* Call CreateProcess, telling it to run DivideByZero
     with lots of defaults... (the NULLs mean "use defaults")
  */

  CreateProcess(NULL,          /* lpApplicationName */
                "DivideByZero.exe", /* lpCommandLine */
                NULL,          /* lpsaProcess */
                NULL,          /* lpsaThread */
                FALSE,         /* bInheritHandles */
                DETACHED_PROCESS, /* dwCreationFlags */
                NULL,          /* lpEnvironment */
                NULL,          /* lpCurDir */
                &si,           /* lpStartupInfo */
                &pi            /* lpProcInfo */
                );


  printf("New Process ID: %d\n",pi.dwProcessId);

  getc(stdin); //Pause here to check task manager etc...

  retCode = ExitProcess(pi.dwProcessId);

  printf("Returned %d\n\n",retCode);

  return(0);
}


//DivideByZero

#include <stdio.h>

int main(void)
{
        int result = 0;

        printf("DivideByZero is about to crash\n");

        result = 42/result;

        printf("Result: %d\n", result);

    return(0);
}

I'm assuming that you want to kill the parent process. In that case try the following:

#include <windows.h>
#include <stdio.h>

int main(void) {

    int retCode = 0;
    PROCESS_INFORMATION pi;        /* filled in by CreateProcess */
    STARTUPINFO si;                /* startup info for the new process */

    /* print out our process ID */
    printf("Process %d reporting for duty\n",GetCurrentProcessId());

    /* Get startup info for current process, we will use this
       as the startup info for the new process as well... */
    GetStartupInfo(&si);

    /* Call CreateProcess, telling it to run DivideByZero
       with lots of defaults... (the NULLs mean "use defaults")
    */

    CreateProcess(NULL,          /* lpApplicationName */
                  "DivideByZero.exe", /* lpCommandLine */
                  NULL,          /* lpsaProcess */
                  NULL,          /* lpsaThread */
                  FALSE,         /* bInheritHandles */
                  DETACHED_PROCESS, /* dwCreationFlags */
                  NULL,          /* lpEnvironment */
                  NULL,          /* lpCurDir */
                  &si,           /* lpStartupInfo */
                  &pi            /* lpProcInfo */
                 );


    printf("New Process ID: %d\n",pi.dwProcessId);
    int iRet = WaitForSingleObject( pi.hProcess, INFINITE );
    printf("The WaitForSingleObject() return value is 0X%.8X\n", iRet);
    if(CloseHandle(pi.hProcess) != 0)
        printf("pi.hProcess handle was closed...\n");
    else
        printf("CloseHandle(pi.hProcess) failed\n");

    if(CloseHandle(pi.hThread) != 0)
        printf("pi.hThread handle was closed...\n");
    else
        printf("CloseHandle(pi.hThread) failed\n");

    return(0);
}

Bob,

Thanks! That works great! To wrap this up I have to humbly ask for a vocabulary lesson. I thought the process my code is running in would be considered the parent and the process it creates to run DivideByZero would be the child. That is why I asked about killing the child process. How should I be looking at it?

Greg

Be a part of the DaniWeb community

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