I have been experimenting with a way to dynamically load a DLL and its entrypoints on the fly with error checking instead of implicit linking. I didn't like how if I was to do this, I would have to write 100's of stubs that called LoadLibrary and GetProcAddress... so I came up with this

{ Lookup Table We use this instead of stubs because delphi likes to add asm to functions with params that we are forced to clean up, this way the asm is pure and we always know what state the stack is in.

pushad //Save the registers push _XX //Push the name of the function onto the stack call DllFunc //Call the generic dll handler //only gets here if dll loading failed, DllFunc does not return. popad //Restore the registers and the stack ret //Return

The idea is to have one generic function that loads the library if it is not loaded, find the proc address, and if it doesnt exist then throw a detailed exception.

so, using this example... calling glutInit would follow this flow:

glutInit -> CallTable + 0 -> DllFunc -> Real glutInit

Each entry in CallTable saves the registers, pushes the function name onto the stack, and calls DllFunc, if DllFunc falls through because the dll could not be loaded, it restores the registers and returns. If DllFunc succeeds, then the stack is restored to the state it was in when CallTable was called, and then it jumps to the function inside the dll.

DllFunc will only return if the dll/proc wasnt found, and CallTable will only return if DllFunc returned.

Hope this all makes sence, it seems to work really well and saves heaps of duplicated code.

Share your thoughts/suggestions/ideas... I would be interested to know what others think.

I could be possible to change the address of the variable "glutInit" to the real address in the dll after the first time it has been called to bypass the error checking. I suppose you could call it dynamic implicit linking.

edit:

Code re-writing of the call table should accomplish this... if the code is replaced with the following:

jmp addr
dw ????? //The entrypoint address
addr:
jmp dword ptr[eip-4] //absolute jump using the address on the last line