| 1 |
/*++ |
| 2 |
CreatePipe-like function that lets one or both handles be overlapped |
| 3 |
Author: |
| 4 |
Dave Hart Summer 1997 |
| 5 |
--*/ |
| 6 |
#ifdef __WXMSW__ |
| 7 |
#include "MyCreatePipe.h" |
| 8 |
|
| 9 |
ULONG PipeSerialNumber = 0; |
| 10 |
|
| 11 |
/*++ |
| 12 |
Routine Description: |
| 13 |
The MyCreatePipe API is used to create an anonymous pipe I/O device. |
| 14 |
Unlike CreatePipe FILE_FLAG_OVERLAPPED may be specified for one or |
| 15 |
both handles. |
| 16 |
Two handles to the device are created. One handle is opened for |
| 17 |
reading and the other is opened for writing. These handles may be |
| 18 |
used in subsequent calls to ReadFile and WriteFile to transmit data |
| 19 |
through the pipe. |
| 20 |
Arguments: |
| 21 |
lpReadPipe - Returns a handle to the read side of the pipe. Data |
| 22 |
may be read from the pipe by specifying this handle value in a |
| 23 |
subsequent call to ReadFile. |
| 24 |
lpWritePipe - Returns a handle to the write side of the pipe. Data |
| 25 |
may be written to the pipe by specifying this handle value in a |
| 26 |
subsequent call to WriteFile. |
| 27 |
lpPipeAttributes - An optional parameter that may be used to specify |
| 28 |
the attributes of the new pipe. If the parameter is not |
| 29 |
specified, then the pipe is created without a security |
| 30 |
descriptor, and the resulting handles are not inherited on |
| 31 |
process creation. Otherwise, the optional security attributes |
| 32 |
are used on the pipe, and the inherit handles flag effects both |
| 33 |
pipe handles. |
| 34 |
nSize - Supplies the requested buffer size for the pipe. This is |
| 35 |
only a suggestion and is used by the operating system to |
| 36 |
calculate an appropriate buffering mechanism. A value of zero |
| 37 |
indicates that the system is to choose the default buffering |
| 38 |
scheme. |
| 39 |
Return Value: |
| 40 |
TRUE - The operation was successful. |
| 41 |
FALSE/NULL - The operation failed. Extended error status is available |
| 42 |
using GetLastError. |
| 43 |
--*/ |
| 44 |
BOOL APIENTRY MyCreatePipe(OUT LPHANDLE lpReadPipe, OUT LPHANDLE lpWritePipe, IN LPSECURITY_ATTRIBUTES lpPipeAttributes, |
| 45 |
IN DWORD nSize) |
| 46 |
|
| 47 |
{ |
| 48 |
HANDLE ReadPipeHandle, WritePipeHandle; |
| 49 |
DWORD dwError; |
| 50 |
CHAR PipeNameBuffer[MAX_PATH]; |
| 51 |
|
| 52 |
// |
| 53 |
// Only one valid OpenMode flag - FILE_FLAG_OVERLAPPED |
| 54 |
// |
| 55 |
DWORD dwReadMode = FILE_FLAG_OVERLAPPED; |
| 56 |
DWORD dwWriteMode = FILE_FLAG_OVERLAPPED; |
| 57 |
|
| 58 |
// |
| 59 |
// Set the default timeout to 120 seconds |
| 60 |
// |
| 61 |
if(nSize == 0) { nSize = 4096; } |
| 62 |
|
| 63 |
// Increment the pipe number, in a MT safe way |
| 64 |
InterlockedIncrement(&PipeSerialNumber); |
| 65 |
sprintf(PipeNameBuffer, "\\\\.\\Pipe\\LOCAL\\RemoteExeAnon.%08x.%08x", (unsigned int)GetCurrentProcessId(), |
| 66 |
(unsigned int)PipeSerialNumber); |
| 67 |
|
| 68 |
ReadPipeHandle = CreateNamedPipeA(PipeNameBuffer, PIPE_ACCESS_INBOUND | dwReadMode, PIPE_TYPE_BYTE | PIPE_WAIT, |
| 69 |
1, // Number of pipes |
| 70 |
nSize, // Out buffer size |
| 71 |
nSize, // In buffer size |
| 72 |
120 * 1000, // Timeout in ms |
| 73 |
lpPipeAttributes); |
| 74 |
|
| 75 |
if(!ReadPipeHandle) { return FALSE; } |
| 76 |
|
| 77 |
WritePipeHandle = CreateFileA(PipeNameBuffer, GENERIC_WRITE, |
| 78 |
0, // No sharing |
| 79 |
lpPipeAttributes, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | dwWriteMode, |
| 80 |
NULL // Template file |
| 81 |
); |
| 82 |
|
| 83 |
if(INVALID_HANDLE_VALUE == WritePipeHandle) { |
| 84 |
dwError = GetLastError(); |
| 85 |
CloseHandle(ReadPipeHandle); |
| 86 |
SetLastError(dwError); |
| 87 |
return FALSE; |
| 88 |
} |
| 89 |
|
| 90 |
*lpReadPipe = ReadPipeHandle; |
| 91 |
*lpWritePipe = WritePipeHandle; |
| 92 |
return (TRUE); |
| 93 |
} |
| 94 |
#endif // __WXMSW__ |