  uses CoreWin

  'BOOL WINAPI CreatePipe(
  '  __out     PHANDLE hReadPipe,
  '  __out     PHANDLE hWritePipe,
  '  __in_opt  LPSECURITY_ATTRIBUTES lpPipeAttributes,
  '  __in      DWORD nSize
  ');
  '
  '
  'BOOL WINAPI SetHandleInformation(
  '  __in  HANDLE hObject,
  '  __in  DWORD dwMask,
  '  __in  DWORD dwFlags
  ');


  sys hChildStd_IN_Rd
  sys hChildStd_IN_Wr
  sys hChildStd_OUT_Rd
  sys hChildStd_OUT_Wr



  sub CreateChildProcess()
  '=======================
 
  string              szCmdline="child"
  PROCESS_INFORMATION pInfo
  STARTUPINFO         sInfo
  bool ok
 
  sInfo.cb         = sizeof STARTUPINFO 
  sInfo.hStdError  = hChildStd_OUT_Wr
  sInfo.hStdOutput = hChildStd_OUT_Wr
  sInfo.hStdInput  = hChildStd_IN_Rd
  sInfo.dwFlags    = STARTF_USESTDHANDLES
 

  'Create the child process.

  string Cmd="IOChild.exe"

  ok = CreateProcess(null, 
    strptr cmd, 'command line 
    null,       'process security attributes 
    null,       'primary thread security attributes 
    TRUE,       ' handles are inherited 
    0,          'creation flags 
    null,       'use parent's environment 
    null,       'use parent's current directory 
    @sinfo,     'STARTUPINFO pointer 
    @pinfo)     'receives PROCESS_INFORMATION 


   int er
   if not ok
      print "CreateProcess" : er=1 : exit sub
   else 
      'Close handles to the child process and its primary thread.
      'Some applications might keep these handles to monitor the status
      'of the child process, for example. 

      CloseHandle pinfo.hProcess
      CloseHandle pinfo.hThread
   end if
  end sub


  function main()  as sys
  '======================
    '
    SECURITY_ATTRIBUTES sa
    '
    sa.nLength = sizeof SECURITY_ATTRIBUTES
    sa.bInheritHandle = TRUE
    '
    sys ok,dwRead,dwWritten
    '
    ok=CreatePipe(@hChildStd_OUT_Rd, @hChildStd_OUT_Wr, @sa, 0) 
    ok=SetHandleInformation (hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0) 

    CreatePipe(@hChildStd_IN_Rd, @hChildStd_IN_Wr, @sa, 0)
    SetHandleInformation(hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0)
    '
    CreateChildProcess

    string OutBuf=nuls 16000
    string InBuf =nuls 16000

    'WRITE TO PIPE
    '=============
    '
    mid outbuf,1,"Greetings from parent process"
    ok=WriteFile(hChildStd_IN_Wr, strptr OutBuf,30 , @dwWritten, null)
    CloseHandle(hChildStd_IN_Wr) 
    '
    'READ FROM PIPE
    '==============
    '
    ok=ReadFile(hChildStd_OUT_Rd, strptr InBuf, len(InBuf), @dwRead, null)
    CloseHandle(hChildStd_OUT_Rd) 
    '
    print left InBuf,dwRead
    '
  end function

  main
