Analysis of Windows core programming code based on Visual C ++ (16) Process Communication Using the mail Trough

Source: Internet
Author: User

In Windows, there are many ways to implement inter-process communication, such as sockets, pipelines, remote process calls, and NetBIOS. the mail slot is one of the ways to implement inter-process communication between single-channel processes. The process of creating a mail slot is called the mail slot server, while other processes that send messages to the mail slot are called the mail slot client. The client can send messages to the mail slot of the local machine or to the mail slot of other computers in the LAN. All these messages are stored in the mail slot, until the mail slot server reads it. These messages are generally sent in broadcast mode based on connectionless datagram. Therefore, the transmission quality is not reliable when the line is poor.

This method of inter-process communication is suitable for transmitting and receiving short messages in the LAN environment, or broadcasting messages to all computers in the LAN.

There are three steps for inter-process communication using the mail slot: creating a mail slot server, sending messages to the mail slot, and reading messages from the mail slot. The following three steps are completed using Windows API functions.

 

The client code of the mail trough is implemented as follows.

/* Header file */# include <windows. h> # include <stdio. h>/* global variable */handle hslot; lptstr lpszslotname = text ("\\\\. \ Mailslot \ sample_mailslot "); // Mailslot name lptstr lpszmessage = text (" Test message for Mailslot "); // communication content /********************************** * *** void main () * function: The Mailslot communication client ********************************* * ***/void main () {bool fresult; handle hfile; DWORD cbwritten; DWORD cbmessage; // open m Ailslothfile = createfile (lpszslotname, generic_write, // writable file_assist_read, (lpsecurity_attributes) null, open_existing, // open an existing Mailslot, which should have been created by the server, (handle) null); If (hfile = invalid_handle_value) {printf ("createfile failed with % d. \ n ", getlasterror (); return;} // write fresult = writefile (hfile, lpszmessage, (DWORD) (lstrlen (lpszmessage) + 1) to Mailslot) * sizeof (tchar), & cbwri TTen, (lpoverlapped) null); If (! Fresult) {printf ("writefile failed with % d. \ n ", getlasterror (); return;} // end printf (" slot written to successfully. \ n "); closehandle (hfile); return ;}

 

 

The server code implementation of the mail slot is as follows.

 

/* Header file */# include <windows. h> # include <stdio. h>/* global variable */handle hslot; lptstr lpszslotname = text ("\\\\. \ Mailslot \ sample_mailslot "); lptstr message = text (" message for Mailslot in primary domain. "); /************************************ void main () * function: The Mailslot communication client ********************************* * ***/void main () {DWORD cbmessage, cmessage, cbread; bool fresult; lptstr lpszbuffer; tchar Achi D [80]; DWORD callmessages; handle hevent; overlapped ov; cbmessage = cmessage = cbread = 0; hslot = createmailslot (lpszslotname, // Mailslot name 0, // do not limit the Message Size mailslot_wait_forever, // No timeout (lpsecurity_attributes) null); If (hslot = invalid_handle_value) {printf ("createmailslot failed with % d \ n ", getlasterror (); return;} else printf ("Mailslot created successfully. \ n "); While (1) {// get Mailslot information fresult = Getmailslotinfo (hslot, // Mailslot handle (lpdword) null, // no maximum message Limit & cbmessage, // the size of the next message & cmessage, // The number of messages (lpdword) null); // No time limit if (! Fresult) {printf ("getmailslotinfo failed with % d. \ n ", getlasterror (); return;} If (cbmessage = mailslot_no_message) {// no message. After a while, read sleep (20000); continue ;} callmessages = cmessage; while (cmessage! = 0) // get all the messages. More than one {// message wsprintf (lptstr) achid, "\ nmessage # % d of % d \ n ", callmessages-cmessage + 1, callmessages); // allocate space lpszbuffer = (lptstr) heapalloc (getprocessheap (), heap_zero_memory, lstrlen (lptstr) achid) * sizeof (tchar) + cbmessage); If (null = lpszbuffer) {return;} // read the message fresult = readfile (hslot, // Mailslot handle lpszbuffer, // cache cbmessage, // message length & cbread, // The actual read length is null); If (! Fresult) {printf ("readfile failed with % d. \ n ", getlasterror (); globalfree (hglobal) lpszbuffer); return;} // process information, display lstrcat (lpszbuffer, (lptstr) achid ); printf ("Contents of the Mailslot: % s \ n", lpszbuffer); heapfree (getprocessheap (), 0, lpszbuffer ); // calculate the remaining Message count fresult = getmailslotinfo (hslot, (lpdword) null, & cbmessage, & cmessage, (lpdword) null); If (! Fresult) {printf ("getmailslotinfo failed (% d) \ n", getlasterror (); Return ;}} return ;}

 

/* Header file */# include <windows. h> # include <stdio. h>/* global variable */handle hslot; lptstr lpszslotname = text ("\\\\. \ Mailslot \ sample_mailslot "); lptstr message = text (" message for Mailslot in primary domain. "); /************************************ void main () * function: The Mailslot communication client ********************************* * ***/void main () {DWORD cbmessage, cmessage, cbread; bool fresult; lptstr lpszbuffer; tchar Achi D [80]; DWORD callmessages; handle hevent; overlapped ov; cbmessage = cmessage = cbread = 0; hslot = createmailslot (lpszslotname, // Mailslot name 0, // do not limit the Message Size mailslot_wait_forever, // No timeout (lpsecurity_attributes) null); If (hslot = invalid_handle_value) {printf ("createmailslot failed with % d \ n ", getlasterror (); return;} else printf ("Mailslot created successfully. \ n "); While (1) {// get Mailslot information fresult = Getmailslotinfo (hslot, // Mailslot handle (lpdword) null, // no maximum message Limit & cbmessage, // the size of the next message & cmessage, // The number of messages (lpdword) null); // No time limit if (! Fresult) {printf ("getmailslotinfo failed with % d. \ n ", getlasterror (); return;} If (cbmessage = mailslot_no_message) {// no message. After a while, read sleep (20000); continue ;} callmessages = cmessage; while (cmessage! = 0) // get all the messages. More than one {// message wsprintf (lptstr) achid, "\ nmessage # % d of % d \ n ", callmessages-cmessage + 1, callmessages); // allocate space lpszbuffer = (lptstr) heapalloc (getprocessheap (), heap_zero_memory, lstrlen (lptstr) achid) * sizeof (tchar) + cbmessage); If (null = lpszbuffer) {return;} // read the message fresult = readfile (hslot, // Mailslot handle lpszbuffer, // cache cbmessage, // message length & cbread, // The actual read length is null); If (! Fresult) {printf ("readfile failed with % d. \ n ", getlasterror (); globalfree (hglobal) lpszbuffer); return;} // process information, display lstrcat (lpszbuffer, (lptstr) achid ); printf ("Contents of the Mailslot: % s \ n", lpszbuffer); heapfree (getprocessheap (), 0, lpszbuffer ); // calculate the remaining Message count fresult = getmailslotinfo (hslot, (lpdword) null, & cbmessage, & cmessage, (lpdword) null); If (! Fresult) {printf ("getmailslotinfo failed (% d) \ n", getlasterror (); Return ;}} return ;}

 

 

 

 

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.