| xHarbour Reference Documentation > Function Reference |  | 
|  |  |  | 
Opens a child process.
HB_OpenProcess( <xExeFile>   , ;
              [@<nStdIN>]    , ;
              [@<nStdOUT>]   , ;
              [@<nStdERR>]   , ;
              [<lBackGround>], ;
              [@<nProcessID>   ) --> nProcessHandle
The function returns the process handle (similar to a file handle) of the new child process as a numeric value, or -1 if an error occurs. Use function FError() to identify the cause of failure.
Function HB_OpenProcess() opens a new child process and executes the program <cExeFile> in the new process. The program is executed synchronously (<lBackGround>==.T.) or asynchronously (<lBackGround>==.F.).
When the parent process needs to exchange data with the child process, it can use the standard handlers STDIN, STDOUT and STDERR. Numeric handles for them are assigned to the reference parameters <nStdIN>, <nStdOUT> and <nStdERR>.
The parent process can wait for the end of a synchronously started child process with function HB_ProcessValue(), or it can terminate the child process explicitely with function HB_CloseProcess().
When the child process is complete, its process handle and the standard handles, if obtained, must be closed with FCLose().
| See also: | HB_CloseProcess(), HB_ProcessValue(), RUN, StartThread() | 
| Category: | Process functions , xHarbour extensions | 
| Source: | rtl\philes.c | 
| LIB: | xhb.lib | 
| DLL: | xhbdll.dll | 
Parent process
// This example implements a parent process launching a child process
// (it is shown in the next example).
   PROCEDURE Main()
      LOCAL cData  := "Hello World"
      LOCAL cEXE   := "ChildProc.exe"
      LOCAL nBytes, nChild, nError, nStdIN, nStdOUT, nStdERR
      CLS
      ? "Opening child process:", cEXE
      nChild := HB_OpenProcess( cEXE, @nStdIN, @nStdOUT, @nStdERR )
      IF nChild < 0
         ? "Error:", FError()
         QUIT
      ENDIF
      ? "Sending data         :", FWrite( nStdIN, cData )
      ? "Receiving data       : "
      cData := Space( 1000 )
      nBytes := Fread( nStdOUT, @cData, Len(cData) )
      ?? Left( cData, nBytes )
      ? "Reading errors       : "
      cData := Space( 1000 )
      nBytes := Fread( nStdERR, @cData, Len(cData) )
      ?? Left( cData, nBytes )
      ? "Waiting for end      :", HB_ProcessValue( nChild )
      FClose( nChild )
      FClose( nStdIN )
      FClose( nStdOUT )
      FClose( nStdERR )
   RETURN
Child process
// This example is the child process lauched by the first exemple.
// Both must be compiled and linked as separate EXE files.
   // definition of standard file handles available
   // in a child process:
   #define  FH_STDIN   0
   #define  FH_STDOUT  1
   #define  FH_STDERR  2
   #include "Fileio.ch"
   #define BUFF_SIZE   1024
   PROCEDURE Main
      LOCAL nBytes, cBuffer, cData, nError
      cBuffer := Space( BUFF_SIZE )
      nBytes  := FRead( FH_STDIN, @cBuffer, Len( cBuffer ) )
      cBuffer := "Child process received "    + ;
                        LTrim( Str( nBytes) ) + ;
                 " bytes [" + Trim( cBuffer ) + "]"
      nBytes  := FWrite( FH_STDOUT, cBuffer, Len(cBuffer) )
      nError  := FError()
      cBuffer := "Error code: " + LTrim( Str( nError ) )
      nBytes  := FWrite( FH_STDERR, cBuffer, Len(cBuffer) )
   RETURN
http://www.xHarbour.com