Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Some simple snippets from the client (your service) &amp; the server (the notifier) [<em>Note: This is adapted from a project I've done a while ago which in turn was heavily "influenced" by the MSDN samples from CreateNamedPipe &amp; co</em>]:</p> <p>Server side:</p> <pre><code>HANDLE hPipe = INVALID_HANDLE_VALUE; bool bConnected = false; hPipe = CreateNamedPipe( L"\\\\.\\pipe\\nhsupspipe", PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, sizeof( Message ), 0, 0, NULL ); // failed to create pipe? if( hPipe == INVALID_HANDLE_VALUE ){ return -1; } // Wait for the client to connect; if it succeeds, // the function returns a nonzero value. If the function // returns zero, GetLastError returns ERROR_PIPE_CONNECTED. bConnected = ConnectNamedPipe( hPipe, NULL ) ? true : ( GetLastError() == ERROR_PIPE_CONNECTED ); if( bConnected ){ while( true ){ unsigned long ulBytesRead = 0; // read client requests from the pipe. bool bReadOk = ReadFile( hPipe, &amp;message, sizeof( message ), &amp;ulBytesRead, NULL ); // bail if read failed [error or client closed connection] if( !bReadOk || ulBytesRead == 0 ) break; // all ok, process the message received } } else{ // the client could not connect, so close the pipe. CloseHandle( hPipe ); } return 0; </code></pre> <p>The client:</p> <pre><code>HANDLE hPipe = INVALID_HANDLE_VALUE; // create the named pipe handle hPipe = CreateFile( L"\\\\.\\pipe\\nhsupspipe", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL ); // if everything ok set mode to message mode if( INVALID_HANDLE_VALUE != hPipe ){ DWORD dwMode = PIPE_READMODE_MESSAGE; // if this fails bail out if( !SetNamedPipeHandleState( hPipe, &amp;dwMode, NULL, NULL ) ){ CloseHandle( hPipe ); return -1; } } unsigned long ulBytesWritten = 0; bool bWriteOk = WriteFile( hPipe, ( LPCVOID )&amp;message, sizeof( Message ), &amp;ulBytesWritten, NULL ); // check if the writing was ok if( !bWriteOk || ulBytesWritten &lt; sizeof( Message ) ){ return -1; } // written ok return 0; </code></pre> <p>The <strong>Message</strong> mentioned above is a structure that will be your message, which you will probably want to <a href="http://en.wikipedia.org/wiki/Data_structure_alignment#Data_Structure_Padding" rel="nofollow noreferrer">pack</a>.</p> <p>Since in your scenario the client (the service) will probably be up &amp; running before the server (the notifier) you'll need to have in place some kind of reconnection strategy on the client side.</p> <p>And on a slightly different note you should consider carefully what Mr. Osterman said in his <a href="https://stackoverflow.com/questions/1849918/pipe-communication-c/1850277#1850277">reply</a> (even for nothing else but because he's Larry Osterman).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload