Any way to access a C++ DLL?

mike vertex at gmx.at
Wed Sep 6 04:47:12 PDT 2006


Am 06.09.2006, 13:12 Uhr, schrieb xs0 <xs0 at xs0.com>:

> 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

Stupid me! Never thought about that (why do it the easy way when you can  
do it the hard way ... hehe). Thanks a lot!

-Mike

-- 
Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/



More information about the Digitalmars-d mailing list