home *** CD-ROM | disk | FTP | other *** search
- //*** WinSpawn.lib - WinSpawn() routine to execute the spawn
- //*** ver.1 function in a Windows session under WINOS2
- //*** that is running WinSpawn.cmm from CEnvi
- //*** for Windows.
- //***
- //*** WinSpawn(): Spawn from WinSpawn.cmm windows session
- // SYNTAX: WinSpawn(PipeSpec,Action,Command)
- // WHERE: PipeSpec: unique name that WinSpawn.cmm was started with
- // Action: Action to perform for WinSpawn, may be the following
- #define WIN_SPAWN 0 // spawn command and return immediately
- #define WIN_SPAWN_WAIT 1 // spawn and wait until command is finished
- #define WIN_SPAWN_EXIT 2 // spawn and then exit WinSpawn.cmm
- // Command: text string command to perform. If zero-length or NULL
- // then perform no command
- // RETURN: Return results from Windows spawn call; -1 for error
- // NOTE: This function may not work on some versions of OS/2 2.1
- // with the service pack XR200 applied. Refer to APAR PJ13427
- // at IBM Phone 1-800-992-4777 regarding the fix.
- //
-
- #include <FileIO.lib>
- #include <NamePipe.lib>
-
- WinSpawn(pPipeSpec,pAction,pCommand)
- {
- bool lSuccess = False; // assume failure
- sprintf(lFullPipeName,"\\PIPE\\%s",pPipeSpec);
-
- if ( !DosCreateNPipe(lFullPipeName,lPipeHandle,
- NP_ACCESS_DUPLEX | NP_NOINHERIT,
- NP_NOWAIT | NP_TYPE_BYTE | NP_UNLIMITED_INSTANCES | NP_READMODE_BYTE,
- 4096, 4096, 0) ) {
-
- // Give the Windows program up to 30 seconds to open the file
- for ( lStart = time(); difftime(time(),lStart) < 30; ) {
- suspend(100);
- if ( !DosConnectNPipe(lPipeHandle) ) {
- lSuccess = True;
- break;
- }
- }
- if ( lSuccess ) {
- lSuccess = False;
-
- // change pipe to blocking state to make sure reads and writes are
- // finished
- DosSetNPHState(lPipeHandle,NP_WAIT | NP_READMODE_BYTE);
-
- // if command is NULL then make zero-length command
- lCommand = pCommand ? pCommand : "" ;
-
- // create big blob to send which contains the Action, the length
- // of command, and then the command
- BLObPut(lSendBLOb,pAction,UWORD32);
- BLObPut(lSendBLOb,1+strlen(lCommand),UWORD32);
- BLObPut(lSendBLOb,lCommand,1+strlen(lCommand));
-
- // write this buffer to the pipe for WinSpawn.cmm to read
- if ( !DosWrite(lPipeHandle,lSendBLOb,BLObSize(lSendBLOb),lBytesSent)
- && lBytesSent == BLObSize(lSendBLOb) ) {
-
- // Windows WinSpawn.cmm will signal it is finished by writing to
- // the named pipe; so when we return code it is done
- BLObPut(lReceiveBLOb,-1,SWORD32);
- if ( !DosRead(lPipeHandle,lReceiveBLOb,4,lBytesRead)
- && 4 == lBytesRead ) {
- lSuccess = True;
- lSpawnReturn = BLObGet(lReceiveBLOb,0,SWORD32);
- }
- }
- DosDisconnectNPipe(lPipeHandle);
- }
- DosClose(lPipeHandle);
- }
- return( lSuccess ? lSpawnReturn : -1 );
- }
-