Any more forwarding addresses?

Dear Dr. GUI:

As you know, in Win32, a DLL can forward a symbol reference onto another DLL. In essence, it both imports it from another DLL and exports it from its own. For example, in KERNEL32.DLL in Windows NT, the export named "HeapAlloc" is forwarded to the string "NTDLL.RtlAllocateHeap". This allows applications to use the NT-specific module "NTDLL.DLL" without actually containing import references to it. The application's import table references only "KERNEL32.DLL". Therefore, the application is not specific to Windows NT and can run on any Win32 system.

I need to do something similar in a pair of DLLs I am providing but cannot, for the life of me, find the directive or option that tells the linker or compiler how to do this. Any help would be much appreciated.

Gerry Attilio,
Software Jockey

Dr. GUI replies:

First Nigel, now you. Does everyone think that Dr. GUI is a forwarding service? Sheesh. And to think I spent big bucks on med school for this! It's almost enough to make you go postal.

Oh, this is a programming question! That I can do—and it's not too difficult. For example, suppose you have two .dlls: dll1.dll and dll2.dll. dll2.dll contains a function, foo(). For the sake of simplicity, assume that no name decorating is in effect. Thus dll2.dll exports "foo", and we have in our hands the import library for dll2.dll. Now if dll1.dll needs to forward a call to foo(), all you need to do is add a module definition (.def) file to dll1's build project. This .def file should contain the following:

EXPORTS

foo = dll2.foo

You must relink dll1.dll with the import library of dll2.dll. Now when a client calls foo() in dll1 (which has no implementation whatsoever of foo()), the call will be automagically forwarded to dll2's foo(). This mapping is a one-time translation done by the loader at program load time. Note that you'll have to use the decorated name rather than "foo" if the compiler decorates function names, which it will for C++. You can get the decorated name from a full linker map.