The following communication methods are provided under Win32:
Clipboard clipboard: Commonly Used in the 16-bit era, which is supported by the cwnd class.
COM/DCOM: exchange data between processes through the proxy stubs of the com system, but it can only transmit data when calling interface functions, data can be transferred between different hosts through DCOM.
Dynamic Data Exchange (DDE): a frequently used method in the 16-bit era.
File mapping: A new method provided in a 32-bit system to share memory.
Mailslots: A new method provided in the 32-bit system. It can exchange data between different hosts and is divided into server and customer. Both parties can exchange data through it, in Win9x, only mail slot customers are supported.
Pipes: pipeline, which is divided into unknown pipelines: data exchange between parent and child processes; famous pipelines: data exchange between different hosts, divided into server and customer, in Win9x, only famous MPs queue customers are supported.
RPC: Remote Process calls are rarely used for two reasons: complex and not fully compatible with the RCP of UNIX systems. However, the call to COM/DCOM is based on rpc.
Windows Sockets: a network interface that can exchange data between different hosts, divided into the server side and the customer side. (For more information, see Chapter 6 Network Communication Development in Visual C ++/mfc)
Wm_copydata: Send the wm_copydata message and put the data in the parameter to pass the data to other processes.
When creating a named MPs queue, you must specify a host name and MPs queue name. The client can use the following format: // [host_name]/pipe/[pipe_name]/or //. /pipe/pipe_name/where. indicates the local machine. The server can only specify the local host as the host name, that is, the following format can be used: //./pipe_name /. In addition, remember that the pipe name is unique on the same host. Once a named pipe is created, the pipe with the same name cannot be created again.
The server passes through:
Handle createnamedpipe (
Lptstr 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 ndefatimetimeout, // time-out interval
Lpsecurity_attributes lpsecurityattributes // SD
);
Create a named pipe and open an existing named pipe. lpname indicates the pipe name, dwopenmode indicates the creation method, and can be a combination of the following nominal values:
Pipe_access_inbound: the pipeline can only be used to receive data.
Pipe_access_outbound: the pipe can only be used to send data.
Pipe_access_duplex: A pipe can send or receive data. (Only one of the above three values can be obtained)
File_flag_write_through: the pipeline is used to synchronously send and receive data. The function is returned only when the data is sent to the target address, if you do not set this parameter, the system may send messages only when the data is accumulated to a certain amount because of reduced network convolutionals, and calls to the sending function will be immediately returned.
File_flag_overlapped: the pipeline can be used for asynchronous input and output. The methods related to asynchronous read/write are the same as those for asynchronous file read/write.
Dwpipemode specifies the pipe type, which can be a combination of the lower nominal values:
Pipe_type_byte: The data is sent as a byte stream when it is sent through the pipeline, and cannot be shared with pipe_readmode_message.
Pipe_type_message: The data is sent as a message when it is sent through the pipeline, and cannot be shared with pipe_readmode_byte.
Pipe_readmode_byte: receives byte streams when receiving data.
Pipe_readmode_message: receives messages when receiving data.
Pipe_wait: The wait mode is used. When reading, writing, and establishing a connection, the other party of the pipeline must complete the corresponding action before returning.
Pipe_nowait: in non-Wait mode, when reading, writing, and establishing a connection, the other party of the pipeline does not need to complete the corresponding action and will return immediately.
Nmaxinstances is the maximum number of pipelines. This parameter indicates the number of pipelines that can exist at the same time when a server pipeline is created for the first time. Pipe_unlimited_instances indicates that the quantity is not limited. Noutbuffersize and ninbuffersize indicate the buffer size. Ndefatimetimeout indicates the longest waiting time (in milliseconds) while waiting for the connection. If it is set to nmpwait_use_default_wait during creation, there is no limit to waiting, in the future, the server needs to set the same value for other pipeline instances. Lpsecurityattributes is a Security Attribute and is generally set to null. If it fails to be created or opened, invalid_handle_value is returned. You can get an error through getlasterror.
Customers pass:
Handle createfile (
Lptstr lpfilename, // file name
DWORD dwdesiredaccess, // Access Mode
DWORD dw1_mode, // share mode
Lpsecurity_attributes lpsecurityattributes, // SD
DWORD dwcreationdisposition, // how to create
DWORD dwflagsandattributes, // file attributes
Handle htemplatefile // handle to Template File
);
Createfile can be used to create file, MPs queue, email slot, and directory. createfile is used to open the named MPs queue on the client. Lpfilename is used to specify the MPs queue name. Dwdesiredaccess is used to indicate the usage. The following values can be used:
Generic_read: open a read-only pipeline.
Generic_write: open a pipeline for writing only.
Generic_read | generic_write: open a pipeline for reading and writing.
Dwsharemode specifies the sharing mode. Generally, it is set to 0, lpsecurityattributes is set to Security Attribute, null, dwcreationdisposition is set to open_existing, and dwflagsandattributes is set to file_attribute_normal, in addition, it can be set to file_flag_overlapped for asynchronous communication, and htemplatefile is set to null. If it fails to be opened, invalid_handle_value is returned. You can get an error through getlasterror.
In addition, the customer can use:
Bool callnamedpipe (
Lptstr 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 message sending pipeline.
For MPs queue connection management, the client can establish a server connection immediately after calling createfile. Once the MPs queue is opened or created, the server can use
Bool connectnamedpipe (
Handle hnamedpipe, // handle to Named Pipe
Lpoverlapped // overlapped Structure
);
Wait until the client connection is established. You can call
Bool waitnamedpipe (
Lptstr lpnamedpipename, // pipe name
DWORD ntimeout // time-out interval
);
The lpnamepipename here directly uses the name when creating the pipeline. If the server wants to close the connection, it calls
Bool disconnectnamedpipe (
Handle hnamedpipe // handle to Named Pipe
);
Once the connection is closed, the server can call connectnamedpipe again to establish the connection. To close the MPs queue, call closehandle. Note that the closing and closing of pipelines mentioned here are different. connections can be established repeatedly on the same pipeline, and the system load can be reduced. If the maximum number of pipelines is specified, the new pipeline cannot be opened if the old one is not closed after the maximum number of opened pipelines is reached.
The customer cannot close the connection, but can only directly call closehandle to close the pipeline.
Both the server and the customer can use readfile and writefile to perform pipeline read and write for data transmission.
The following is an example. The server creates or opens an MTS queue and reads the data sent by the other party. The server converts lowercase letters to uppercase and then returns the data, the customer creates a connection to the server, sends a string, and reads the converted data:
In this example, three server processes are run, and when the fourth process is run, the pipeline fails to be opened because the number of pipelines reaches the limit.
// Service provider
Void cnamed_pipedlg: oncreatep ()
{
DWORD dwto = nmpwait_use_default_wait; // set the connection wait time
Handle hsvr = createnamedpipe ("//. // pipe // test_pipe //", pipe_access_duplex, pipe_type_byte, 3,256,256, dwto, null );
If (invalid_handle_value = hsvr)
{
Afxmessagebox ("error create/Open pipe ");
}
Else
{
If (connectnamedpipe (hsvr, null ))
{
Byte bread;
DWORD dwread, dwwritten;
While (readfile (hsvr, & bread, 1, & dwread, null ))
{
If (bread> = 'A' & bread $ lt; = 'Z ')
Bread = 'A' + (bread-'A ');
Writefile (hsvr, & bread, 1, & dwwritten, null );
}
}
Else
{
Afxmessagebox ("error when waiting connected ");
}
Closehandle (hsvr );
}
}
// Client
Void cnamed_pipe_cdlg: onconn ()
{
Handle hclient = createfile ("//. // pipe // test_pipe //", generic_write | generic_read, 0, null, open_existing, file_attribute_normal, null );
If (hclient = invalid_handle_value)
{
Afxmessagebox ("error open pipe ");
}
Else
{
DWORD dwread, dwwritten;
Char szsend [10] = "send ...";
Char szrecv [10];
For (INT I = 0; I <strlen (szsend) + 1; I ++)
{
Writefile (hclient, szsend + I, 1, & dwwritten, null );
Readfile (hclient, szrecv + I, 1, & dwread, null );
}
Closehandle (hclient); // close Pipe
Afxmessagebox (szrecv );
}
}