Loading a DLL at runtime?

Started by
11 comments, last by Tree Penguin 19 years, 8 months ago
Lets say you have a few versions of a dll and at startup you load the one that's listed in your configuration file. Is that possible? If not i guess it's possible in this more cumbersome manner: - The app loads (startapp) - startapp copies the right dll to the other executables' (the one that needs the dll) working directory - startapp starts the actuall main app Right? [Edited by - Tree Penguin on August 21, 2004 3:11:59 PM]
Advertisement
Yes, you can load a dll from a configuration file and use that path to load the functions that you need.

Basically you just get the string containing the dll path and load it using the "LoadLibrary" function.

-TOmcAT
-Keith
Thanks!
You must remember that using LoadLibrary() requires you to search for identifiers yourself as opposed to having them link at startup. So for every function you want to call you will have to manually obtain the function pointer (see GetProcAddress()). (Otherwise you would have to use Self-Modifying Code, which is unfortunately not available).
Quote:
- The app loads (startapp)
- startapp copies the right dll to the other executables' (the one that needs the dll) working directory
- startapp starts the actuall main app

A possible variation on this can be done using delay loading. Delay loading means that the DLLs your apps are linked against will not be loaded at startup, but rather at the first time a function from that DLL is used. MSVC++6 has support for this with some sort of /DELAYLOAD flag or something. Check with MSDN for more info.

What you'd do is switch delay loading on, and then do all the copying you describe in some initialization function in your application.

THat'd save you the pain of writing a startapp - just write a startup function. However, this isn't an elegant solution either.

Ok, thanks!

Btw, stupid question (i'm a Linux n00b):
What's the equivalent for DLLs in Linux?
And are they used in the same way?
Linux uses .so (shared object) files and provides an API to load them which is almost the same as Windows.

dlopen() opens the .so, like LoadLibrary() on Windows,
dlsym() gets the functions you want from the .so, like GetProcAddress(),
dlclose() closes the .so, like FreeLibrary().
Wow, that saves me a LOT of extra coding [smile], thanks.
Ok, i can't get it to work probably because of some stupid mistake i made (as usual).

I have this for function (pointer) definention: (i just give you one example)

// Some header file
typedef int (*GAMESTARTGAMEPROC) (void);
extern GAMESTARTGAMEPROC gameStartGame; //declared in a source file

// In the source file
gameStartGame=(GAMESTARTGAMEPROC)GetProcAddress(GameModule,"gameStartGame");


The library is loaded successfully, the function isn't, it stays NULL.

Error: 127: can't find the specified procedure. (if i translate it correctly)

// The function is declared in the dll's header file like this:

__declspec(dllexport) int gameStartGame(void);

[Edited by - Tree Penguin on August 21, 2004 1:21:25 PM]
Nvm, my bad.

EDIT: no, not my bad, still got the problem.

This topic is closed to new replies.

Advertisement