How to call a method of class D from function of C++ in DLL?

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 27 00:13:01 PDT 2016


On Saturday, 27 August 2016 at 04:44:10 UTC, MGW wrote:
> Method which I use now:
>
> source D:
> ---------
> extern (C) {
>     int on_metFromD(CEditWin* uk, int aa, int bb) {
>         return (*uk).metFromD(int aa, int bb);
>     }
> }
>
> class CEditWin {
>     . . .
>     // Method for to call
>     int metFromD(int a, int b) {
>         return a + b;
>     }
> }
>
> main() {
>     CEditWin f = new CEditWin(); void* pf = &f;
>     // save pf and &on_metFromD in DLL
>     toTransferInDLL(pf, &on_metFromD);
> }
>
> source C++ DLL
> --------------
> extern "C" typedef int (*execDmet1)(void*, int, int);
>
> extern "C" void toTransferInDLL(void* pf, void* on_metFromD) {
>     int rez = ((execDmet1)on_metFromD)(pf, 2, 3);
> }
>
> ---------------------
> How to change a code that it was possible transferring two 
> parameters in C++ ( toTransferInDLL(pf, &on_metFromD) ) 
> directly to call a method D.
>
> How to call delegate from C++

easiest method would be to mark the D class extern(C++) noting 
that in C++ a D class reference becomes a pointer to the C++ 
class.
Also note that in D classes are reference types so
int on_metFromD(CEditWin* uk, int aa, int bb)
should probably be
int on_metFromD(CEditWin uk, int aa, int bb)
as the extra level of indirection is unnecessary.


More information about the Digitalmars-d-learn mailing list