Windows core Programming Study Notes-share static data between multiple instances of the same executable file or DLL

Source: Internet
Author: User

By default, global or static data is not shared among multiple instances of the same EXE file or DLL. Windows does not provide an easy way to check whether a user is running multiple instances of the same application. If multiple instances of the application can share the same global variable, we can use this variable to save the number of running instances. This method can be used to control the number of currently running process instances.
Each EXE file or DLL file image consists of multiple segments. Each standard segment name starts with a dot. For example, if the code is put in the. Text Segment, uninitialized data is put in the. BSS segment, and initialized data is put in the. Data Segment. Each segment has several attributes: read, write, execute, and shared. The shared attribute is used to share data among multiple instances. In addition to the standard segments created by the compiler and the linker, we can also create our own segments during compilation. For example, we create a segment named "shared", which contains only one long variable.
# Pragma data_seg ("shared ")
Volatile long g_lapplicationinstances = 0;
# Pragma data_seg ()
Note that the compiler only saves initialized variables in this section. In addition, Visual C ++ provides an allocate declaration that allows us to put uninitialized data in any segment we want. For example:
_ Declspec (allocate ("shared") int C = 0; the segment must already exist.
With the above method, we can put the variables shared among multiple instances in a separate segment. Once the variables are put in a separate segment, the system will no longer create new instances for them in each instance of the same executable file or DLL. To share variables, we must also tell the linker to share the variables in this segment. You can use the/section switch in the CLI of the linker to implement:/section: Name and attributes. Name specifies the segment name and attributes specifies the segment attribute. In the above shared example, you can directly embed the linker switch into the source code:
# Pragma comment (linker, "/section: shared, RWS") RWS is a combination of read, write, and shared attributes.
The code above tells the compiler to embed the strings into a special segment in the generated. OBJ file, with the segment name ". drectve ". When the linker combines all. OBJ modules, the linker checks the ". drectve" segments of each module and treats all strings as command line parameters passed to the linker.

Example program:

# Include <windows. h> # include <tchar. h> # include "resource. H "uint g_umsgappinstcountupdate = wm_app + 123; // custom message, notification when a new instance is created and destroyed // all related top-level windows # pragma data_seg (" shared ") volatile long g_lapplicationinstances = 0; // multi-threaded access, use volatile to avoid Compiler Optimization # pragma data_seg () # pragma comment (linker, "/section: shared, RWS ") int_ptr winapi dlg_proc (hwnd, uint umsg, wparam, lparam) {If (umsg = signature) {setdlgitemint (hwnd, idc_edit_num, g_lapplicationinstances, false ); // display the number of instances of the current application} switch (umsg) {Case wm_initdialog: postmessage (hwnd_broadcast, g_umsgappinstcountupdate, 0, 0); // broadcast break; Case wm_command: switch (loword (wparam) {Case idok: Case idcancel: enddialog (hwnd, 0); break;} return false;} int winapi winmain (in hinstance, in hinstance hprevinstance, in lpstr lpcmdline, in int nshowcmd) {response = registerwindowmessage (_ T ("response"); interlockedexchangeadd (& g_lapplicationinstances, 1); dialogbox (hinstance, makeintresource (idd_dialog), null, dlg_proc); interlockedexchangeadd (& g_lapplicationinstances,-1); postmessage (hwnd_broadcast, g_umsgappinstcountupdate, 0, 0); Return 0 ;}

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.