views:

35

answers:

1

Generated import libraries (the ones used to link a program that will load a dll) AFAICS implements the various calls to the imported functions as assembly jmp statments.

This indeed looks like a very optimal solution. It does not require pushing the arguments a second time, neither returning from a call.

I want to create my own import library, to add a couple of other functions in it. For this, I have to call the LoadLibrary() and GetProcAddress() functions to get the addresses of the functions in the dll. But then, I have to offer my own functions that will call the imported ones throught the function address. But this implies two calls, and pushing again the parameters on the stack. I'd like this double call to be optimized.

Does anyone know about a way to implement in C an import library that would do the same jmp trick? (Or does the compiler optimizes the trail call?)

A: 

Sounds like you might be interested in what I believe is called "function forwarding". It allows you to export a function from a DLL that is really implemented in a different DLL all togeher.

Should be about the most efficient solution

  1. You won't need the calls to LoadLibrary() / GetProcAddress()
  2. When you import from your export dll, the loader stores the address of the real impmplementation in the import table, so its one call and one jmp

Probably the easiest way to implement this is with something like

#pragma comment(linker, "/export:ExportedFuncName=ActualDll.ActualFuncName")

In case you're wondering, this is effectively the same as passing "/export:ExportedFuncName=ActualDll.ActualFuncName" on the linker command line. It's also possible to do this using exports section of a module definition (.DEF)file. See the bottom of here for details.

torak
Nice technique. But forwarding does not apply: I don't want to create a DLL, I want to create a static library.
Didier Trosset
@Didier Trosset: OK, so something else will be needed. Is this a 32-bit or a 64-bit? It affects the ease with thich assembley code can be used.
torak