<p>Is it possible to use overlapped I/O with an anonymous pipe? CreatePipe() does not have any way of specifying FILE_FLAG_OVERLAPPED, so I assume ReadFile() will block, even if I supply an OVERLAPPED-structure. </p>

[{'answer_id': 60681, 'author': 'ChrisN', 'author_id': 3853, 'author_profile': 'https://Stackoverflow.com/users/3853', 'pm_score': 4, 'selected': False, 'text': '<p>No. As explained <a href="http://msdn.microsoft.com/en-us/library/aa365141%28VS.85%29.aspx" rel="noreferrer">here</a>, anonymous pipes do not support asynchronous I/O. You need to use a named pipe. There\'s example code to do this on MSDN <a href="http://msdn.microsoft.com/en-us/library/aa365603%28VS.85%29.aspx" rel="noreferrer">here</a> and <a href="http://msdn.microsoft.com/en-us/library/aa365592%28VS.85%29.aspx" rel="noreferrer">here</a>.</p>\n'}, {'answer_id': 419736, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 6, 'selected': True, 'text': '<p>Here is an implementation for an anonymous pipe function with the possibility to specify FILE_FLAG_OVERLAPPED:</p>\n\n<pre><code>/******************************************************************************\\\n*       This is a part of the Microsoft Source Code Samples. \n*       Copyright 1995 - 1997 Microsoft Corporation.\n*       All rights reserved. \n*       This source code is only intended as a supplement to \n*       Microsoft Development Tools and/or WinHelp documentation.\n*       See these sources for detailed information regarding the \n*       Microsoft samples programs.\n\\******************************************************************************/\n\n/*++\nCopyright (c) 1997  Microsoft Corporation\nModule Name:\n    pipeex.c\nAbstract:\n    CreatePipe-like function that lets one or both handles be overlapped\nAuthor:\n    Dave Hart  Summer 1997\nRevision History:\n--*/\n\n#include &lt;windows.h&gt;\n#include &lt;stdio.h&gt;\n\nstatic volatile long PipeSerialNumber;\n\nBOOL\nAPIENTRY\nMyCreatePipeEx(\n    OUT LPHANDLE lpReadPipe,\n    OUT LPHANDLE lpWritePipe,\n    IN LPSECURITY_ATTRIBUTES lpPipeAttributes,\n    IN DWORD nSize,\n    DWORD dwReadMode,\n    DWORD dwWriteMode\n    )\n\n/*++\nRoutine Description:\n    The CreatePipeEx API is used to create an anonymous pipe I/O device.\n    Unlike CreatePipe FILE_FLAG_OVERLAPPED may be specified for one or\n    both handles.\n    Two handles to the device are created.  One handle is opened for\n    reading and the other is opened for writing.  These handles may be\n    used in subsequent calls to ReadFile and WriteFile to transmit data\n    through the pipe.\nArguments:\n    lpReadPipe - Returns a handle to the read side of the pipe.  Data\n        may be read from the pipe by specifying this handle value in a\n        subsequent call to ReadFile.\n    lpWritePipe - Returns a handle to the write side of the pipe.  Data\n        may be written to the pipe by specifying this handle value in a\n        subsequent call to WriteFile.\n    lpPipeAttributes - An optional parameter that may be used to specify\n        the attributes of the new pipe.  If the parameter is not\n        specified, then the pipe is created without a security\n        descriptor, and the resulting handles are not inherited on\n        process creation.  Otherwise, the optional security attributes\n        are used on the pipe, and the inherit handles flag effects both\n        pipe handles.\n    nSize - Supplies the requested buffer size for the pipe.  This is\n        only a suggestion and is used by the operating system to\n        calculate an appropriate buffering mechanism.  A value of zero\n        indicates that the system is to choose the default buffering\n        scheme.\nReturn Value:\n    TRUE - The operation was successful.\n    FALSE/NULL - The operation failed. Extended error status is available\n        using GetLastError.\n--*/\n\n{\n  HANDLE ReadPipeHandle, WritePipeHandle;\n  DWORD dwError;\n  UCHAR PipeNameBuffer[ MAX_PATH ];\n\n  //\n  // Only one valid OpenMode flag - FILE_FLAG_OVERLAPPED\n  //\n\n  if ((dwReadMode | dwWriteMode) &amp; (~FILE_FLAG_OVERLAPPED)) {\n    SetLastError(ERROR_INVALID_PARAMETER);\n    return FALSE;\n  }\n\n  //\n  //  Set the default timeout to 120 seconds\n  //\n\n  if (nSize == 0) {\n    nSize = 4096;\n  }\n\n  sprintf( PipeNameBuffer,\n           "\\\\\\\\.\\\\Pipe\\\\RemoteExeAnon.%08x.%08x",\n           GetCurrentProcessId(),\n           InterlockedIncrement(&amp;PipeSerialNumber)\n         );\n\n  ReadPipeHandle = CreateNamedPipeA(\n                       PipeNameBuffer,\n                       PIPE_ACCESS_INBOUND | dwReadMode,\n                       PIPE_TYPE_BYTE | PIPE_WAIT,\n                       1,             // Number of pipes\n                       nSize,         // Out buffer size\n                       nSize,         // In buffer size\n                       120 * 1000,    // Timeout in ms\n                       lpPipeAttributes\n                       );\n\n  if (! ReadPipeHandle) {\n    return FALSE;\n  }\n\n  WritePipeHandle = CreateFileA(\n                      PipeNameBuffer,\n                      GENERIC_WRITE,\n                      0,                         // No sharing\n                      lpPipeAttributes,\n                      OPEN_EXISTING,\n                      FILE_ATTRIBUTE_NORMAL | dwWriteMode,\n                      NULL                       // Template file\n                    );\n\n  if (INVALID_HANDLE_VALUE == WritePipeHandle) {\n    dwError = GetLastError();\n    CloseHandle( ReadPipeHandle );\n    SetLastError(dwError);\n    return FALSE;\n  }\n\n  *lpReadPipe = ReadPipeHandle;\n  *lpWritePipe = WritePipeHandle;\n  return( TRUE );\n}\n</code></pre>\n'}, {'answer_id': 51448441, 'author': 'RbMm', 'author_id': 6401656, 'author_profile': 'https://Stackoverflow.com/users/6401656', 'pm_score': 4, 'selected': False, 'text': '<p>first of all need understand - what is <a href="https://learn.microsoft.com/en-us/windows/desktop/ipc/anonymous-pipes" rel="noreferrer"><em>Anonymous Pipes</em></a> and what, are exist difference between anonymous and <a href="https://learn.microsoft.com/en-us/windows/desktop/ipc/named-pipes" rel="noreferrer"><em>Named Pipes</em></a> at all. </p>\n\n<p>really exist only <strong>single pipe type</strong> (implemented by <em>npfs.sys</em>). no any difference, except name, between named and anonymous pipes at all. both is only pipes. </p>\n\n<p>so called anonymous pipes - this is special/random named pipes before win7 and true  unnamed pipes begin from win7. </p>\n\n<p>when msdn write that <em>"anonymous pipe is one-way pipe"</em> - this is <strong>lie</strong>. as any pipe it can be one-way or duplex. when msdn write that <em>"Asynchronous (overlapped) read and write operations are not supported by anonymous pipes."</em> - this is <strong>lie</strong>. of course pipes support asynchronous io. the name of pipe not affect this.</p>\n\n<p>before win7 really unnamed pipes even not exist at all. <a href="https://learn.microsoft.com/en-us/windows/desktop/ipc/anonymous-pipe-operations" rel="noreferrer"><code>CreatePipe</code></a> function use <code>Win32Pipes.%08x.%08x</code> format for create name of "Anonymous Pipe". </p>\n\n<pre><code>    static LONG PipeSerialNumber;\n    WCHAR name[64];\n    swprintf(name, L"\\\\Device\\\\NamedPipe\\\\Win32Pipes.%08x.%08x", \n        GetCurrentProcessId(), InterlockedIncrement(&amp;PipeSerialNumber));\n</code></pre>\n\n<p>begin from win7 <code>CreatePipe</code>  use another technique (relative file open) for create pipe pair - now it really anonymous.</p>\n\n<p>for example code witch create pipe pair where one pipe is asynchronous and not inheritable. and another pipe is synchronous and inheritable. both pipes is duplex (support both read and write)</p>\n\n<pre><code>ULONG CreatePipeAnonymousPair7(PHANDLE phServerPipe, PHANDLE phClientPipe)\n{\n    HANDLE hNamedPipe;\n\n    IO_STATUS_BLOCK iosb;\n\n    static UNICODE_STRING NamedPipe = RTL_CONSTANT_STRING(L"\\\\Device\\\\NamedPipe\\\\");\n\n    OBJECT_ATTRIBUTES oa = { sizeof(oa), 0, const_cast&lt;PUNICODE_STRING&gt;(&amp;NamedPipe), OBJ_CASE_INSENSITIVE };\n\n    NTSTATUS status;\n\n    if (0 &lt;= (status = NtOpenFile(&amp;hNamedPipe, SYNCHRONIZE, &amp;oa, &amp;iosb, FILE_SHARE_VALID_FLAGS, 0)))\n    {\n        oa.RootDirectory = hNamedPipe;\n\n        static LARGE_INTEGER timeout = { 0, MINLONG };\n        static UNICODE_STRING empty = {};\n\n        oa.ObjectName = &amp;empty;\n\n        if (0 &lt;= (status = ZwCreateNamedPipeFile(phServerPipe,\n            FILE_READ_ATTRIBUTES|FILE_READ_DATA|\n            FILE_WRITE_ATTRIBUTES|FILE_WRITE_DATA|\n            FILE_CREATE_PIPE_INSTANCE, \n            &amp;oa, &amp;iosb, FILE_SHARE_READ|FILE_SHARE_WRITE,\n            FILE_CREATE, 0, FILE_PIPE_BYTE_STREAM_TYPE, FILE_PIPE_BYTE_STREAM_MODE,\n            FILE_PIPE_QUEUE_OPERATION, 1, 0, 0, &amp;timeout)))\n        {\n            oa.RootDirectory = *phServerPipe;\n            oa.Attributes = OBJ_CASE_INSENSITIVE|OBJ_INHERIT;\n\n            if (0 &gt; (status = NtOpenFile(phClientPipe, SYNCHRONIZE|FILE_READ_ATTRIBUTES|FILE_READ_DATA|\n                FILE_WRITE_ATTRIBUTES|FILE_WRITE_DATA, &amp;oa, &amp;iosb, \n                FILE_SHARE_VALID_FLAGS, FILE_SYNCHRONOUS_IO_NONALERT)))\n            {\n                NtClose(oa.RootDirectory);\n            }\n        }\n\n        NtClose(hNamedPipe);\n    }\n\n    return RtlNtStatusToDosError(status);\n}\n\nULONG CreatePipeAnonymousPair(PHANDLE phServerPipe, PHANDLE phClientPipe)\n{\n    static char flag_supported = -1;\n\n    if (flag_supported &lt; 0)\n    {\n        ULONG dwMajorVersion, dwMinorVersion;\n        RtlGetNtVersionNumbers(&amp;dwMajorVersion, &amp;dwMinorVersion, 0);\n        flag_supported = _WIN32_WINNT_WIN7 &lt;= ((dwMajorVersion &lt;&lt; 8)| dwMinorVersion);\n    }\n\n    if (flag_supported)\n    {\n        return CreatePipeAnonymousPair7(phServerPipe, phClientPipe);\n    }\n\n    static LONG PipeSerialNumber;\n\n    WCHAR name[64];\n\n    swprintf(name, L"\\\\\\\\?\\\\pipe\\\\Win32Pipes.%08x.%08x", GetCurrentProcessId(), InterlockedIncrement(&amp;PipeSerialNumber));\n\n    HANDLE hClient, hServer = CreateNamedPipeW(name, \n        PIPE_ACCESS_DUPLEX|FILE_READ_DATA|FILE_WRITE_DATA|FILE_FLAG_OVERLAPPED, \n        PIPE_TYPE_BYTE|PIPE_READMODE_BYTE, 1, 0, 0, 0, 0);\n\n    if (hServer != INVALID_HANDLE_VALUE)\n    {\n        static SECURITY_ATTRIBUTES sa = { sizeof(sa), 0, TRUE };\n\n        hClient = CreateFileW(name, FILE_GENERIC_READ|FILE_GENERIC_WRITE, \n            FILE_SHARE_READ|FILE_SHARE_WRITE, &amp;sa, OPEN_EXISTING, 0, 0);\n\n        if (hClient != INVALID_HANDLE_VALUE)\n        {\n            *phServerPipe = hServer, *phClientPipe = hClient;\n            return NOERROR;\n        }\n\n        CloseHandle(hServer);\n    }\n\n    return GetLastError();\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/60645', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3923/']