WIN32 Pipe

Pipes

A pipe is a section of shared memory that processes use for communication. The process that creates a pipe is the pipe server. A process that connects to a pipe is a pipe client. One process writes information to the pipe, then the other process reads the information from the pipe. This overview describes how to create, manage, and use pipes.

//管道是用于进程间通信的一段共享内存,创建管道的进程称为管道服务器端,连接管道的进程称为管道客户端。一个进程向管道写入数据,然后其它进程从里面读出数据。以下概述描述了如何去创建、控制和使用管道。

 

About Pipes

There are two types of pipes: anonymous pipes and named pipes. Anonymous pipes require less overhead than named pipes, but offer limited services.

The term pipe, as used here, implies that a pipe is used as an information conduit. Conceptually, a pipe has two ends. A one-way pipe allows the process at one end to write to the pipe, and allows the process at the other end to read from the pipe. A two-way (or duplex) pipe allows a process to read and write from its end of the pipe.

//管道有两种类型,一种称为“匿名管道”(Anonynous pipes),另一种称为“命名管道”(Naned pipes)。匿名管道比命名管道易于管理,但是功能较少。

在这里,“管道”这个词表明其用作一种信息通道。概念上,一个管道有两个端,一端用于进程向其写入数据,另一端用于对方进程从管道读取数据。一个双向的管道允许一个进程对其进行读写操作。

 

Anonymous Pipes

An anonymous pipe is an unnamed, one-way pipe that typically transfers data between a parent process and a child process. Anonymous pipes are always local; they cannot be used for communication over a network.

//匿名管道是一种未命名、单向的管道,经常用于父进程和子进程之间的数据传输。匿名管道一般供本地应用,不能支持网络应用。

 

Named Pipes

A named pipe is a named, one-way or duplex pipe for communication between the pipe server and one or more pipe clients. All instances of a named pipe share the same pipe name, but each instance has its own buffers and handles, and provides a separate conduit for client/server communication. The use of instances enables multiple pipe clients to use the same named pipe simultaneously.

Any process can access named pipes, subject to security checks, making named pipes an easy form of communication between related or unrelated processes.

Any process can act as both a server and a client, making peer-to-peer communication possible. As used here, the term pipe server refers to a process that creates a named pipe, and the term pipe client refers to a process that connects to an instance of a named pipe.

Named pipes can be used to provide communication between processes on the same computer or between processes on different computers across a network. If the server service is running, all named pipes are accessible remotely. If you intend to use a named pipe locally only, deny access to NT AUTHORITY/NETWORK or switch to local RPC.

//命名管道是一种有命名、单向或者双向的管道,用于管道服务器和一个或者多个管道客户端之间的通信。所有命名管道的实例共享一个共同的管道别名,但是各个实例都有属于自己的内存空间和句柄,而且提供了单独的管道用于服务器/客户通信。

 

 

 

 

CreatePipe Function

Creates an anonymous pipe, and returns handles to the read and write ends of the pipe.

//创建一个匿名的管道,并且返回管道读写函数的句柄。

Syntax

C++

BOOL WINAPI CreatePipe(
  __out     PHANDLE hReadPipe,
  __out     PHANDLE hWritePipe,
  __in_opt  LPSECURITY_ATTRIBUTES lpPipeAttributes,
  __in      DWORD nSize
);

Parameters

hReadPipe [out]

A pointer to a variable that receives the read handle for the pipe.

//指向管道读操作函数句柄的指针

hWritePipe [out]

A pointer to a variable that receives the write handle for the pipe.

//指向管道写操作函数句柄的指针

lpPipeAttributes [in, optional]

A pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpPipeAttributes is NULL, the handle cannot be inherited.

The lpSecurityDescriptor member of the structure specifies a security descriptor for the new pipe. If lpPipeAttributes is NULL, the pipe gets a default security descriptor. The ACLs in the default security descriptor for a pipe come from the primary or impersonation token of the creator.

//SECURITY_ATTRIBUTES 结构体用于决定本函数返回句柄能否被子进程继承,lpPipeAttributes参数为指向该结构体的指针。如果lpPipeAttributes参数设置为NULL,则句柄不能被继承。该结构体的lpSecurityDescriptor成员变量定义了新管道的安全描述符。如果lpPipeAttributes设置为NULL,则管道获得一个默认的安全描述符。管道默认安全描述符的ACLs来源于构造函数。

nSize [in]

The size of the buffer for the pipe, in bytes. The size is only a suggestion; the system uses the value to calculate an appropriate buffering mechanism. If this parameter is zero, the system uses the default buffer size.

//管道内存空间的大小(单位:byte),这个大小仅仅是一个提议,系统用这个参数去计算计划一个合适的内存管理方式。如果该参数设置为NULL,则系统使用默认值。

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

//如果函数执行成功,则范围的值不为0, 如果执行失败,返回值为0。若要取得详细的错误信息,请调用GetLastError。

Remarks

CreatePipe creates the pipe, assigning the specified pipe size to the storage buffer. CreatePipe also creates handles that the process uses to read from and write to the buffer in subsequent calls to the ReadFile and WriteFile functions.

To read from the pipe, a process uses the read handle in a call to the ReadFile function. ReadFile returns when one of the following is true: a write operation completes on the write end of the pipe, the number of bytes requested has been read, or an error occurs.

When a process uses WriteFile to write to an anonymous pipe, the write operation is not completed until all bytes are written. If the pipe buffer is full before all bytes are written, WriteFile does not return until another process or thread uses ReadFile to make more buffer space available.

Anonymous pipes are implemented using a named pipe with a unique name. Therefore, you can often pass a handle to an anonymous pipe to a function that requires a handle to a named pipe.

//CreatePipe函数创建管道,分配指定大小的内存空间,并且创建了进程用于对管道进行读和写的函数的句柄,这两个句柄将在接下来的ReadFile和WriteFile函数中被调用。

若要从管道中读取信息,进程须在ReadFile函数中调用管道的Read句柄。在以下某种情况为true时,ReadFile返回:

管道Write端的写操作完成;

指定大小的字节读操作完成;

错误发生

当一个进程用WriteFile函数向一个匿名管道进行写操作时,直到所有字节全部写完时写操作才会完成。如果在所有字节还未写完之前管道的内存空间已经占满,WriteFile函数不会返回,除非有其它的进程或者线程调用了ReadFile操作腾出了足够的管道内存空间。

匿名管道其实是以唯一名字的命名管道形式进行执行的,所以,你可以将匿名管道的句柄赋给一个命名管道。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值