Windows core programming-data sharing among multiple Application Instances

Source: Internet
Author: User
Multiple applications Program Shared data between instances

Windows creates a wall around the address space of a Win32 program. Generally, the data in the address space of a program is private and invisible to other programs. However, there is still a simple way to share data among multiple instances of an application. When you modify data in an application instance, this change is immediately reflected in other application instances.

Data andCodeSegments are in independent segments. When multiple programs are started, static variables and other data are independent and cannot be used to share data among multiple instances. The reason for their independence is that the default segment provided by the system does not have the sharing function. We can manually specify the shared data segment in the program, so that we can share data directly between multiple application instances.
I will use an example to explain it directly.

/*************************************** *********************************** // Reprinted please note Article From: http://blog.csdn.net/windows_nt * // * share data across multiple application instances *//*********************** **************************************** * ********/# include <windows. h> # include <iostream> // 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 th E linker to make the shared section readable, writable, and shared. # pragma comment (linker, "/section: shared, RWS") void main () {// There is another instance of this application runninginterlockedexchangeadd (& g_lapplicationinstances, 1 ); printf ("this is the % d application instance. \ R \ n ", g_lapplicationinstances); printf (" You can now start a new application instance to test data. \ R \ n "); getchar (); // This instance of the application is terminatinginterlockedexchangeadd (& g_lapplicationinstances,-1); return ;}

Note:

The first # pragma describes how to create a data segment. The name here is shared. You can name this section as any name you like. Here, # All initialized variables described by Pragma are stored in the shared data segment.
The second # End Of The Pragma description section. Special initialization of variables is very important, otherwise the compiler will put them in common uninitialized data segments rather than in shared.
The connector must know that there is a shared data segment. You can directly use the DLL original code to specify the link option, as shown in the following figure:
# Pragma comment (linker, "/section: shared, RWS") the RWS letter indicates that the segment has read, write, and share attributes.

Application scenarios:
1. # pragma data_seg () is generally used in DLL. That is to say, define a shared, named data segment in the DLL. The most important thing is that the global variables in this data segment can be shared by multiple processes. Otherwise, the global variables in the DLL cannot be shared among multiple processes.
2. Shared data must be initialized. Otherwise, the Microsoft compiler will put uninitialized data in the. BSS segment, leading to failure in sharing among multiple processes.


 

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.