0

Hey I'm trying to write a Game-Engine and I was trying to export a class in a Dll and was trying to use it in my main code. Like using loadlibrary() function. I know how to export and use functions to and from Dll. But I want to export classes and then use them just like I use functions. I don't want to include <headers> for that class and then use it. I want it to be run-time. I have the following code for a very simple class that I was using to just experiment with it.

#ifndef __DLL_EXP_
#define __DLL_EXP_

#include <iostream>

#define DLL_EXPORT __declspec(dllexport)

class ISid
{
public:
virtual void msg() = 0;
};

class Sid : public ISid
{
void msg()
{
    std::cout << "hkjghjgulhul..." << std::endl;
}
};

ISid DLL_EXPORT *Create()
{
return new Sid();
}

void DLL_EXPORT Destroy(ISid *instance)
{
   delete instance;
}

#endif

How do I use this in my main code? Any help will be really appreciated. In case if it's important I'm on Visual Studio 2012.

Xk0nSid
  • 961
  • 2
  • 11
  • 19
  • http://stackoverflow.com/questions/110833/dynamically-importing-a-c-class-from-a-dll –  Nov 23 '13 at 10:07
  • Although the above thread is nice, in short, you may take the interface class out of the dll into a separate header and then call methods via base pointer assigned to the Create-ed instance. –  Nov 23 '13 at 11:20
  • The solution in it shows how to load functions from Dll and I know that already. I am not able to understand how to load a class. I'm not sure how to load that Create function, since while creating typdef for that I'll need return type but I don't want to include the header. – Xk0nSid Nov 23 '13 at 11:32
  • @typical I'm sorry but I didn't really get what you are trying to say. Can you elaborate? I'm really sorry I'm very new to all this. – Xk0nSid Nov 23 '13 at 11:41

1 Answers1

1

If I understand the problem is not that you do not know how to load the class but cannot quite imagine how to use it afterwards? I cannot help with the syntax because I am used to shared objects dynamic loading, not dll but the usecase is the following:

// isid.h that gets included everywhere you want to use Sid instance
class ISid
{
public:
    virtual void msg() = 0;
};

If you want to use dynamic loaded code, you still have to know its interface. That is why I suggest you move the interface into a regular not-dll header

// sid.h
#ifndef __DLL_EXP_
#define __DLL_EXP_

#include <iostream>
#include "isid.h" // thus you do not know what kind of dll you are loading, but you are well aware of the interface

#define DLL_EXPORT __declspec(dllexport)
class Sid : public ISid
{
void msg()
{
    std::cout << "hkjghjgulhul..." << std::endl;
}
};

ISid DLL_EXPORT *Create()
{
    return new Sid();
}

void DLL_EXPORT Destroy(ISid *instance)
{
    delete instance;
}

#endif

And then you do something like that:

// main.cpp
#include <sid.h>
int main()
{
 // windows loading magic then something like where you load sid.dll
.....
typedef ISid* (*FactoryPtr)();
FactoryPtr maker = (FactoryPtr) dlsym(symHanlde, "Create");
ISid* instance = (*maker)();
instance->msg();
...
}

Sorry I cannot provide the dll code but I do not wish to learn windows dll interface now so I hope this helps as to understand my comment.

  • In Windows you can dynamically load a library using `LoadLibrary` and get the address of an exported function via `GetProcAddress`. –  Nov 23 '13 at 13:22
  • Thank you so much this actually helped. I was just trying to find a way to do without having to include header `isid.h`. Guess can't do it without that. But thanks anyways it did help. – Xk0nSid Nov 23 '13 at 18:33