Jump to content
  • Advertisement
Sign in to follow this  

How to integrate DLL loading/binding code into other projects in an elegant way

This topic is 2849 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


I am switching to a dynamic library load and bind method, and was wondering what the best way would be to do this. The DLL functions to bind are all pure C, so should the code to load that library be (so that the code can be used from a C or C++ application).

I was thinking about having just a header file that includes everything, so that the user just has to:

1) include the header in all files that need the DLL functions
2) call the library load function once at application start
3) call the function bind function once
4) use the imported DLL functions
5) call the library unload function once at application end

I tried above method that works fine as long as I just include the header file once. If I include it in several files (so that the DLL functions become available from several code parts), it gives me "error LNK2005" errors for the DLL functions (xxx already defined in yyy).

Following is a simplified version of what I have:


// Various types used in the interface functions:
typedef unsigned char myBool;
typedef char myChar;
typedef int myInt;
typedef float myFloat;
typedef void myVoid;
typedef unsigned char myUChar;

// Some enum:
enum {

// Some struct:
struct SMyCallBack
myInt objectColor;
myFloat objectSize;

// DLL function typedefs:
typedef myInt (__cdecl *ptrGetObjectCount)(void);
typedef myInt (__cdecl *ptrGetObjectColor)(myInt objectIndex);

// DLL function declarations:
ptrGetObjectCount getObjectCount=0;
ptrGetObjectColor getObjectColor=0;

// Function to load the DLL:
HINSTANCE loadMyLibrary(const char* pathAndFilename)
return LoadLibrary(pathAndFilename);

// Function to unload the DLL:
void unloadMyLibrary(HINSTANCE lib)
if (lib!=0)

// Function to bind the DLL functions:
int getMyLibProcAddresses(HINSTANCE lib)

if (getObjectCount==0)
return 0;
if (getObjectColor==0)
return 0;

return 1;

#endif // ifndef MYLIB_INCLUDED_

What can improve in above code? I tried adding the external keyword for the function declarations, but that didn't help


Share this post

Link to post
Share on other sites
Thanks SiCrane,

Actually I know how to handle that problem normally (e.g. to use a header AND source file), but in my current case I was hoping to find a solution that only uses a header file. One single file for simplicity... and in that case I need the function and variable definitions and implementation in the header file. I was hoping there was some neat "trick" to allow for that

Share this post

Link to post
Share on other sites
Well if you don't mind forcing every file that uses your header to load and unload the DLL separately and duplicating memory for global variables, you can make your globals static. This is a pretty poor solution, but it would allow you to not have a source file. I seriously don't recommend it, but it's possible.

Share this post

Link to post
Share on other sites
Note that if you're willing to give up C compatibility there are other options I can think of that would behave more like it seems you want that allow you not to need a source file. Admittedly somewhat evil options.

Share this post

Link to post
Share on other sites
I do not catch the thing you want do but whenever I use runtime loading of shared libraries I do the following.

1. Loading the library as you do.
2. Get the one and only function that the library exports directly
3. Call the one and only function to create an interface object that contains the Methods to call within the library.

Anyway I do that in C++ it is no problem to make a structure with pointer to functions and return a pointer to this structure. That will work in C the same way.

Afterwards you can call the functions with ptr_to_structure->ptr_to_fnc()

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!