Ignore:
Timestamp:
Jun 2, 2003, 6:25:36 PM (22 years ago)
Author:
sandervl
Message:

CreatePipe: create unique named pipe

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/npipe.cpp

    r9975 r10132  
    1 /* $Id: npipe.cpp,v 1.11 2003-04-02 12:58:30 sandervl Exp $ */
     1/* $Id: npipe.cpp,v 1.12 2003-06-02 16:25:18 sandervl Exp $ */
    22/*
    33 * Win32 Named pipes API
     
    3838                         LPSECURITY_ATTRIBUTES lpsa, DWORD cbPipe)
    3939{
    40   return (HMCreatePipe(phRead,phWrite,lpsa,cbPipe));
     40  char        szPipeName[64];
     41  HANDLE      hPipeRead, hPipeWrite;
     42
     43  // create a unique pipe name
     44  for(int i=0;i<16;i++)
     45  {
     46        sprintf(szPipeName, "\\\\.\\pipe\\Win32.Pipes.%08x.%08x", GetCurrentProcessId(), GetCurrentTime());
     47        hPipeRead = ::CreateNamedPipeA(szPipeName, PIPE_ACCESS_DUPLEX,
     48                                     PIPE_TYPE_BYTE | PIPE_WAIT, 1, cbPipe, cbPipe,
     49                                     NMPWAIT_USE_DEFAULT_WAIT, lpsa);
     50        if(hPipeRead != INVALID_HANDLE_VALUE) break;
     51
     52        Sleep(10);
     53  }
     54
     55  if (hPipeRead == INVALID_HANDLE_VALUE) {
     56      dprintf(("ERROR: Unable to create named pipe with unique name!! (%s)", szPipeName));
     57      return FALSE;
     58  }
     59
     60  ULONG mode = PIPE_NOWAIT|PIPE_READMODE_BYTE;
     61
     62  //Set pipe in non-blocking mode
     63  SetNamedPipeHandleState(hPipeRead, &mode, NULL, NULL);
     64
     65  //Set pipe in listening mode (so the next CreateFile will succeed)
     66  ConnectNamedPipe(hPipeRead, NULL);
     67
     68  //Put pipe back in blocking mode
     69  mode = PIPE_WAIT|PIPE_READMODE_BYTE;
     70  SetNamedPipeHandleState(hPipeRead, &mode, NULL, NULL);
     71
     72  //Create write pipe
     73  hPipeWrite = CreateFileA(szPipeName, GENERIC_WRITE, 0, lpsa, OPEN_EXISTING, 0, 0);
     74  if (hPipeWrite == INVALID_HANDLE_VALUE)
     75  {
     76      dprintf(("ERROR: Unable to create write handle for anonymous pipe!!"));
     77      CloseHandle(hPipeRead);
     78      return FALSE;
     79  }
     80
     81  dprintf(("Created pipe with handles %x and %x", hPipeRead, hPipeWrite));
     82  *phRead  = hPipeRead;
     83  *phWrite = hPipeWrite;
     84  return TRUE;
    4185}
    4286//******************************************************************************
Note: See TracChangeset for help on using the changeset viewer.