Sometimes we encounter the need to share data between window processes, for example, I want to know how many instances of a process are currently in the system. We can define a global variable in the program, initialize it to 0, add 1 when the program starts, and of course we can use third-party media to store the variable and parse it. This must be done after the first write parsing, can not update the data in real time. What if you do not consider other storage media, just the communication in the process? Windows provides some practical ways to do this, as described in the following two common types.
One, shared data segment
#include "stdafx.h" #include <Windows.h>}; #pragma data_seg ("Shared") volatile int g_lappinstances = 0; #pragma data _seg () #pragma comment (linker, "/SECTION:SHARED,RWS") int _tmain (int argc, _tchar* argv[]) {printf ("The instance of app is% D\n ", ++g_lappinstances); GetChar (); return 0;}
The above is the inclusion of shared data segments in the code. When you run an instance of a program and open another instance, G_lappinstances points to the same memory so that data can be shared. However, the disadvantage of this method is that only one variable can be shared with the data, which is not possible for the struct body.
Second, memory Mapping text pieces
First Program:
#include " StdAfx.h "#include <windows.h>struct shareddata{int A; int b;float C; Shareddata (int x, int y, float z) {a = x; b = y; c = z;}}; const int buf_size = 256; TCHAR szname[] = _t ("global\\myfilemappingobj"); int _tmain (int argc, _tchar* argv[]) {HANDLE hmapfile = createfilemapping (Invalid_handle_value,null, Page_readwrite, 0, Buf_size, szName), if (hmapfile = = NULL) {_tprintf (_t ("Could not create File mapping obj\n ")); return 1;} LPCTSTR PBuf = (LPCTSTR) mapviewoffile (hmapfile, file_map_all_access, 0, 0, buf_size); if (PBuf = = NULL) {_tprintf (_t (" Could not mapping file\n ")); CloseHandle (Hmapfile); return 2;} <span style= "White-space:pre" ></span><pre name= "code" class= "CPP" ><span style= "White-space: Pre "></span>shareddata *psd = (shareddata*) pBuf; _tprintf (" The data from _t is%d,%d, IPC2 "),%f\n, PS D->b, psd->c); GetChar ();
UnmapViewOfFile (PBUF); CloseHandle (hmapfile); return 0;}
Second program:
#include "stdafx.h" #include <windows.h>struct shareddata{int A; int b;float C; Shareddata (int x, int y, float z) {a = x; b = y; c = z;}}; const int buf_size = 256; TCHAR szname[] = _t ("global\\myfilemappingobj"); int _tmain (int argc, _tchar* argv[]) {HANDLE hmapfile = createfilemapping (Invalid_handle_value,null, Page_readwrite, 0, Buf_size, szName), if (hmapfile = = NULL) {_tprintf (_t ("Could not create File mapping obj\n ")); return 1;} LPCTSTR PBuf = (LPCTSTR) mapviewoffile (hmapfile, file_map_all_access, 0, 0, buf_size); if (PBuf = = NULL) {_tprintf (_t (" Could not mapping file\n ")); CloseHandle (Hmapfile); return 2;} <pre name= "code" class= "CPP" ><span style= "White-space:pre" ></span>tchar s[buf_size]; Shareddata SD (1, 2, 3.14); memcpy ((LPVOID) PBuf, &SD, sizeof (SD));
UnmapViewOfFile (PBUF); CloseHandle (hmapfile); return 0;}
We first run the second program, and then run the first program, found that the first program to print out the second program a structure of the value, to achieve the purpose of data sharing.
inter-process communication also includes clipboard, mail slots, pipelines, etc., but they are essentially using the memory Mapping file is implemented.