There are several ways to communicate between the processes provided under Win32:
Clipboard CLIPBOARD: Support in the CWND class in the CWND class in the 16-bit era. COM / DCOM: Inter-process data exchange is performed through the agent stub mode of the COM system, but can only be transmitted during the call of the interface function, and data can be transmitted between different hosts through the DCOM. Dynamic Data Exchange (DDE): Ways often use in 16 times. FILE MAPPING: Filemmaps, new methods available in the 32-bit system can be used to share memory. Mailslots: The mail slot, the new method provided in the 32-bit system, can exchange data between different hosts, divided into server side and client, and both sides can be exchanged, only the mail slot customer is supported under Win9X. Pipes: Pipes, divided into non-name pipe: exchange data between parent son processes; well-known pipes: Data can be exchanged between different hosts, divided into server side and client, only support guest customers under Win9X. RPC: Remote procedure calls, rarely used, there are two reasons: complicated and RCPs with UNIX systems are not fully compatible. However, COM / DCOM is built on the RPC. Windows Sockets: Network Sets, can exchange data between different hosts, divided into server side and client. (Related Introduction to Visual C / MFC Getting Started Tutorial Chapter 6 Network Communicat) WM_COPYDATA: Pass data to other processes by sending a WM_COPYDATA message and putting data in parameters. When you create a naming pipe, you need to specify a hostname and pipe name. For the client, you can have the following format: // [host_name] / PIPE / [PIPE_NAME] / or //./pipe/pipe_name/ where it means. . The server side can only be used as a host name at the specified native, ie, can only use the following format: //./pipe_name/. In addition, it is necessary to remember that the pipeline name on the same host is unique, and once a naming pipe does not allow the same name to be created again.
Server party passes:
HANDLE CreateNamedPipe (LPCTSTR lpName, // pipe name DWORD dwOpenMode, // pipe open mode DWORD dwPipeMode, // pipe-specific modes DWORD nMaxInstances, // maximum number of instances DWORD nOutBufferSize, // output buffer size DWORD nInBufferSize, // input buffer size DWORD nDefaultTimeOut, // time-out interval LPSECURITY_ATTRIBUTES lpSecurityAttributes // SD); create a named pipe and the existing open named pipe, wherein the pipe is lpName name, dwOpenMode create embodiment, it may be a combination of the following values: PIPE_ACCESS_INBOUND: duct It can only be used as receiving data. PIPE_ACCESS_OUTBOUND: The pipe can only be used as a transmission data. PIPE_ACCESS_DUPLEX: The pipe can also be sent either data. (The above three values can only be taken in one) file_flag_write_through: The pipe is used to simultaneously send and receive data, and the send function will only return when the data is sent to the target address. If this parameter is not set, then the system is in the system for named pipes The processing may only be sent due to the reduction of the network, and the data accumulation is sent to a certain amount, and the call to the send function will return immediately. FILE_FLAG_OVERLAPPED: The pipe can be used for asynchronous input and output, and the way asynchronous read and write is the same. DWPIPEMODE Specifies the type of pipe, which can be a combination of the following value: PIPE_TYPE_BYTE: Data is sent as byte stream when sending through the pipeline, and cannot be shared with the PIPE_READMODE_MESSAGE. PIPE_TYPE_MESSAGE: Data is sent as a message when sending through the pipeline, cannot be shared with the PIPE_READMODE_BYTE. PIPE_READMODE_BYTE: Receive byte streams when receiving data. PIPE_READMODE_MESSAGE: Receive messages when receiving data. PIPE_WAIT: Using Waiting Mode, when reading, writing, and establishing a connection, it is necessary to return the other party of the pipe to complete the corresponding action. PIPE_NOWAIT: Use a non-waiting mode, when reading, writing, and establishing a connection, the other party does not require the corresponding action after completing the corresponding action. NmaxInstances is the maximum number of pipelines, and this parameter indicates the amount of the same time when the server is established in the first time. PIPE_UNLIMITED_INSTANCES indicates that the number is not restricted. NoutBuffersize and ninbuffersize indicate the size of the buffer. NDEFAULTTIMEOUT indicates that the longest wait time (in milliseconds in milliseconds) at the time of connection, if set to nmpwait_use_default_wait, indicate unlimited waits, while the other piping instances of the server side need to set the same value. LPSecurityAttributes is a security attribute, typically set to NULL.
Returns Invalid_Handle_Value if you created or open fails. An error can be obtained by getLastError. Client side through: HANDLE CreateFile (LPCTSTR lpFileName, // file name DWORD dwDesiredAccess, // access mode DWORD dwShareMode, // share mode LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD DWORD dwCreationDisposition, // how to create DWORD dwFlagsAndAttributes, // file attributes HANDLE HTemplateFile // Handle To Template File; Create a client named pipe, CreateFile can be used to create files, pipes, mail slots, directories, etc. Here, you will introduce you to open the client named pipe with CREATEFILE. LPFileName is used to indicate the name of the pipeline. DwdesiredAccess is used to indicate how to use it, you can use the following value: generic_read: Open a pipe that is only used for reading. Generic_Write: Open a pipe that is only used. Generic_Read | Generic_Write: Opens a pipe for reading and writing. dwShareMode specify the shared mode, generally designated as 0, lpSecurityAttributes is safe property, usually set to NULL, dwCreationDisposition setting to FILE_ATTRIBUTE_NORMAL to OPEN_EXISTING, dwFlagsAndAttributes, may also be provided in addition to asynchronous communication is FILE_FLAG_OVERLAPPED, hTemplateFile set to NULL. Returns Invalid_Handle_Value if the open failed. An error can be obtained by getLastError. In addition, the client can use:
BOOL CallNamedPipe (LPCTSTR lpNamedPipeName, // pipe name LPVOID lpInBuffer, // write buffer DWORD nInBufferSize, // size of write buffer LPVOID lpOutBuffer, // read buffer DWORD nOutBufferSize, // size of read buffer LPDWORD lpBytesRead, // number of bytes Read dword ntimeout // Time-out value); Create a pipe that sends a message. The connection management of the pipeline, the client can establish the connection of the server immediately after calling createfile, and the server side can be used once the pipe is opened or created.
Bool ConnectNamedPipe (Handle Hnamedpipe, // Handle To Named Pipe Lpoverlapped LPoverLapped "; to wait for the client's connection to create. If you want to detect if there is a connection arrival at the server side, you can call BOOL WAITNAMEDPIPE (LPCTSTSTR LPNAMEDPIPENAME, / / PIPE NAME DWORDPIPIPEN // Time-Out Interval; the LPNamePipeName here is directly using the name when the pipe is created, if the server is hoped to close The connection calls Bool DisconnectNamedPipe (Handle HnamedPipe // Handle to Named PIPE);
Once the connection is turned off, the server can call ConnectNameDpipe again to establish a connection. If you want to close the pipe, call CloseHandle directly. Note that the shutdown pipes mentioned herein and the closed connection are different, and the connection can be established in turn on the same pipe, and the load can be reduced. And if the maximum number of pipelines is specified, the new pipe cannot be opened if the old pipe is not closed if the open pipe has reached the maximum limit.
Connections cannot be turned off for the client, but only directly call CloseHandle to close the pipe.
The sending of data, whether the server or the client can use ReadFile and Writefile to make the purpose of communication.
Below is an example, the server party creates or opens a pipe and read the data sent by the other party, converts the lowercase letters into uppercase letters, return, and the customer sends a connection to the server and sends a string and read back data:
When using this example, run three server processes, and the fourth time is run because of the limit of the pipeline, the pipe failed.