匿名管道上重叠的I/O.

Mob*_*yDX 13 winapi pipe

是否可以将重叠I/O与匿名管道一起使用?CreatePipe()没有任何指定FILE_FLAG_OVERLAPPED的方法,所以我假设ReadFile()将阻塞,即使我提供了OVERLAPPED结构.

小智 19

以下是匿名管道函数的实现,可以指定FILE_FLAG_OVERLAPPED:

/******************************************************************************\
*       This is a part of the Microsoft Source Code Samples. 
*       Copyright 1995 - 1997 Microsoft Corporation.
*       All rights reserved. 
*       This source code is only intended as a supplement to 
*       Microsoft Development Tools and/or WinHelp documentation.
*       See these sources for detailed information regarding the 
*       Microsoft samples programs.
\******************************************************************************/

/*++
Copyright (c) 1997  Microsoft Corporation
Module Name:
    pipeex.c
Abstract:
    CreatePipe-like function that lets one or both handles be overlapped
Author:
    Dave Hart  Summer 1997
Revision History:
--*/

#include <windows.h>
#include <stdio.h>

ULONG PipeSerialNumber;

BOOL
APIENTRY
MyCreatePipeEx(
    OUT LPHANDLE lpReadPipe,
    OUT LPHANDLE lpWritePipe,
    IN LPSECURITY_ATTRIBUTES lpPipeAttributes,
    IN DWORD nSize,
    DWORD dwReadMode,
    DWORD dwWriteMode
    )

/*++
Routine Description:
    The CreatePipeEx API is used to create an anonymous pipe I/O device.
    Unlike CreatePipe FILE_FLAG_OVERLAPPED may be specified for one or
    both handles.
    Two handles to the device are created.  One handle is opened for
    reading and the other is opened for writing.  These handles may be
    used in subsequent calls to ReadFile and WriteFile to transmit data
    through the pipe.
Arguments:
    lpReadPipe - Returns a handle to the read side of the pipe.  Data
        may be read from the pipe by specifying this handle value in a
        subsequent call to ReadFile.
    lpWritePipe - Returns a handle to the write side of the pipe.  Data
        may be written to the pipe by specifying this handle value in a
        subsequent call to WriteFile.
    lpPipeAttributes - An optional parameter that may be used to specify
        the attributes of the new pipe.  If the parameter is not
        specified, then the pipe is created without a security
        descriptor, and the resulting handles are not inherited on
        process creation.  Otherwise, the optional security attributes
        are used on the pipe, and the inherit handles flag effects both
        pipe handles.
    nSize - Supplies the requested buffer size for the pipe.  This is
        only a suggestion and is used by the operating system to
        calculate an appropriate buffering mechanism.  A value of zero
        indicates that the system is to choose the default buffering
        scheme.
Return Value:
    TRUE - The operation was successful.
    FALSE/NULL - The operation failed. Extended error status is available
        using GetLastError.
--*/

{
  HANDLE ReadPipeHandle, WritePipeHandle;
  DWORD dwError;
  UCHAR PipeNameBuffer[ MAX_PATH ];

  //
  // Only one valid OpenMode flag - FILE_FLAG_OVERLAPPED
  //

  if ((dwReadMode | dwWriteMode) & (~FILE_FLAG_OVERLAPPED)) {
    SetLastError(ERROR_INVALID_PARAMETER);
    return FALSE;
  }

  //
  //  Set the default timeout to 120 seconds
  //

  if (nSize == 0) {
    nSize = 4096;
  }

  sprintf( PipeNameBuffer,
           "\\\\.\\Pipe\\RemoteExeAnon.%08x.%08x",
           GetCurrentProcessId(),
           PipeSerialNumber++
         );

  ReadPipeHandle = CreateNamedPipeA(
                       PipeNameBuffer,
                       PIPE_ACCESS_INBOUND | dwReadMode,
                       PIPE_TYPE_BYTE | PIPE_WAIT,
                       1,             // Number of pipes
                       nSize,         // Out buffer size
                       nSize,         // In buffer size
                       120 * 1000,    // Timeout in ms
                       lpPipeAttributes
                       );

  if (! ReadPipeHandle) {
    return FALSE;
  }

  WritePipeHandle = CreateFileA(
                      PipeNameBuffer,
                      GENERIC_WRITE,
                      0,                         // No sharing
                      lpPipeAttributes,
                      OPEN_EXISTING,
                      FILE_ATTRIBUTE_NORMAL | dwWriteMode,
                      NULL                       // Template file
                    );

  if (INVALID_HANDLE_VALUE == WritePipeHandle) {
    dwError = GetLastError();
    CloseHandle( ReadPipeHandle );
    SetLastError(dwError);
    return FALSE;
  }

  *lpReadPipe = ReadPipeHandle;
  *lpWritePipe = WritePipeHandle;
  return( TRUE );
}
Run Code Online (Sandbox Code Playgroud)

  • Dave Hart的`MyCreatePipeEx`实现非常出色(基本上取决于Windows`CreatePipe`源代码本身),但你应该在所述实现中将`PipeSerialNumber ++`更改为`InterlockedIncrement(&PipeSerialNumber)`以避免MT代码中的竞争条件. (4认同)
  • 哇,这种方法非常微不足道,天才也是如此:).简而言之:它使用所有Overlapped选项创建一对命名管道(使用CreateNamedPipe读取管道并使用WriteFile写入管道)并返回其句柄. (3认同)

Chr*_*isN 16

号作为解释在这里,匿名管道不支持异步I/O.您需要使用命名管道.这里这里有 MSDN上的示例代码.

  • 错误的答案。匿名管道当然支持异步 I/O。 (2认同)

RbM*_*bMm 9

首先需要了解 - 什么是匿名管道以及匿名管道命名管道之间存在什么区别。

真正只存在单个管道类型(由npfs.sys实现)。除了名称之外,命名管道和匿名管道之间没有任何区别。两者都只是管道。

所谓的匿名管道 - 这是 win7 之前的特殊/随机命名管道,真正的未命名管道从 win7 开始。

当 msdn 写到“匿名管道是单向管道”时- 这是谎言。与任何管道一样,它可以是单向的或双工的。当 msdn 写到“匿名管道不支持异步(重叠)读写操作”时。- 这是谎言。当然管道支持异步io。管道的名称不影响这一点。

在win7之前,真正的无名管道甚至根本不存在。创建“匿名管道”名称的CreatePipe函数使用Win32Pipes.%08x.%08x格式。

    static LONG PipeSerialNumber;
    WCHAR name[64];
    swprintf(name, L"\\Device\\NamedPipe\\Win32Pipes.%08x.%08x", 
        GetCurrentProcessId(), InterlockedIncrement(&PipeSerialNumber));
Run Code Online (Sandbox Code Playgroud)

从 win7 开始CreatePipe 使用另一种技术(相对文件打开)来创建管道对 - 现在它真的是匿名的。

例如代码女巫创建管道对,其中一个管道是异步且不可继承的。另一个管道是同步和可继承的。两个管道都是双工的(支持读写)

ULONG CreatePipeAnonymousPair7(PHANDLE phServerPipe, PHANDLE phClientPipe)
{
    HANDLE hNamedPipe;

    IO_STATUS_BLOCK iosb;

    static UNICODE_STRING NamedPipe = RTL_CONSTANT_STRING(L"\\Device\\NamedPipe\\");

    OBJECT_ATTRIBUTES oa = { sizeof(oa), 0, const_cast<PUNICODE_STRING>(&NamedPipe), OBJ_CASE_INSENSITIVE };

    NTSTATUS status;

    if (0 <= (status = NtOpenFile(&hNamedPipe, SYNCHRONIZE, &oa, &iosb, FILE_SHARE_VALID_FLAGS, 0)))
    {
        oa.RootDirectory = hNamedPipe;

        static LARGE_INTEGER timeout = { 0, MINLONG };
        static UNICODE_STRING empty = {};

        oa.ObjectName = &empty;

        if (0 <= (status = ZwCreateNamedPipeFile(phServerPipe,
            FILE_READ_ATTRIBUTES|FILE_READ_DATA|
            FILE_WRITE_ATTRIBUTES|FILE_WRITE_DATA|
            FILE_CREATE_PIPE_INSTANCE, 
            &oa, &iosb, FILE_SHARE_READ|FILE_SHARE_WRITE,
            FILE_CREATE, 0, FILE_PIPE_BYTE_STREAM_TYPE, FILE_PIPE_BYTE_STREAM_MODE,
            FILE_PIPE_QUEUE_OPERATION, 1, 0, 0, &timeout)))
        {
            oa.RootDirectory = *phServerPipe;
            oa.Attributes = OBJ_CASE_INSENSITIVE|OBJ_INHERIT;

            if (0 > (status = NtOpenFile(phClientPipe, SYNCHRONIZE|FILE_READ_ATTRIBUTES|FILE_READ_DATA|
                FILE_WRITE_ATTRIBUTES|FILE_WRITE_DATA, &oa, &iosb, 
                FILE_SHARE_VALID_FLAGS, FILE_SYNCHRONOUS_IO_NONALERT)))
            {
                NtClose(oa.RootDirectory);
            }
        }

        NtClose(hNamedPipe);
    }

    return RtlNtStatusToDosError(status);
}

