How To Share All Data in a DLL

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

        - Microsoft Windows NT, versions 3.5, 3.51, 4.00
        - Microsoft Windows 95, version 4.0
    

SUMMARY

Win32 dynamic-link libraries (DLLs) use instance data by default. This means that each application that uses a DLL gets its own copy of the DLL s data. However, it is possible to share the DLL data among all applications that use the DLL.

If you only need to share some of the DLL data, we recommend creating a new section and sharing it instead. For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q89817
   TITLE     : How to Specify Shared and Nonshared Data in a DLL

If you want to share *all* of the DLL static data, it is important to do two things:
  • First, the DLL must use the DLL version of the C Run-time (for example CRTDLL.LIB or MSVCRT.LIB). Please see your product documentation for more information about using the C Run-time in a DLL.

    NOTE: CRTDLL.LIB is no longer part of the SDK, starting with Windows NT 3.51. It was last released on the April 1995 MSDN 3.5 SDK. Win32 now requires users to specify their own version of C run-time LIBs supplied by their own compiler vender.

  • Second, you will need to specify that both .data and .bss are shared. Often, this is done in the SECTIONS portion of the .DEF file. For example:

    SECTIONS

          .bss  READ WRITE SHARED
          .data READ WRITE SHARED
    

    If you are using Visual C++ 32-bit Edition, you have to specify this using the -section switch on the linker. For example:

    link -section:.data,rws -section:.bss,rws

Only static data is shared. Memory allocated dynamically with calls to APIs/functions such as GlobalAlloc() or malloc() are still specific to the calling process.

The system tries to load the shared memory block at the same address in each process. However, if the block cannot be loaded into the same memory address, the system maps the shared section into a different memory address. The memory is still shared. Note that the pointers inside the shared section are invalid under this circumstance and cannot be placed in shared sections.

MORE INFORMATION

The C Run-time uses global variables. If the CRT is statically linked to the DLL, these variables will be shared among all clients of the DLL and will most likely cause an exception c0000005.

The reason you need to specify both .data and .bss as shared is because they each hold different types of data. The .data section holds initialized data and the .bss section holds the uninitialized data.

One reason for sharing all data in a DLL is to have consistent behavior in the DLL between Win32 (running on Windows NT) and Win32s (running on Windows 3.1). When running on Win32s, a 32-bit DLL shares its data among all of the processes that use the DLL.

Note that it is not necessary to share all data to behave identically between Win32 and Win32s. The DLL can use thread local storage (TLS) on Win32s to store variables as instance data. For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q109620
   TITLE     : Creating Instance Data in a Win32s DLL


Additional reference words: 3.10 3.50 4.00 95
KBCategory: kbprg kbhowto
KBSubcategory: BseDll


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: December 17, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.