Skip to main content
gamedev.net
🔒 Locked

DLL question

Started by nPenAPaT Jun 28, 2007 at 3:06 AM 2 replies 650+ views
Original Post
nPenAPaT
nPenAPaT
Hi. I have a class written in C++. My question is can I put it in a DLL and then use it. I tried many times but with no effect. The linker allways complains about the constructor and the destructor of the Class. Is there something special - like all functions should be virtual or smth.
DaBono
DaBono
You can export classes to a DLL by adding the declspec(dllexport) after the class keyword.
However, I found that quite hard to make it work correctly when loading DLLs dynamically. So, what I did is do it more or less the COM-way: export a C-function that creates the object for you. So:
class MyClass {   .. bla bla ..};DLL_EXPORT MyClass *createMyClass();
and then in the DLL code:
DLL_EXPORT MyClass *createMyClass() {   return new MyClass();}


Both ways should work fine, however, when you just want to use the DLLs at startup. What's the linker complaining about? Are you linking with your import-library correctly?
nPenAPaT
nPenAPaT
Thanks for the answer DaBono. I was making a stupid mistake. Instead of puting the declspec(dllexport) after the class keyword I used it in all functions of the class ( in their declarations). But what do you mead dinamically ? Is there a difference between using a .lib file and loading the library at runtime ?
taby
taby
When you include the use of a DLL in your application by linking in the corresponding .lib file, you are using static linking.

When you do not link in the .lib file, you must use the LoadLibrary and family of functions at runtime to grab a handle to the DLL and the functions inside of it. This is dynamic linking.

In the static method, the pointers to the functions are defined at compile time (in fact, you don't really use function pointers since they aren't necessary).

In the dynamic method, the pointers to the functions are defined at run time.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.