Microsoft KB Archive/69796

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

Article ID: 69796

Article Last Modified on 10/20/2003

-

APPLIES TO


 * Microsoft BASIC Professional Development System 7.1
 * Microsoft BASIC Professional Development System 7.0

-



This article was previously published under Q69796



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 &quot;.ext&quot; is optional. Typically, one process (called the &quot;server&quot;) creates the named pipe, and other programs (called &quot;clients&quot;) 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, &quot;Interprocess Communication,&quot; of &quot;Advanced OS/2 Programming&quot; by Ray Duncan (Microsoft Press, 1989) or the &quot;Microsoft OS/2 Programmer's Reference&quot; volume 1, chapter 51, &quot;Interprocess Communication&quot; (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$ = &quot;\pipe\connect&quot; + CHR$(0) SendStr = &quot;Hello from SERVER&quot; PRINT &quot;SERVER: Creating named pipe.&quot; bool% = DosMakeNmPipe(SSEG(connectPipe$),_                     SADD(connectPipe$),_                      connectPipeHP%,_                      PIPEACCESSDUPLEX,_                      PIPENOWAIT OR PIPEUNLIMITEDINSTANCES,_                      512, 512, 60000) IF bool% THEN PRINT &quot;SERVER: error creating named pipe.&quot; END END IF PRINT &quot;SERVER: Waiting for client to connect.&quot; bool% = ERRORPIPENOTCONNECTED WHILE bool% = ERRORPIPENOTCONNECTED bool% = DosConnectNmPipe(connectPipeHP%) SLEEP 1 WEND IF bool% THEN PRINT &quot;SERVER: error connecting pipe.&quot; END END IF

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

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

PRINT &quot;SERVER: DIsconnecting pipe.&quot; bool% = DosDisConnectNmPipe(connectPipeHP%)

PRINT &quot;SERVER: Closing pipe.&quot; bool% = DosClose(connectPipeHP%)

PRINT &quot;SERVER: Program ended.&quot; END

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

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

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

PRINT &quot;CLIENT: Closing pipe.&quot; bool% = DosClose(connectPipeHP%) PRINT &quot;CLIENT: done.&quot; END

Additional query words: BasicCom

Keywords: KB69796

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.