HOWTO: Share Data Between Different Mappings of a DLL

Last reviewed: March 17, 1998
Article ID: Q125677
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

    - Microsoft Windows NT versions 3.51, 4.0

    - Microsoft Windows 95

SUMMARY

Under certain circumstances, 32-bit DLLs might have to share data with other 32-bit DLLs loaded by a different application or with different mappings of the same DLL. Because 32-bit DLLs are mapped into the calling process's address space, which is private, sharing data with other DLLs mapped into the address spaces of different applications involves creating shared data section(s) or using memory mapped files. This article discusses the former -- creating shared data sections by using the #pragma statement. Typically, system-wide hooks installed in a DLL need to share some common data among different mappings.

MORE INFORMATION

Each Win32-based application runs in its own private address space. If a 32- bit application installs a system-wide hook with the hook callback function in a DLL, this DLL is mapped into the address space of every application for which the hook event occurred.

Every application that the DLL gets mapped into, gets its own set of variables (data). Often there will be a scenario where hook callback functions mapped into different application or process address spaces need to share some data variables -- such as HHOOK or a Window Handle -- among all mappings of the DLL.

Because each application's address space is private, DLLs with hook callback functions mapped into one application's address spaces cannot share data (variables) with other hook callback functions mapped into a different application's address space unless a shared data SECTION exists in the DLL.

Every 32-bit DLL (or EXE) is composed of a collection of sections. By convention, each section name begins with a period. (The period is not required.) These sections can have the following attributes: READ, WRITE, SHARED, and EXECUTE.

DLLs that need to share data among different mappings can use the #pragma pre-processor command in the DLL source file to create a shared data section that contains the data to be shared.

The following sample code shows by example how to define a named-data section (.shared) in a DLL.

Sample Code

   #pragma data_seg(".shared")
   int iSharedVar = 0;
   #pragma data_seg()

The first line directs the compiler to place all the data declared in this section into the .shared data segment. Therefore, the iSharedVar variable is stored in the .shared segment. By default, data is not shared. Note that you must initialize all data in the named section. The data_seg pragma applies only to initialized data. The third line, #pragma data_seg(), resets allocation to the default data section.

If one application makes any changes to variables in the shared data section, all mappings of this DLL will reflect the same changes, so you need to be careful when dealing with shared data in applications or DLLs.

You must also tell the linker that the variables in the section you defined are to be shared by modifying your .DEF file to include a SECTIONS section or by specifying /SECTION:.shared,RWS in your link line. Here's an example SECTIONS section:

   SECTIONS
   .shared   READ WRITE SHARED

Alternatively, some compilers allow you to set the linker switch in your code so that if your file is ever copied to another project, the linker switch goes with it. To do this, include the following line in your code preferably near the #pragma data_seg(".shared") line:

   #pragma comment(linker, "/SECTION:.shared,RWS")

Be careful not to include any extraneous spaces inside the quotation marks because this may cause the linker to misinterpret the directive. In the case of a typical hook DLL, the HHOOK, HINSTDLL, and other variables can go into the shared data section.

REFERERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q100634
   TITLE     : HOWTO: Specify Shared and Nonshared Data in a DLL

   ARTICLE-ID: Q100292
   TITLE     : PRB: Data Section Names Limited to Eight Characters
Keywords          : BseMisc
Version           : WIN95; WINNT:3.51,4.0;
Platform          : Win95 winnt
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 17, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.