Sign in to follow this  

Loading a DLL at runtime?

This topic is 4865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Isn't there anyone who can help me?

Any help would be greatly appreciated! This is starting to piss me off.

Lets list my problem a little better:

I got a DLL with this function:

__declspec(dllexport) int gameStartGame(void){
// blabla
return 1;
}

I want to load that dll at runtime, using LoadLibrary, i do it like this and it works (at least, it doesn't return NULL):

module=LoadLibrary(file);

Then i want to get a pointer to the function i mentioned earlier (gameStartGame) like this:

typedef int (*GAMESTARTGAMEPROC) (void);
GAMESTARTGAMEPROC gameStartGame;

//...

gameStartGame=(GAMESTARTGAMEPROC)GetProcAddress(module,"gameStartGame");

And it is NULL, with the error (127) can't find the specified procedure.

As i said any help will be appreciated very much!

EDIT: After LoadLibrary this error is set (and before a different unsignificant error):
ERROR 6: The handle is invalid.
Does that help?

EDIT2: I now use LoadLibraryEx and i get no errors (0: Succeeded)

[Edited by - Tree Penguin on August 21, 2004 3:06:49 PM]

Share this post


Link to post
Share on other sites

This topic is 4865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this