Classes in DLL's
Hi, the title says it all. How do i put a class (or struct for that matter) in a .DLL. I HAVE searched the forums, but most of the posts were about how to make it possible to use on all compilers. I have tried to make a DLL before, even using normal functions. But it all screwed up, and i couldnt get it to run the functions, even tho i had linked the lib, and included the header. I am using Dev-C++, and there arnt many tutorials on how to use DLLs with it. Anyway, thx.
Hi!
Here you go:
Using Interfaces with DLLs
Sometimes, searching the forums ain't enough... ;)
Hope that helps!
Cheers,
Drag0n
Here you go:
Using Interfaces with DLLs
Sometimes, searching the forums ain't enough... ;)
Hope that helps!
Cheers,
Drag0n
Have to say i did not get a word out of that article. Perhaps you could point me to something more general about DLLS, simple tutorials and such.
Dude, your going to start a really wild ride. I had the same problem as you about a year ago. The thing is, Microsoft didn't design DLLs to support C++. They only support C. So you have to be tricksy to put classes in the DLLs. There are two ways that I know of to do it. They are through the static linked library that is created with the DLL and through runtime loading and linking.
Commonalities between the different methods.
extern "C" __declspec(dllexport) //This tells the compiler to export the function or structure immediately following.
//Example: extern "C" __declspec(dllexport) Initialize();
//Since that can make your code look ugly I suggest defining a macro for it.
extern "C" __declspec(dllimport) //Compiler will use the static library to import the function or stucture.
Static Library method.
When you are creating your DLL make all the function and structure or class definitions use the export line from above. When you are using your DLL use the import line from above in the header.
Runtime method.
This is harder. Creating your DLL is the same as static method for functions and structures. But classes are harder. What you have to do is create an interface. Sorta like the bridging method of OO programming. You hide everything but the public functions. Then when you import you will have to create a handle for the DLL, load it, and access the functions via pointers. Example below.
//Creating DLL header.
structure dllInterface
{
virtual void Initialize() = 0;
virtual void Shutdown() = 0;
}
extern "C" __declspec(dllexport) void GetInterface(dllInterface **interface);
typedef void (*DLLINTERFACE)(dllInterface **interface);
extern "C" __declspec(dllexport) void ReleaseInterface(dllInterface **interface);
typedef void (*RELEASEINTERFACE)(dllInterface **interface);
//See? It's an abstract base structure. The functions are for obtaining an instance of your class.
//Class definition.
class dllClass : public dllInterface
//See? It derives from dllInterface which means that dllInterface pointers can point to an instance of it.
//Above functions themselves.
void GetInterface(dllInterface **interface)
{
if (!*interface) {
*interface = new dllClass;
return true;
}
return false;
}
void ReleaseInterface(dllInterface **interface)
{
if (!*interface)
return false;
delete *interface;
return true;
}
//When you want to use the DLL.
dllInterface *dllstuff;
HINSTANCE instance;
GETINTERFACE interface = 0;
instance = LoadLibrary(/*dll name here*/);
interface = (GETINTERFACE) GetProcAddress(instance, "GetInterface");
interface(&dllstuff);
//You then can call your functions like so.
dllstuff->Initialize();
//When you are stopping the application.
interface = (GETINTERFACE) GetProcAddress(instance, "ReleaseInterface");
interface(&dllstuff);
FreeLibrary(instance);
Well, I hope that made sense and helps. Another thing of note is that the retrieve and release interface types must be different.
Commonalities between the different methods.
extern "C" __declspec(dllexport) //This tells the compiler to export the function or structure immediately following.
//Example: extern "C" __declspec(dllexport) Initialize();
//Since that can make your code look ugly I suggest defining a macro for it.
extern "C" __declspec(dllimport) //Compiler will use the static library to import the function or stucture.
Static Library method.
When you are creating your DLL make all the function and structure or class definitions use the export line from above. When you are using your DLL use the import line from above in the header.
Runtime method.
This is harder. Creating your DLL is the same as static method for functions and structures. But classes are harder. What you have to do is create an interface. Sorta like the bridging method of OO programming. You hide everything but the public functions. Then when you import you will have to create a handle for the DLL, load it, and access the functions via pointers. Example below.
//Creating DLL header.
structure dllInterface
{
virtual void Initialize() = 0;
virtual void Shutdown() = 0;
}
extern "C" __declspec(dllexport) void GetInterface(dllInterface **interface);
typedef void (*DLLINTERFACE)(dllInterface **interface);
extern "C" __declspec(dllexport) void ReleaseInterface(dllInterface **interface);
typedef void (*RELEASEINTERFACE)(dllInterface **interface);
//See? It's an abstract base structure. The functions are for obtaining an instance of your class.
//Class definition.
class dllClass : public dllInterface
//See? It derives from dllInterface which means that dllInterface pointers can point to an instance of it.
//Above functions themselves.
void GetInterface(dllInterface **interface)
{
if (!*interface) {
*interface = new dllClass;
return true;
}
return false;
}
void ReleaseInterface(dllInterface **interface)
{
if (!*interface)
return false;
delete *interface;
return true;
}
//When you want to use the DLL.
dllInterface *dllstuff;
HINSTANCE instance;
GETINTERFACE interface = 0;
instance = LoadLibrary(/*dll name here*/);
interface = (GETINTERFACE) GetProcAddress(instance, "GetInterface");
interface(&dllstuff);
//You then can call your functions like so.
dllstuff->Initialize();
//When you are stopping the application.
interface = (GETINTERFACE) GetProcAddress(instance, "ReleaseInterface");
interface(&dllstuff);
FreeLibrary(instance);
Well, I hope that made sense and helps. Another thing of note is that the retrieve and release interface types must be different.
Exporting C++ Classes without using MFC extension DLLs
Exporting & Importing classes and functions when building multiple DLLs
However, I would recommend going with interfaces. In my experience, exported/imported classes are a pain because under VC, you have to also export every class that's used inside your class.
e.g.
Exporting & Importing classes and functions when building multiple DLLs
However, I would recommend going with interfaces. In my experience, exported/imported classes are a pain because under VC, you have to also export every class that's used inside your class.
e.g.
class __declspec(dllexport) My_class{private: My_second_class m_something; // Will have to export My_second_class std::vector<int> m_another; // Will also have to export std::vector<int>, IIRC};
i can tell this is going to be difficult.
So what would u recommend for me. I have a (partly finished) engine that is made up of different classes for different things such as lighting, models, textures, etc. The user makes an instance of these classes if he/she wants to use the functions. So is there a good method to separate these classes from the app, so that the user dosnt see them, and can only use them.
So what would u recommend for me. I have a (partly finished) engine that is made up of different classes for different things such as lighting, models, textures, etc. The user makes an instance of these classes if he/she wants to use the functions. So is there a good method to separate these classes from the app, so that the user dosnt see them, and can only use them.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement