[Wine]How to : C++ DLLs
Peter Quiring
pquiring at hotmail.com
Tue Nov 9 14:25:17 CST 2004
I remember seeing a message somewhere about C++ DLLs and wanting to
implement them in Winelib. Well here is an idea that I use in Windows that
works great:
Define your class in a common header (let's call it dll.hpp):
class DllClass {
public:
int somedata;
virtual void member(int arg);
};
Define the DLL source with two exports:
#include <dll.hpp>
extern "C" __declspec(dllexport) DllClass *new_DllClass() {
return new DllClass;
}
extern "C" __declspec(dllexport) void delete_DllClass(DllClass *x) {
delete x;
}
void DllClass::member(int arg) {
//do something with arg..
}
Then in your main source code just load the DLL, get the addr of
new_DllClass and delete_DllClass to create and destroy classes of type
DllClass. The members of the class must be 'virtual' so that the main
source will call into the loaded DLL (otherwise it will not even compile).
Now that you only have two simple "C" style exports the SPEC file that
Winelib uses will be easy to setup.
There will of course be an extra jmp because of the virtual table, but the
benefits outway the cost.
Main Source:
#include <dll.hpp>
int main() {
DllClass *(new_DllClass*)(); //func ptr to new_DllClass
void (delete_DllClass*)(DllClass *); //func ptr to delete_DllClass
DllClass *myClass;
LoadDLL();
new_DllClass = GetProcAddress("new_DllClass");
delete_DllClass = GetProcAddress("delete_DllClass");
myClass = (*new_DllClass)();
//use myClass here
(*delete_DllClass)(myClass);
}
Hope this helps.
Peter Quiring
More information about the wine-users
mailing list