Re: Exporting Classes from DLLs
"Mike M" wrote:
A couple of weeks ago in this newsgroup (or it may have
been the un-managed
version of this group), [...]
This is unmanaged version.
someone was asking whether they should export a
"delete" function to go along with their create function
which would
create/destroy an instance of a C++ class defined by the
DLL. I'm interested
in understanding how I can use this technique.
Besides exporting a class you can make common abstract base
class and implement it inside DLL. Like this:
// Common header file (used both by DLL and EXE)
//
class ISomething
{
public:
...
virtual ~ISomething() {};
virtual int Foo(int param) = 0;
virtual void Delete() = 0;
};
// Export only one function:
//
__declspec(export/import) ISomething* CreateIt(...);
//// End of common header
// DLL implementation (.CPP file)
//
#include "Something.h"
class CSomething : public ISomething
{
public:
...
virtual int Foo(int param) { ... }
virtual void Delete() { delete this; }
};
ISomething* CreateIt(...)
{
...
return new CSomething();
}
In EXE you call `CreateIt' to get pointer to `ISomething'
interface and use it:
ISomething* pSmth = CreateIt();
int a = pSmth->Foo(42);
....
pSmth->Delete();
pSmth = NULL;
Is there a way to inject the C++ class definitions into
the library without
polluting the export list?
You could get away making `CreateIt' returning `void*',
however, it's not type safe and error prone.
Alex