home *** CD-ROM | disk | FTP | other *** search
/ Programming Win32 Under the API / ProgrammingWin32UnderTheApiPatVillani.iso / Chapter7 / 7-4 / client.c next >
C/C++ Source or Header  |  2000-03-08  |  922b  |  52 lines

  1.  
  2. #include <stdio.h>
  3. #include <windows.h>
  4.  
  5. #define PIPESIZE 1024
  6. #define PIPE    "\\\\.\\pipe\\mypipe"
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     HANDLE hPipe;
  11.     DWORD dwWritten, dwRead, dwError;
  12.     char sBuffer[PIPESIZE];
  13.     BOOL fConnected;
  14.  
  15.     for(;;)
  16.     {
  17.         if((hPipe = CreateFile(PIPE,
  18.               GENERIC_READ | GENERIC_WRITE,
  19.               0,
  20.               NULL,
  21.               OPEN_EXISTING,
  22.               0,
  23.               NULL)) != INVALID_HANDLE_VALUE)
  24.         {
  25.             break;
  26.         }
  27.  
  28.         if((dwError = GetLastError()) != ERROR_PIPE_BUSY)
  29.         {
  30.             printf("Could not open pipe, hPipe = %08lx error = #%d.\n",
  31.               hPipe, dwError);
  32.             ExitProcess(1);
  33.         }
  34.  
  35.         if(!WaitNamedPipe(PIPE, 20000))
  36.         {
  37.             printf("Could not open pipe #2.\n");
  38.             ExitProcess(1);
  39.         }
  40.     }
  41.  
  42.     ReadFile(hPipe, &sBuffer, PIPESIZE, &dwRead, NULL);
  43.     sBuffer[dwRead] = 0;
  44.         printf("%ld Bytes read, Data = \"%s\"\n", dwRead, sBuffer);
  45.  
  46.     FlushFileBuffers(hPipe);
  47.     DisconnectNamedPipe(hPipe);
  48.     CloseHandle(hPipe);
  49. }
  50.  
  51.  
  52.