ULONG CreatePipeAnonymousPair(PHANDLE phServerPipe, PHANDLE phClientPipe)
{
    static char flag_supported = -1;

    if (flag_supported < 0)
    {
        ULONG dwMajorVersion, dwMinorVersion;
        RtlGetNtVersionNumbers(&dwMajorVersion, &dwMinorVersion, 0);
        flag_supported = _WIN32_WINNT_WIN7 <= ((dwMajorVersion << 8)| dwMinorVersion);
    }

    if (flag_supported)
    {
        return CreatePipeAnonymousPair7(phServerPipe, phClientPipe);
    }

    static LONG PipeSerialNumber;

    WCHAR name[64];

    swprintf(name, L"\\\\?\\pipe\\Win32Pipes.%08x.%08x", GetCurrentProcessId(), InterlockedIncrement(&PipeSerialNumber));

    HANDLE hClient, hServer = CreateNamedPipeW(name, 
        PIPE_ACCESS_DUPLEX|FILE_READ_DATA|FILE_WRITE_DATA|FILE_FLAG_OVERLAPPED, 
        PIPE_TYPE_BYTE|PIPE_READMODE_BYTE, 1, 0, 0, 0, 0);

    if (hServer != INVALID_HANDLE_VALUE)
    {
        static SECURITY_ATTRIBUTES sa = { sizeof(sa), 0, TRUE };

        hClient = CreateFileW(name, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 
            FILE_SHARE_READ|FILE_SHARE_WRITE, &sa, OPEN_EXISTING, 0, 0);

        if (hClient != INVALID_HANDLE_VALUE)
        {
            *phServerPipe = hServer, *phClientPipe = hClient;
            return NOERROR;
        }

        CloseHandle(hServer);
    }

    return GetLastError();
}
Run Code Online (Sandbox Code Playgroud)

  • @GeorgeSovetov 在 win7 之前,所有管道都被命名,只是使用随机名称,如“\\Device\\NamedPipe\\Win32Pipes.%08x.%08x”。从win7开始设计了这段代码(它需要*npfs.sys*驱动程序的支持)。so 文件根本没有名称。技巧是如何创建**对**。假设我们创建无名管道。如何“连接”到它?我们不能使用名称(它是空的)。因此,*npfs* 内部设计的特殊方式 - 使用 object_attributes 中的“RootDirectory”句柄作为点 - 我们想要连接到哪个管道 (3认同)
  • @GeorgeSovetov - 如果 api 作为输入参数 [`OBJECT_ATTRIBUTES`](https://docs.microsoft.com/en-us/windows/win32/api/ntdef/ns-ntdef-_object_attributes) 它支持打开或创建文件 *相对于 RootDirectory 目录*。具体来说 - 首先我们在 *\\Device\\NamedPipe* 上打开根文件夹 \,然后我们创建具有空名称的管道。然后使用空名称再次配对,但使用 `oa.RootDirectory = *phServerPipe`。这在 `CreatePipe` 代码内部使用,只是这个 api 硬编码管道上的同步 I/O (2认同)