Windows core programming-memory ing file (2)

Source: Internet
Author: User
/*************************************** * ********************************* // 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 ;}

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.