Any way to access a C++ DLL?

xs0 xs0 at xs0.com
Wed Sep 6 04:12:20 PDT 2006


mike wrote:
> Hi!
> 
> Lot's of questions coming up ... but that's really important for me, if 
> I can't solve that I have to throw away over half a year of invested 
> spare time, so:
> 
> Is there any way to access a C++ DLL (precisely: a VST plugin - for 
> those who don't know: VST is a plugin API for virtual synthesizers, 
> audio/midi effects, etc.) from D?
> 
> I'm working on a VST host in D. I knew that VST plugins are written in 
> C++ but I somehow had in mind that the VST SDK just maps the C++ objects 
> to C functions ... now I found out that I was terribly wrong - a VST 
> plugin host obtains a pointer to a C++ object from the DLL's main and 
> calls that for processing. So ... is there a way to wrap that with a D 
> class?
> 
> Since I'm not really experienced with plugins and calling conventions 
> and that ... my guess is that there's the vtbl somewhere stored in the 
> DLL, one can calculate entry points for every member of the class from 
> that and needs to possibly push a this ptr on the stack before calling 
> the function pointer. Is that correct and doable? With a little research 
> I'm sure I can make it work, but I would like to ask the experts here if 
> it's at all possible or if I should rather think of something else.

You can't call C++ code directly from D. However, you can call C code, 
meaning you can write a bunch of C functions that call C++ methods, and 
use that from D.

something like

C++:

struct FooBoo
{
     FooBoo() { ... }
     void boo(int a);
}

extern "C" {
void* FooBoo_create()
{
     return (void*) new FooBoo();
}

void FooBoo_boo(void* obj, int a)
{
     ((FooBoo*)obj)->boo(a);
}

void FooBoo_delete(void *obj)
{
     delete ((FooBoo*)obj);
}
}


D:

extern(C) FooBoo_create();
extern(C) FooBoo_boo(void* obj, int a);
extern(C) FooBoo_delete(void *obj);

class FooBoo
{
     void* obj;
     public this() {
         obj = FooBoo_create();
     }

     public boo(int a) {
         FooBoo_boo(obj, a);
     }

     ~this() {
         FooBoo_delete(obj); obj = null;
     }
}


xs0



More information about the Digitalmars-d mailing list