Sharing data between processes of Windows core programming

Source: Internet
Author: User

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 are able to define a global variable in the program. Initialized to 0. Add 1 when the program is started. Of course we can use third-party media to store this variable and then parse it.

This must be done before parsing is written. Data cannot be updated in real time. Assume that no other storage medium is considered. What should be done only in the process of communication? Windows provides a number of possible methods, which are described in two frequently used ways.

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 to add shared data segments in your code. When an instance of a program is executed at the same time, an instance is opened. G_lappinstances will point to the same memory so that data can be shared. But the disadvantage of such a method is that it can only share the data of a variable, which is not possible for a struct.


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 execute the second program, and then execute the first program, found that the first program printed a second program a structure of the value, to achieve the purpose of data sharing.

inter-process communication also includes a clipboard, a postal slot. Pipelines, but they are essentially implemented using memory- mapped files.

Sharing data between processes of Windows core programming

Related Article

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.