Microsoft KB Archive/103244

From BetaArchive Wiki

INF: Example of Importing Functions PSS ID Number: Q103244 Article last modified on 04-23-1994



The information in this article applies to:
- Microsoft Win32 Software Development Kit (SDK) for Windows NT, version 3.1


NOTE: This article deals specifically with the Linker shipped with the Win32 SDK, which is the current linker for MIPS and Alpha only.

The linker does not take a .DEF file directly. To use EXPORTS, it is standard to build an export module (.EXP) to link with the dynamic-link library (DLL), and to use an import library (.LIB) to link with any application or DLL that uses this DLL.

There are alternatives to an import library. One alternative is to use LoadLibrary() and GetProcAddress() to call an exported DLL function. Another alternative is to build an import module (.IMP) using the steps described below:

  1. Create the .DEF file. The following is based on the SELECT example. Note that it is necessary to decorate the names and include one dummy export. The __stdcall functions have an appended “@<number>”, where <number> is the number of bytes in the parameter list for the function. The linker automatically handles the underscore, which is prepended to __cdecl and __stdcall function names.

    NAME Demo

    EXPORTS DemoWndProc@12

    IMPORTS select.StartSelection@16 select.UpdateSelection@16 select.EndSelection@8 select.ClearSelection@12

  2. Build the .IMP file:

    $(PROJ).imp: $(PROJ).def <math display="inline">(implib) -machine:</math>(CPU)
    -def:<math display="inline">(PROJ).def \ -out:</math>(PROJ).lib

  3. Link the .IMP file into the application:

    $(PROJ).exe: $(PROJ).obj $(PROJ).rbj $(PROJ).def $(PROJ).imp $(link) $(linkdebug) <math display="inline">(guiflags) \ -out:</math>(PROJ).exe $(PROJ).obj $(PROJ).rbj
    $(PROJ).imp $(guilibsdll)


If building a DLL, another interesting technique is to use forwarders. For example, the following


EXPORTS MyFunc = prvtdll.DllFunc

will expose MyFunc() as an export for TEST.DLL. However, the loader will actually fix up the reference at load time to point to DllFunc in PRVTDLL.DLL. This method involves no additional code.

Another way to provide a forwarder is to write a stub function, which you do export, that forwards the reference for you. On DLL PROCESS_ATTACH, do LoadLibrary(prvtdll), then GetProcAddress() on DllFunc. In your forwarder function, just call through with the arguments passed to you.

Additional reference words: 3.10 KBCategory: Prg KBSubcategory: TlsMisc


Copyright Microsoft Corporation 1994.