Jump to content

DosSetNPipeSem: Difference between revisions

From EDM2
Ak120 (talk | contribs)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Description==
Attaches a shared event semaphore to a local named pipe.
Attaches a shared event semaphore to a local named pipe.


==Syntax==
==Syntax==
<PRE>
DosSetNPipeSem(hpipe, hsem, key)
#define INCL_DOSNMPIPES
#include <os2.h>


HPIPE    hpipe;  /*  The named-pipe handle to which a semaphore is to be attached. */
HSEM      hsem;  /*  The handle of an event semaphore or a multiple-wait (muxwait) semaphore that is posted when the pipe (hpipe) has either data to be read or write space available. */
ULONG    key;    /*  A key value that distinguishes events arriving on different named pipes that are attached to the same semaphore. */
APIRET    ulrc;  /*  Return Code. */
ulrc = DosSetNPipeSem(hpipe, hsem, key);
</PRE>
==Parameters==
==Parameters==
; hpipe (HPIPE) - input : The named-pipe handle to which a semaphore is to be attached.
;hpipe (HPIPE) - input : The named-pipe handle to which a semaphore is to be attached.
 
:The server handle is returned by DosCreateNPipe; the client handle is returned by DosOpen.
The server handle is returned by DosCreateNPipe; the client handle is returned by DosOpen.  
;hsem (HSEM) - input : The handle of an event semaphore or a multiple-wait (muxwait) semaphore that is posted when the pipe (hpipe) has either data to be read or write space available.
 
;key (ULONG) - input : A key value that distinguishes events arriving on different named pipes that are attached to the same semaphore.
; hsem (HSEM) - input : The handle of an event semaphore or a multiple-wait (muxwait) semaphore that is posted when the pipe (hpipe) has either data to be read or write space available.  


; key (ULONG) - input : A key value that distinguishes events arriving on different named pipes that are attached to the same semaphore.
==Return Code==
==Return Code==
ulrc (APIRET) - returns
;ulrc (APIRET) - returns:DosSetNPipeSem returns one of the following values:
 
* 0 NO_ERROR
DosSetNPipeSem returns one of the following values:
* 1 ERROR_INVALID_FUNCTION
 
* 6 ERROR_INVALID_HANDLE
* 0         NO_ERROR  
* 87 ERROR_INVALID_PARAMETER
* 1         ERROR_INVALID_FUNCTION
* 187 ERROR_SEM_NOT_FOUND
* 6         ERROR_INVALID_HANDLE
* 230 ERROR_BAD_PIPE
* 87         ERROR_INVALID_PARAMETER
* 233 ERROR_PIPE_NOT_CONNECTED
* 187       ERROR_SEM_NOT_FOUND
* 230       ERROR_BAD_PIPE
* 233       ERROR_PIPE_NOT_CONNECTED


==Remarks==
==Remarks==
DosSetNPipeSem can be used with local pipes or on the client end of a remote pipe. (A remote pipe is the client end of a pipe created on a remote named pipe server.) If an attempt is made to attach a semaphore to the server end of a remote pipe, DosSetNPipeSem returns 1 (ERROR_INVALID_FUNCTION).
DosSetNPipeSem can be used with local pipes or on the client end of a remote pipe. (A remote pipe is the client end of a pipe created on a remote named pipe server.) If an attempt is made to attach a semaphore to the server end of a remote pipe, DosSetNPipeSem returns 1 (ERROR_INVALID_FUNCTION).


If a semaphore is already attached to the specified handle, DosSetNPipeSem replaces the existing semaphore with the new one.  
If a semaphore is already attached to the specified handle, DosSetNPipeSem replaces the existing semaphore with the new one.
 
==Example Code==
==Example Code==
This example handles the host end of a named pipe for several other named pipe examples. Some return code checking has been omitted for brevity.
This example handles the host end of a named pipe for several other named pipe examples. Some return code checking has been omitted for brevity.
Line 125: Line 110:
   return NO_ERROR;
   return NO_ERROR;
}
}
</PRE>


