How To Export Data from a DLL or an Application

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

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

SUMMARY

It is possible for a Win32-based application to be able to address DLL global variables directly by name from within the executable. This is done by exporting global data names in a way that is similar to the way you export a DLL function name. Use the following steps to declare and utilize exported global data.

  1. Define the global variables in the DLL code. For example:

          int i = 1;
          int *j = 2;
          char *sz = "WBGLMCMTP";
    

  2. Export the variables in the module-definition (DEF) file. With the 3.1 SDK linker, use of the CONSTANT keyword is required, as shown below:

    EXPORTS

          i  CONSTANT
          j  CONSTANT
          sz CONSTANT
    

    With the 3.5 SDK linker or the Visual C++ linker, use of the DATA keyword is required, as shown below

    EXPORTS

          i  DATA
          j  DATA
          sz DATA
    

    Otherwise, you will receive the warning

          warning LNK4087: CONSTANT keyword is obsolete; use DATA
    

    Alternately, with Visual C++, you can export the variables with:

          _declspec( dllexport ) int i;
          _declspec( dllexport ) int *j;
          _declspec( dllexport ) char *sz;
    

  3. If you are using the 3.1 SDK, declare the variables in the modules that will use them (note that they must be declared as pointers because a pointer to the variable is exported, not the variable itself):

          extern int *i;
          extern int **j;
          extern char **sz;
    

    If you are using the 3.5 SDK or Visual C++ and are using DATA, declare the variables with _declspec( dllimport ) to avoid having to manually perform the extra level of indirection:

          _declspec( dllimport ) int i;
          _declspec( dllimport ) int *j;
          _declspec( dllimport ) char *sz;
    

  4. If you did not use _declspec( dllimport ) in step 3, use the values by dereferencing the pointers declared:

          printf( "%d", *i );
          printf( "%d", **j );
          printf( "%s", *sz );
    

    It may simplify things to use #defines instead; then the variables can be used exactly as defined in the DLL:

          #define i *i
          #define j *j
          #define sz *sz
    

          extern int i;
          extern int *j;
          extern char *sz;
    

          printf( "%d", i );
          printf( "%d", *j );
          printf( "%s", sz );
    

MORE INFORMATION

NOTE: This technique can also be used to export a global variable from an application so that it can be used in a DLL.

REFERENCE

For more information on the use of EXPORTS and CONSTANT in the Module Definition File (DEF) file for the 3.1 SDK, see Chapter 4 of the Win32 SDK "Tools" manual.


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


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