How to Use Named Pipes in a Basic Protected-Mode OS/2 Program

ID: Q69796


The information in this article applies to:
  • Microsoft Basic Professional Development System (PDS) versions 7.00 and 7.10 for MS OS/2


SUMMARY

Microsoft Basic Professional Development System (PDS) versions 7.00 and 7.10 can create OS/2 protected mode programs that use named pipes. Named pipes are a form of OS/2 interprocess communication (IPC). Using named pipes involves calling several different OS/2 API functions. Below are two sample Basic programs that demonstrate the use of named pipes.

This information applies to Microsoft Basic Professional Development System (PDS) versions 7.00 and 7.10 for MS OS/2.


MORE INFORMATION

Named pipes can be used to communicate between OS/2 processes. Pipe names have the following form:

\PIPE\name.ext
The ".ext" is optional. Typically, one process (called the "server") creates the named pipe, and other programs (called "clients") will open the pipe to exchange data. The following is the minimum set of OS/2 API functions that you need to call in order to access named pipes:

For the Server:

   Function                Description
   --------                -----------
   DosMakeNmPipe%()        Create the named pipe.
   DosConnectNmPipe%()     Make named pipe available for connections.
   DosDisConnectNmPipe%()  Make named pipe unavailable for connections. 
For the Client:

   Function                Description
   --------                -----------
   DosOpen%()              Used to connect to the named pipe from the
client. 
For Both the Server and Clients:

   Function                Description
   --------                -----------
   DosRead% ()             Used to retrieve data from the named pipe.
   DosWrite% ()            Used to place data in the named pipe.
   DosClose% ()            Used to close or disconnect the named pipe. 
For more information on named pipes and other IPC methods, see chapter 13, "Interprocess Communication," of "Advanced OS/2 Programming" by Ray Duncan (Microsoft Press, 1989) or the "Microsoft OS/2 Programmer's Reference" volume 1, chapter 51, "Interprocess Communication" (Microsoft Press, 1989).

The following are two sample programs that demonstrate the use of named pipes. To compile the programs, enter the following commands at the command prompt:

   bc  /o /Lp server;
   bc  /o /Lp client; 
When linking the programs you must include the OS/2 API functions import library. In Basic PDS 7.00 and 7.10, the name of the library is OS2.LIB. The link lines are as follows:

   link server,,,os2;
   link client,,,os2; 

NAMEPIPE.BI


DECLARE FUNCTION DosClose% (BYVAL P1%)
DECLARE FUNCTION DosConnectNmPipe% (BYVAL P1%)
DECLARE FUNCTION DosDisConnectNmPipe% (BYVAL P1%)
DECLARE FUNCTION DosMakeNmPipe% (BYVAL P1s%, BYVAL P1o%, SEG P2%,_
           BYVAL P3%, BYVAL P4%, BYVAL P5%, BYVAL p6%, BYVAL P7&)
DECLARE FUNCTION DosOpen% (BYVAL P1s%, BYVAL P1o%, SEG P2%, SEG P3%,_
           BYVAL P4&, BYVAL P5%, BYVAL p6%, BYVAL P7%, BYVAL P8&)
DECLARE FUNCTION DosRead% (BYVAL P1%, BYVAL P2s%, BYVAL P2o%,_
           BYVAL P3%, SEG P4%)
DECLARE FUNCTION DosWrite% (BYVAL P1%, BYVAL P2s%, BYVAL P2o%,_
           BYVAL P3%, SEG P4%)

' Named pipe attributes and flags
CONST PIPEACCESSINBOUND = 0
CONST PIPEACCESSOUTBOUND = 1
CONST PIPEACCESSDUPLEX = 2
CONST PIPEINHERIT = 0
CONST PIPENOINHERIT = &H80
CONST PIPEWRITEBEHIND = 0
CONST PIPENOWRITEBEHIND = &H4000
CONST PIPEWAIT = 0
CONST PIPENOWAIT = &H8000
CONST PIPEUNLIMITEDINSTANCES = &HFF
CONST PIPEREADMODEMESSAGE = &H100
CONST PIPEREADMODEBYTE = 0
CONST PIPETYPEMESSAGE = &H400
CONST PIPETYPEBYTE = 0
CONST ERRORPIPENOTCONNECTED = 233

