IPC inter-process communication + mail slot MailSlotIPC (Inter-Process Communication ).Use of modern computersVirtual Memory Mechanism, IsProcessProvides an independent enough address space for security purposes. A process does not have special permissions and cannot access the memory space of another process. The process is isolated from each other. Inter-process communication (IPC) requires a special mechanism. MailSlot is one of the commonly used IPC methods.1. MailSlot):The MailSlot communication process is dividedServerAndClient. The server creates a MailSlot and specifies the mail slot name during creation. The client opens MailSlot with the same mail slot name.The mail trough is unidirectional.The server can only read MailSlot, and the client can only write MailSlot. The server reads data first in first out, that is, the data written first is read first.
2. mail slot name:Local format: \. \ mailslot \ [path \] nameFor example: \. mailslot \ my_mailslot
3. Basic API functions:Create a mailbox:HANDLE CreateMailSlot (Lptstr lpName,DWORD nMaxMessageSize,DWORD lReadTimeout,LPSECURITY_ATTRIBUTES lpSecurityAttributes );The first parameter is the mail slot name.The second parameter indicates the maximum message length.Third parameter: Read timeout. If it is set to 0, no message is returned immediately. When MAILSLOT_WAIT_FORVER, the message remains waiting.Third parameter: Security Attribute, which is generally set to NULL.This function creates a Mailslot and returns the handle of the mailbox.Read the mail trough:BOOL ReadFile (
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDOWRD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped );The first parameter is the mail slot handle.The second parameter is the cache address.The third parameter is the message length.Fourth parameter: the actual read length.Fifth parameter: generally set to NULL.This function is used to read data in the mailbox.Open the mailbox:BOOL CreateFile (Lptstr lpFileName,
DWORD dwDesiredAccess,
DWORD dw1_mode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile);The first parameter is the file pointer.The second parameter is access mode.Third parameter: Shared Mode.Fourth parameter: Security Attribute pointer.Fifth parameter: Create option.Sixth parameter: file attributes.Seventh parameter: used to copy the file handle.This function is used by the client to open Mailslot.Write to the mailbox:BOOL WriteFile (
HANDLE hFile,
LPCVOID lpBuffer,
DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped );The first parameter is the file handle.The second parameter is the data buffer pointer.Third parameter: number of bytes written.Fourth parameter: returns the actual number of bytes written.Fifth parameter: struct pointer, usually NULL.This function is used by the client to write data to Mailslot.
4. Cow test:Run the server program in VC6.0 and then run the client program:
Running effect:
Mailslot Server:Mailslot client: