//==================================================== file = process.c =====
//=  A simple bare-bones example of processes in Windows 9X/NT              =
//===========================================================================
//=  Notes:                                                                 =
//=    1) This program demonstrates how processes work                      =
//=    2) This program requires that hello.exe be in the same directory     =
//=    3) This program is only for Windows 9X/NT and compiles with both     =
//=       Borland and Microsoft compilers                                   =
//=    4) Key Win32 API data structures for this program are:               =
//=        - PROCESSINFORMATION                                             =
//=        - STARTUPINFO                                                    =
//=    5) Key Win32 API functions for this program are:                     =
//=        - CreateProcess()                                                =
//=        - GetExitCodeProcess()                                           =
//=        - OpenProcess()                                                  =
//=        - TerminateProcess()                                             =
//=-------------------------------------------------------------------------=
//= Example execution:                                                      =
//=                                                                         =
//=   Fork a process...                                                     =
//=     >>> The process global id is -184597                                =
//=     Press any key to kill the process...                                =
//=       Hello World - - - iteration #1                                    =
//=       Hello World - - - iteration #2                                    =
//=       Hello World - - - iteration #3                                    =
//=       Hello World - - - iteration #4                                    =
//=       Hello World - - - iteration #5                                    =
//=                                                                         =
//=     Process killed                                                      =
//=   End!                                                                  =
//=                                                                         =
//=-------------------------------------------------------------------------=
//=  Build: bcc32 process.c                                                 =
//=-------------------------------------------------------------------------=
//=  Execute: process (with hello.exe in the same directory)                =
//=-------------------------------------------------------------------------=
//=  Author: Kenneth J. Christensen                                         =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: AM/KJC (12/08/98) - Genesis                                   =
//=           AM/KJC (02/24/99) - Changed "%ld" to "%ud" in printf()        =
//===========================================================================

//----- Include files -------------------------------------------------------
#include <stdio.h>        // Needed for printf()
#include <windows.h>      // Needed for Sleep() and Win32 API stuff

//----- Defines -------------------------------------------------------------
#define FILE_NAME  "hello.exe"    // Name of program to execute as a process
#define ATTEMPTS           10     // Number of times to try to get ExitCode
#define ATTEMPT_DELAY     100     // Delay in millisec for try to get ExitCode

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main()
{
  PROCESS_INFORMATION ProcInfo;    // Structure for PROCESS_INFORMATION
  STARTUPINFO         StartUpInfo; // Structure for STARTUPINFO
  DWORD               dwResult;    // A double world result variable
  HANDLE              Proc_handle; // Handle for process
  int                 i;           // Loop counter

  // Initialize STARTUPINFO structure
  StartUpInfo.cb = sizeof(STARTUPINFO);

  // Output a banner
  printf("Forking a process... \n");

  // Create the process
  if (!CreateProcess(NULL, FILE_NAME, NULL, NULL, FALSE,
                     CREATE_DEFAULT_ERROR_MODE, NULL, NULL,
                     &StartUpInfo, &ProcInfo))
  {
    printf("*** ERROR - Unable to create the process \n");
    return;
  }

  // Output the process global id
  printf("  >>> The process global id is %lu \n", ProcInfo.dwProcessId);

  // Loop ATTEMPTS time waiting for a STILL_ACTIVE ExitCode
  for(i=0; i<ATTEMPTS; i++)
  {
    GetExitCodeProcess(ProcInfo.hProcess, &dwResult);
    if(dwResult != STILL_ACTIVE)
      Sleep(ATTEMPT_DELAY);
    else
      break;
  }

  // Output an error message if forked process is never found to be active
  if (dwResult != STILL_ACTIVE)
  {
    printf("  *** ERROR - Forked process not active");
    return;
  }

  // Let the forked process run until a key is pressed to continue
  printf("  Press any key to kill the process... \n") ;
  getchar();

  // Get the forked process handle and terminate it
  Proc_handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcInfo.dwProcessId);
  if(!TerminateProcess(Proc_handle, -1))
  {
    printf("  *** ERROR - Error in killing process");
    return;
  }
  printf("  Process killed \n" ) ;

  // All done
  printf("End! \n");
}

