Windows interprocess Communication--Named pipes

Source: Internet
Author: User
Tags readfile

1. Related overview

Named pipes (Named Pipes) is a simple inter-process communication (IPC) mechanism. A named pipe can be reliable bidirectional or unidirectional data communication between different processes on the same computer, or between different processes across a network of different computers.

Named pipes take advantage of the Microsoft Network provider (MSNP) redirector, so there is no need to involve details such as the underlying communication protocol. Named pipes are a mechanism designed around the Windows file system, using the Named pipe file system (Named pipe SYSTEM,NPFS) interface. As a result, both the client and the server can use standard WIN32 file system API functions such as ReadFile and WriteFile to send and receive data.

 The naming specification for named Pipes follows the Universal Naming convention (UNC):

  \\server\pipe[\path]\name

    • Where \\server specifies the name of a server, and if it is native, use \ \. Indicates that the \\192.168.1.100 represents a server on the network.
    • \pipe is an immutable "hard-coded" string (case insensitive) that indicates that the file belongs to the NPFS
    • [\path]\name uniquely identifies the name of a named pipe.]

2. Correlation function2.1 Service-side functions2.1.1    CreateNamedPipe Creating Named Pipes
1234567891011121314151617181920212223 /************************************************************************* Purpose :  创建命名管道,如果存在指定名字的管道,则创建该管道的一个实例 Input   :  lpName              --  管道名称            dwOpenMode          --  打开模式            dwPipeMode          --  消息模式            nMaxInstances       --  最大实例数(1-255)            nOutBufferSize      --  输出缓冲区长度,0表示用默认设置            nInBufferSize       --  输入缓冲区长度,0表示用默认设置            nDefaultTimeOut     --  管道的默认超时时间(毫秒),0表示默认超时时间50毫秒            lpSecurityAttributes--  安全描述符,如无特殊需求默认为0即可 Return  :  成功 -- 返回管道句柄 失败 -- 返回INVALID_HANDLE_VALUE 通过GetLastError()获取错误代码 Remark  :   *************************************************************************/HANDLE WINAPI CreateNamedPipe(  _In_      LPCTSTR lpName,  _In_      DWORD dwOpenMode,  _In_      DWORD dwPipeMode,  _In_      DWORD nMaxInstances,  _In_      DWORD nOutBufferSize,  _In_      DWORD nInBufferSize,  _In_      DWORD nDefaultTimeOut,  _In_opt_  LPSECURITY_ATTRIBUTES lpSecurityAttributes);

Dwopenmode is a combination of the following constants

one of the constants:
    • Pipe_access_duplex pipes are bidirectional.
    • pipe_access_inbound data flows from the client to the server side
    • pipe_access_outbound Data flows from the server side to the client
    the second of the constants:
    • File_flag_write_through in a byte-type pipeline established in a network, forcing data to be transmitted over the network at every read and write operation. Otherwise the transport cache causes delays
    • file_flag_overlapped allows (but does not require) asynchronous (overlapping) operations with this pipeline

Dwpipemode is a combination of the following constants

one of the constants:

    • pipe_type_byte Data is written to the pipeline as a continuous byte stream
    • pipe_type_message Data is written to the pipeline in the form of a block (called "message" or "packet")
the second of the constants:
    • pipe_readmode_byte data is read from the pipeline as a separate byte
    • pipe_readmode_message Data is read from the pipeline as a block of data named "message" (requires specifying Pipe_type_message)
three of the constants:
    • pipe_wait synchronous operation hangs thread while waiting
    • The pipe_nowait operation returns immediately. This provides a backward implementation for asynchronous transmission, which has been replaced by the overlapping transport mechanism of WIN32 (not recommended!). )
2.1.2    Connectnamedpipe Waiting for customer connection
1234567891011 /************************************************************************* Purpose :  等待客户连接管道 Input   :  hNamedPipe              --  创建管道的句柄,由CreateNamedPipe成功返回            lpOverlapped            --  打开模式 Return  :  TRUE -- 成功 FALSE -- 失败,通过GetLastError()获取错误码 Remark  :   *************************************************************************/ BOOL WINAPI ConnectNamedPipe(  _In_         HANDLE hNamedPipe,  _Inout_opt_  LPOVERLAPPED lpOverlapped);

lpoverlapped if set to NULL, the thread is suspended until a customer is connected to the pipe. Otherwise, the event object in the lpoverlapped structure is triggered when the customer is connected to the pipe, as soon as the pipe is not connected. A wait function can then be used to monitor the connection.


2.2 Client Functions2.2.1    CreateFile connecting to a named pipe2.2.2    Waitnamedpipe waiting for pipeline instances to be available
1234567891011 /************************************************************************* Purpose :  等待管道实例是否可用 Input   :  lpNamedPipeName         --  管道名称            nTimeOut                --  等待时间 Return  :  TRUE -- 成功 FALSE -- 失败,通过GetLastError()获取错误码 Remark  :   *************************************************************************/BOOL WINAPI WaitNamedPipe(  _In_  LPCTSTR lpNamedPipeName,  _In_  DWORD nTimeOut);
    2.3 Pipe Transceiver Data Functions2.3.1    ReadFile reading data from a pipe2.3.2    WriteFileWrite data to the pipeline




 

From for notes (Wiz)

Windows interprocess Communication--Named pipes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.