+ 1
Export class method from dll using function pointer
Hi I have one dll where a class and member functions are there. It do have a global function. Class and global function has been exported using __declspec(dllexport) which generates a dll and import lib for me. Now, I could link import lib with my exe and can include header file. This allows me to create a class object into exe code. Other way for me is to not to include header and import lib. Here, I can use LoadLibrary and GetProcAddress (On windows) to get function address into function pointer for a global function. How to do so for a class member function? Passing member function name to a GetProcAddress does not work for me. What should be done here or it is not at all allowed to do so?
9 Respuestas
+ 2
anyway, your example works so you can go with that, but create a function to free the object too when you no need it anymore and export it like your factory.
+ 1
you cannot just call a class member function from anywhere, as it need the 'this' that will created when you call the constructor. Specially when you overwrite virtual methods, It will not know which one to call etc...
Except if you define it as static, then it doesn't need any this pointer.
+ 1
what you have tried until now?
Try to use the dllexport keyword as you said exactly after the class keyword. Then to use the header file that contains the blueprint of your class and the DLL or library to the 2nd process.
0
As class method is not a free method, it might have been mangled differently.
I do had extern "c" , but still getprocadres find function adress from dll. How to get the same ?
0
Is it like class member function cannot be declared as extern "C"?
extern "C" int display();
class testClass
{
public:
extern "C" int displayNew();
};
extern "C" on class method throws error. Am I missing something or it is not at all allowed?
0
Finally a success...!
Just declare an interface (pure virtual method) in the header file of dll. This ensure that definition is found due to defined nature of pure virtual method. Inside cpp file of dll, just inherit the class and override the virtual method. As interface method definition is found , we can use that class method in exe code by just importing header file.
Main challenge is to have a class object which will be solved by using the factory method. This method will create the new object of derived type and this method is exported which can be accessed using getprocaddress.
Refer below article for more details as I had followed the same:
https://eli.thegreenplace.net/2011/09/16/exporting-c-classes-from-a-dll/
0
How constructor can be called using getprocaddress ? Is it like getprocaddress taking class name as argument works as if getting adress of constructor?
Even if that works, cannot have object of interface class as interface class object cannot be created.
p.s. I said so as virtual will not solve problem.. it will complain about the missing definition unlike pure virtual definition found due to = 0.
0
But this is not a typical "exporting".
In a Windows PE (portable executable) file, in it's header stored some information like exports and imports. Both are tables. Imports is a table telling which DLLs your application will need to run and also after it load the DLLs, it will resolve for each DLL, sets of: { function name, function pointer }, known as "IAT".
Exports at the other hand has to do it which functions of the self PE to exported, so other PEs in the future will imprort them.
Exports is a table of sets contains: { function name, related offset (a 'virtual' function pointer) }.
When comes to classes, you cannot export any class member as you can with regular functions and get its address from its name with getprocaddress. The reason is multiple classes can have functions with same name and also you need to create a object instance first and have a 'this' pointer before call the function.
0
Ketan Lalcheta you have confused 2 completely different things.
When you export a function you can call it by using getprocaddress.
In your example you exporting just a factory that generates a object and from this you call the member.
It's a different technique of c++ compiler to handle function overwriting and polymorphysm.
It's a lot of technical details, it will take a whole essay to explain how it works...
But in general compiler will create a vtable (Virtual table). Each vtable is a 1D array, has a name (automatically generated by compiler) and contains function pointers of the class. The 1st element is always the destructor. The constructor it's not in the list because to use this vtable it must:
1. allocate memory.
2. link this vtable (it add extra variable at the begin that's pointer to the vtable).
3. assign initial values for the rest variables.
Also every function of vtable also get an extra argument that is the 'this' pointer.
For more details you can search about vtables.