home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / WINSPAWN.LIB < prev    next >
Text File  |  1995-03-28  |  3KB  |  78 lines

  1. //*** WinSpawn.lib - WinSpawn() routine to execute the spawn
  2. //*** ver.1          function in a Windows session under WINOS2
  3. //***                that is running WinSpawn.cmm from CEnvi2
  4. //***                for Windows.
  5. //***
  6. //*** WinSpawn(): Spawn from WinSpawn.cmm windows session
  7. // SYNTAX: WinSpawn(PipeSpec,Action,Command)
  8. // WHERE: PipeSpec: unique name that WinSpawn.cmm was started with
  9. //        Action: Action to perform for WinSpawn, may be the following
  10.             #define WIN_SPAWN       0  // spawn command and return immediately
  11.             #define WIN_SPAWN_WAIT  1  // spawn and wait until command is finished
  12.             #define WIN_SPAWN_EXIT  2  // spawn and then exit WinSpawn.cmm
  13. //        Command: text string command to perform.  If zero-length or NULL
  14. //                 then perform no command
  15. // RETURN: Return results from Windows spawn call; -1 for error
  16. // NOTE: This function may not work on some versions of OS/2 2.1
  17. //       with the service pack XR200 applied. Refer to APAR PJ13427
  18. //       at IBM Phone 1-800-992-4777 regarding the fix.
  19. //
  20.  
  21. #include <FileIO.lib>
  22. #include <NamePipe.lib>
  23.  
  24. WinSpawn(pPipeSpec,pAction,pCommand)
  25. {
  26.    bool lSuccess = False;   // assume failure
  27.    sprintf(lFullPipeName,"\\PIPE\\%s",pPipeSpec);
  28.  
  29.    if ( !DosCreateNPipe(lFullPipeName,lPipeHandle,
  30.                         NP_ACCESS_DUPLEX | NP_NOINHERIT,
  31.                         NP_NOWAIT | NP_TYPE_BYTE | NP_UNLIMITED_INSTANCES | NP_READMODE_BYTE,
  32.                         4096, 4096, 0) ) {
  33.  
  34.       // Give the Windows program up to 30 seconds to open the file
  35.       for ( lStart = time(); difftime(time(),lStart) < 30; ) {
  36.          suspend(100);
  37.          if ( !DosConnectNPipe(lPipeHandle) ) {
  38.             lSuccess = True;
  39.             break;
  40.          }
  41.       }
  42.       if ( lSuccess ) {
  43.          lSuccess = False;
  44.  
  45.          // change pipe to blocking state to make sure reads and writes are
  46.          // finished
  47.          DosSetNPHState(lPipeHandle,NP_WAIT | NP_READMODE_BYTE);
  48.  
  49.          // if command is NULL then make zero-length command
  50.          lCommand = pCommand ? pCommand : "" ;
  51.  
  52.          // create big blob to send which contains the Action, the length
  53.          // of command, and then the command
  54.          BLObPut(lSendBLOb,pAction,UWORD32);
  55.          BLObPut(lSendBLOb,1+strlen(lCommand),UWORD32);
  56.          BLObPut(lSendBLOb,lCommand,1+strlen(lCommand));
  57.  
  58.          // write this buffer to the pipe for WinSpawn.cmm to read
  59.          if ( !DosWrite(lPipeHandle,lSendBLOb,BLObSize(lSendBLOb),lBytesSent)
  60.            && lBytesSent == BLObSize(lSendBLOb) ) {
  61.  
  62.             // Windows WinSpawn.cmm will signal it is finished by writing to
  63.             // the named pipe; so when we return code it is done
  64.             BLObPut(lReceiveBLOb,-1,SWORD32);
  65.             if ( !DosRead(lPipeHandle,lReceiveBLOb,4,lBytesRead)
  66.               && 4 == lBytesRead ) {
  67.                lSuccess = True;
  68.                lSpawnReturn = BLObGet(lReceiveBLOb,0,SWORD32);
  69.             }
  70.          }
  71.          DosDisconnectNPipe(lPipeHandle);
  72.       }
  73.       DosClose(lPipeHandle);
  74.    }
  75.    return( lSuccess ? lSpawnReturn : -1 );
  76. }
  77.  
  78.