/*************************************** * ********************************* // Use memory ing. implement inter-process data communication * // * Please note Article From: http://blog.csdn.net/windows_nt *//************************************ ***********************************/# include <windows. h> # include <iostream> using namespace STD; // tell the compiler to put this initialized variable in its own shared // section so it is shared by all instances of this application. # pragma data_seg ("shared") Volatile long g_lapplicationinstances = 0; # pragma data_seg () // tell the linker to make the shared section readable, writable, and shared. # pragma comment (linker, "/section: shared, RWS") void main () {// multiple applications Program Data sharing between instances is used to determine how many application instances interlockedexchangeadd (& g_lapplicationinstances, 1); printf ("this is the % d application instance. \ R \ n ", g_lapplicationinstances); // memory ing implements data communication between processes, and static handle s_hfilemap = NULL can be implemented between different programs; // create a paging file-backed MMF to contain the Edit Control text. // The MMF is 4 kb at most and is named mmfshareddata. s_hfilemap = createfilemapping (invalid_handle_value, null, page_readwrite, 0, 4*1024, text ("mmfshareddata"); If (s_hfilemap! = NULL) {If (getlasterror () = error_already_exists) {printf ("the memory ing file object already exists. Now read the memory ing and display it "); // see if a memory-mapped file named mmfshareddata already exists. handle hfilemapt = openfilemapping (file_map_read | file_map_write, false, text ("mmfshareddata"); If (hfilemapt! = NULL) {// The MMF does exist, map it into the process's address space. pvoid pview = mapviewoffile (hfilemapt, file_map_read | file_map_write, 0, 0); If (pview! = NULL) {// put the contents of the MMF into the Edit Control. printf ("read data: \ r \ n % s", (char *) pview); unmapviewoffile (pview);} else {printf ("can't map view. ");} closehandle (hfilemapt);} else {printf (" can't open mapping. ");} closehandle (s_hfilemap);} else {// file mapping created successfully. // map a view of the file into the address space. pvoid pview = mapviewoffile (s_hfilemap, file_map_read | file_m Ap_write, 0, 0, 0); If (pview! = NULL) {printf ("memory ing view created successfully! \ R \ n "); // put edit text into the MMF. printf ("Enter the data to be shared (Press enter to terminate): \ r \ n"); CIN> (char *) pview; // protect the MMF storage by unmapping it. unmapviewoffile (pview); printf ("You can now start a new process to read the data you just tested. \ R \ n ");} else {printf (" can't map view of file. ") ;}} else {printf (" can't create file mapping. ");} getchar (); // This instance of the application is terminatinginterlockedexchangeadd (& g_lapplicationinstances,-1); return ;}