</PRE>
==Related Functions==
==Related Functions==
* [[OS2 API:CPI:DosCallNPipe|DosCallNPipe]]
* [[DosCallNPipe]]
* [[OS2 API:CPI:DosClose|DosClose]]
* [[DosClose]]
* [[OS2 API:CPI:DosCloseMuxWaitSem|DosCloseMuxWaitSem]]
* [[DosCloseMuxWaitSem]]
* [[OS2 API:CPI:DosConnectNPipe|DosConnectNPipe]]
* [[DosConnectNPipe]]
* [[OS2 API:CPI:DosCreateEventSem|DosCreateEventSem]]
* [[DosCreateEventSem]]
* [[OS2 API:CPI:DosCreateNPipe|DosCreateNPipe]]
* [[DosCreateNPipe]]
* [[OS2 API:CPI:DosDisConnectNPipe|DosDisConnectNPipe]]
* [[DosDisConnectNPipe]]
* [[OS2 API:CPI:DosDupHandle|DosDupHandle]]
* [[DosDupHandle]]
* [[OS2 API:CPI:DosOpen|DosOpen]]
* [[DosOpen]]
* [[OS2 API:CPI:DosPeekNPipe|DosPeekNPipe]]
* [[DosPeekNPipe]]
* [[OS2 API:CPI:DosQueryNPHState|DosQueryNPHState]]
* [[DosQueryNPHState]]
* [[OS2 API:CPI:DosQueryNPipeInfo|DosQueryNPipeInfo]]
* [[DosQueryNPipeInfo]]
* [[OS2 API:CPI:DosQueryNPipeSemState|DosQueryNPipeSemState]]
* [[DosQueryNPipeSemState]]
* [[OS2 API:CPI:DosRead|DosRead]]
* [[DosRead]]
* [[OS2 API:CPI:DosResetBuffer|DosResetBuffer]]
* [[DosResetBuffer]]
* [[OS2 API:CPI:DosSetNPHState|DosSetNPHState]]
* [[DosSetNPHState]]
* [[OS2 API:CPI:DosTransactNPipe|DosTransactNPipe]]
* [[DosTransactNPipe]]
* [[OS2 API:CPI:DosWaitNPipe|DosWaitNPipe]]
* [[DosWaitNPipe]]
* [[OS2 API:CPI:DosWaitEventSem|DosWaitEventSem]]
* [[DosWaitEventSem]]
* [[OS2 API:CPI:DosWaitMuxWaitSem|DosWaitMuxWaitSem]]
* [[DosWaitMuxWaitSem]]
* [[OS2 API:CPI:DosWrite|DosWrite]]
* [[DosWrite]]
 


[[Category:The OS/2 API Project]]
[[Category:Dos]]

Latest revision as of 00:20, 28 May 2021

Attaches a shared event semaphore to a local named pipe.

Syntax

DosSetNPipeSem(hpipe, hsem, key)

Parameters

hpipe (HPIPE) - input
The named-pipe handle to which a semaphore is to be attached.
The server handle is returned by DosCreateNPipe; the client handle is returned by DosOpen.
hsem (HSEM) - input
The handle of an event semaphore or a multiple-wait (muxwait) semaphore that is posted when the pipe (hpipe) has either data to be read or write space available.
key (ULONG) - input
A key value that distinguishes events arriving on different named pipes that are attached to the same semaphore.

Return Code

ulrc (APIRET) - returns
DosSetNPipeSem returns one of the following values:
  • 0 NO_ERROR
  • 1 ERROR_INVALID_FUNCTION
  • 6 ERROR_INVALID_HANDLE
  • 87 ERROR_INVALID_PARAMETER
  • 187 ERROR_SEM_NOT_FOUND
  • 230 ERROR_BAD_PIPE
  • 233 ERROR_PIPE_NOT_CONNECTED

Remarks

DosSetNPipeSem can be used with local pipes or on the client end of a remote pipe. (A remote pipe is the client end of a pipe created on a remote named pipe server.) If an attempt is made to attach a semaphore to the server end of a remote pipe, DosSetNPipeSem returns 1 (ERROR_INVALID_FUNCTION).

If a semaphore is already attached to the specified handle, DosSetNPipeSem replaces the existing semaphore with the new one.

Example Code

This example handles the host end of a named pipe for several other named pipe examples. Some return code checking has been omitted for brevity.

#define INCL_BASE
#define INCL_DOSSEMAPHORES
#define INCL_DOSNMPIPES
#include <os2.h>
#include <stdio.h>
#include <string.h>

int main(VOID) {
CHAR     PipeName[256] = "\\PIPE\\EXAMPLE" ;     /* Pipe name */
HPIPE    PipeHandle    = NULLHANDLE;             /* Pipe handle */
HEV      hev           = NULLHANDLE;             /* Semaphore handle */
ULONG    ulBytes       = 0;                      /* Bytes read or written */
CHAR     message[256]  = "";                     /* Input/Output buffer */
APIRET   rc            = NO_ERROR;               /* Return code */

   rc = DosCreateNPipe(PipeName,                /* Name of pipe to create */
                       &PipeHandle,             /* Handle returned for pipe */
                       NP_ACCESS_DUPLEX,        /* Duplex pipe */
                       NP_WAIT |
                       NP_TYPE_MESSAGE |
                       NP_READMODE_MESSAGE |
                       NP_WMESG |               /* Write messages */
                       NP_RMESG |               /* Read messages */
                       0x01,                    /* Unique instance of pipe */
                       sizeof(message),         /* Output buffer size */
                       sizeof(message),         /* Input buffer size */
                       0L);                     /* Use default time-out */
   if (rc != NO_ERROR) {
     printf("DosCreateNPipe error: return code = %u\n",rc);
     return 1;
   }

   rc = DosCreateEventSem("\\SEM32\\PIPE\\EXAMPLE", &hev, 0L, 0L);
   /* Should check if (rc != NO_ERROR)  here... This semaphore is not
      always used.                                                     */

   rc = DosSetNPipeSem(PipeHandle,       /* Handle for pipe */
                       (HSEM) hev,       /* Handle of semaphore */
                       1L);              /* Used to distinguish among events */
   if (rc != NO_ERROR) {
     printf("DosSetNPipeSem error: return code = %u\n",rc);
     return 1;
   }

   printf("Waiting for connection to pipe %s...\n",PipeName);

   rc = DosConnectNPipe(PipeHandle);
   if (rc != NO_ERROR) {
     printf("DosConnectNPipe error: return code = %u\n",rc);
     return 1;
   }

   printf("\nCONNECTED\nWaiting for a message...\n");
   rc = DosRead(PipeHandle,            /* Handle of pipe */
                message,               /* Buffer for message read */
                sizeof(message),       /* Buffer size */
                &ulBytes);             /* Number of bytes actually read */
   if (rc != NO_ERROR) {
     printf("DosRead error: return code = %u\n",rc);
     return 1;
   }

   printf("\n\nMessage received was: %s\n\n", message);

   strcpy(message, "Thank you for your message!");
   rc = DosWrite(PipeHandle,           /* Handle of pipe */
                 message,              /* Buffer containing message to write */
                 strlen(message),      /* Length of message */
                 &ulBytes);            /* Number of bytes actually written */
   if (rc != NO_ERROR) {
     printf("DosWrite error: return code = %u\n",rc);
     return 1;
   }

   rc = DosCloseEventSem(hev);
   /* Should check if (rc != NO_ERROR)  here... */

   rc = DosDisConnectNPipe(PipeHandle);
   /* Should check if (rc != NO_ERROR)  here... */

   return NO_ERROR;
}

Related Functions