Advertisement
Guest User

CreatePipeW

a guest
Apr 10th, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #define _WIN32_WINNT 0x0501
  2. #define MAX_PATH 0xFF
  3. #include "pch.h"
  4. #include <iostream>
  5. #include <cstdio>
  6. #include <errno.h>
  7. #include "CreatePipeEx.h"
  8.  
  9. namespace IO
  10. {
  11.     namespace Pipes
  12.     {
  13.         bool __stdcall CreatePipeW(
  14.             OUT LPHANDLE readPipe,
  15.             OUT LPHANDLE writePipe,
  16.             IN LPSECURITY_ATTRIBUTES securityAttributes,
  17.             IN unsigned long size,
  18.             unsigned long readMode,
  19.             unsigned long writeMode,
  20.             wstring pipeName
  21.         )
  22.         {
  23.             if (size == 0) { size = 4096; }
  24.             if ((readMode | writeMode) & (~FILE_FLAG_OVERLAPPED))
  25.             {
  26.                 SetLastError(ERROR_INVALID_PARAMETER);
  27.                 return false;
  28.             }
  29.             HANDLE readPipeHandle = CreateNamedPipeW(
  30.                 pipeName.c_str(),
  31.                 PIPE_ACCESS_INBOUND | readMode,
  32.                 PIPE_TYPE_BYTE | PIPE_WAIT,
  33.                 1,
  34.                 size,
  35.                 size,
  36.                 120 * 1000,
  37.                 securityAttributes
  38.             );
  39.             if (!readPipeHandle)
  40.             {
  41.                 return false;
  42.             }
  43.             HANDLE writePipeHandle = CreateFileW(
  44.                 pipeName.c_str(),
  45.                 GENERIC_WRITE,
  46.                 0,
  47.                 securityAttributes,
  48.                 OPEN_EXISTING,
  49.                 FILE_ATTRIBUTE_NORMAL | writeMode,
  50.                 nullptr
  51.             );
  52.             if (writePipeHandle == INVALID_HANDLE_VALUE)
  53.             {
  54.                 unsigned long errorCode = GetLastError();
  55.                 CloseHandle(readPipeHandle);
  56.                 SetLastError(errorCode);
  57.                 return false;
  58.             }
  59.             *readPipe = readPipeHandle;
  60.             *writePipe = writePipeHandle;
  61.             return true;
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement