Originally Posted by
*DEAD*
Hi, this is just a quick one which i couldnt find any examples off, and my test program doesnt want to compile no matter what i try, but im sure someone must know the answer.
i have a class in a dll, and i want to return that classes constructor to the main program and call the constructor from there, because im using dynamic linking and one of the argument types is not defined in the dll. My question is how do i get the address of the constructor, and once i have it can i just consider it to be a void function. When i say get the address, i will have a c function within the dll which gets the address and return it, not using getprocaddress. Thanks.
you have 2 possibilities:
1. the "active" method:
you add something link this to your implementation file of the future dll.
this function takes the agruments to the ctor of your class (ignore mine here), creates on object on the heap and returns a handle.
in your main program you just get the address of that function (getProcAdress()) and save it. Than you can use it to create your objects.
Code:
extern "C"
{
shared_ptr<Entity> maker(const int id, PrioQueue& prioQueue, tPrioSet prio)
{
shared_ptr<Entity> entity(new FennekEntity(id, prioQueue, prio));
return entity;
}
} // extern "C"
2. the "passive" method:
Code:
// .h file of dll
class Proxy
{
public:
Proxy();
};
// .cpp file of the dll
extern "C"
{
shared_ptr<Entity> maker(const int id, PrioQueue& prioQueue, tPrioSet prio)
{
shared_ptr<Entity> entity(new FennekEntity(id, prioQueue, prio));
return entity;
}
Proxy::Proxy()
{
dem::EntityManager::instance().registerEntityType("FENNEK", &dem::maker);
}
} // extern "C"
namespace
// anonymous namespace to make the global
// invisible to other translation units
{
Proxy p; // calls the Proxy-CTOR
} // anonymous namespace
here you just have to open the dll (LoadLibrary() ), the object p is created what calls back into you main application a function what saves the address of maker and the type of dll. (can be simplified if you just have one dll and no inheritance hierarchy in it)
but beware:
depending on you compiler you have to use import/export macros in front of the exported functions in the dll sources. in the case of minGW there are compiler options to export all symbols (no need for macros then).
the most tricky thing is needed while you are saying
because im using dynamic linking and one of the argument types is not defined in the dll.
because of that (at least under windows) you'll have to build build your main program what should load the dll later first and link it then (or its import lib) to the dll source files.
get deeper informations from here:
http://www.nabble.com/Generic-DLL-qu....html#a1319075
an example for use of import/export macros is here:
http://www.mingw.org/MinGWiki/index.php/sample%20DLL
http://209.85.129.104/search?q=cache...lnk&cd=3&gl=de
some general examples:
http://www.codemaestro.com/articles/...e00000001.html