' DosOpen attribute
CONST FILENORMAL = &H0

' DosOpen action
CONST FILEEXISTED = 1

' DosOpen open flag
CONST FILEOPEN = &H1

' DosOpen mode flags
CONST OPENACCESSREADWRITE = &H2
CONST OPENSHAREDENYNONE = &H40 

SERVER.BAS


'$INCLUDE: 'NAMEPIPE.BI'
DIM SendStr AS STRING * 30, receiveStr AS STRING * 30
connectPipeHP% = 0
connectPipe$ = "\pipe\connect" + CHR$(0)
SendStr = "Hello from SERVER"
PRINT "SERVER: Creating named pipe."
bool% = DosMakeNmPipe(SSEG(connectPipe$),_
                      SADD(connectPipe$),_
                      connectPipeHP%,_
                      PIPEACCESSDUPLEX,_
                      PIPENOWAIT OR PIPEUNLIMITEDINSTANCES,_
                      512, 512, 60000)
IF bool% THEN
  PRINT "SERVER: error creating named pipe."
  END
END IF
PRINT "SERVER: Waiting for client to connect."
bool% = ERRORPIPENOTCONNECTED
WHILE bool% = ERRORPIPENOTCONNECTED
  bool% = DosConnectNmPipe(connectPipeHP%)
  SLEEP 1
WEND
IF bool% THEN
  PRINT "SERVER: error connecting pipe."
  END
END IF

PRINT "SERVER: Writing to pipe."
bool% = DosWrite(connectPipeHP%, VARSEG(SendStr), VARPTR(SendStr),_
                 LEN(SendStr), numwrit%)
PRINT "SERVER SENDING: "; SendStr

PRINT "SERVER: Reading from pipe."
WHILE numRead% = 0
  bool% = DosRead(connectPipeHP%, VARSEG(receiveStr), VARPTR(receiveStr),_
                  LEN(receiveStr), numRead%)
WEND
PRINT "CLIENT RESPONSE: "; receiveStr

PRINT "SERVER: DIsconnecting pipe."
bool% = DosDisConnectNmPipe(connectPipeHP%)

PRINT "SERVER: Closing pipe."
bool% = DosClose(connectPipeHP%)

PRINT "SERVER: Program ended."
END 

CLIENT.BAS


'$INCLUDE: 'NAMEPIPE.BI'
DIM sendStr AS STRING * 30, receiveStr AS STRING * 30
connectPipeHP% = 0
connectPipe$ = "\pipe\connect" + CHR$(0)
sendStr = "Hello from CLIENT"
WHILE usAction% <> FILEEXISTED
  bool% = DosOpen(SSEG(connectPipe$), SADD(connectPipe$),_
                  connectPipeHP%, usAction%, 0&, FILENORMAL, FILEOPEN,_
                  OPENACCESSREADWRITE OR OPENSHAREDENYNONE, 0&)
WEND
IF bool% THEN
  PRINT "CLIENT: Error opening pipe."
  END
ELSE
  PRINT "CLIENT: Pipe open."
END IF

PRINT "CLIENT: reading from pipe."
bool% = DosRead(connectPipeHP%, VARSEG(receiveStr),_
                VARPTR(receiveStr), LEN(receiveStr), numread%)
PRINT "RECEIVED FROM SERVER: "; receiveStr

PRINT "CLIENT: Writing to pipes."
bool% = DosWrite(connectPipeHP%, VARSEG(sendStr), VARPTR(sendStr),_
                       LEN(sendStr), numwrit%)
PRINT "CLIENT REPLYING: "; sendStr

PRINT "CLIENT: Closing pipe."
bool% = DosClose(connectPipeHP%)
PRINT "CLIENT: done."
END 

Additional query words: BasicCom

Keywords :
Version :
Platform :
Issue type :


Last Reviewed: December 10, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.