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

Winapi 匿名管道上的重叠I/O,winapi,pipe,Winapi,Pipe,是否可以对匿名管道使用重叠I/O?CreatePipe()没有任何方法来指定FILE_FLAG_OVERLAPPED,因此我假设ReadFile()将被阻止,即使我提供了重叠结构 否。如上所述,匿名管道不支持异步I/O。您需要使用命名管道。MSDN和上有这样做的示例代码。以下是匿名管道函数的实现,可以指定文件\u标志\u重叠: /******************************************************************************\ *

是否可以对匿名管道使用重叠I/O?CreatePipe()没有任何方法来指定FILE_FLAG_OVERLAPPED,因此我假设ReadFile()将被阻止,即使我提供了重叠结构

否。如上所述,匿名管道不支持异步I/O。您需要使用命名管道。MSDN和上有这样做的示例代码。

以下是匿名管道函数的实现,可以指定文件\u标志\u重叠:

/******************************************************************************\
*       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>

static volatile long 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(),
           InterlockedIncrement(&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 );
}
/******************************************************************************\
*这是Microsoft源代码示例的一部分。
*版权所有1995-1997微软公司。
*版权所有。
*此源代码仅作为对的补充
*Microsoft开发工具和/或WinHelp文档。
*请参阅这些来源,以了解有关
*Microsoft示例程序。
\******************************************************************************/
/*++
版权所有(c)1997年微软公司
模块名称:
胡椒粉
摘要:
CreatePipe类函数,允许一个或两个句柄重叠
作者:
戴夫·哈特1997年夏季
修订历史:
--*/
#包括
#包括
静态易失性长序列号;
布尔
蜂房
MyCreatePipeEx(
输出lpReadPipe,
输出LPHANDLE LPWritePie,
在LPSECURITY_属性lpPipeAttributes中,
在德沃德·恩西泽,
DWORD dwReadMode,
DWORD dwWriteMode
)
/*++
例行程序说明:
CreatePipeEx API用于创建匿名管道I/O设备。
与CreatePipe文件不同,可以为一个或多个文件指定重叠标志
两个把手。
将创建设备的两个句柄。打开一个手柄进行操作
一个是阅读,另一个是写作。这些手柄可能是
用于随后调用ReadFile和WriteFile传输数据
通过管道。
论据:
lpReadPipe-返回管道读取端的句柄。资料
可以通过在中指定此句柄值从管道中读取
对ReadFile的后续调用。
LPWritePie-返回管道写入端的句柄。资料
可以通过在中指定此句柄值写入管道
对WriteFile的后续调用。
lpPipeAttributes—一个可选参数,可用于指定
新管道的属性。如果参数不是
指定,则创建管道时不使用安全性
描述符,并且结果句柄不会在上继承
进程创建。否则,将显示可选的安全属性
在管道上使用,并且“继承句柄”标志会同时产生两种效果
管道手柄。
nSize-为管道提供请求的缓冲区大小。这是
只有一个建议,操作系统使用它来
计算适当的缓冲机制。零值
指示系统将选择默认缓冲
计划。
返回值:
正确-手术成功。
FALSE/NULL-操作失败。扩展错误状态可用
使用GetLastError。
--*/
{
句柄ReadPipeHandle,WritePipeHandle;
德沃德误差;
UCHAR PipeNameBuffer[最大路径];
//
//只有一个有效的OpenMode标志-文件标志重叠
//
if((dwReadMode | dwWriteMode)&(~FILE_FLAG_OVERLAPPED)){
SetLastError(错误\无效\参数);
返回FALSE;
}
//
//将默认超时设置为120秒
//
如果(nSize==0){
nSize=4096;
}
sprintf(PipeNameBuffer,
“\\.\\Pipe\\RemoteExeAnon.%08x.%08x”,
GetCurrentProcessId(),
联锁增量(&PipeSerialNumber)
);
ReadPipeHandle=CreateNamedPipeA(
PipeNameBuffer,
管道|进入| dwReadMode,
管道类型字节管道等待,
1,//管道数量
nSize,//输出缓冲区大小
nSize,//在缓冲区大小中
120*1000,//超时(毫秒)
lpPipeAttributes
);
如果(!ReadPipeHandle){
返回FALSE;
}
writepItemHandle=CreateFileA(
PipeNameBuffer,
你写什么,
0,//没有共享
这些属性,
开放式,
文件|属性|正常| dwWriteMode,
空//模板文件
);
if(无效的\u句柄\u值==writepItemHandle){
dwError=GetLastError();
关闭手柄(ReadPipeHandle);
SetLastError(dwError);
返回FALSE;
}
*lpReadPipe=ReadPipeHandle;
*LPWritePie=WritePiehandle;
返回(真);
}

首先需要了解-什么是匿名,什么是匿名,有什么区别

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

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

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

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

从win7开始
CreatePipe
使用另一种技术(相对文件打开)创建管道对-现在它是r
    static LONG PipeSerialNumber;
    WCHAR name[64];
    swprintf(name, L"\\Device\\NamedPipe\\Win32Pipes.%08x.%08x", 
        GetCurrentProcessId(), InterlockedIncrement(&PipeSerialNumber));
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();
}