www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to call a method of class D from function of C++ in DLL?

reply MGW <mgw yandex.ru> writes:
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++
Aug 26 2016
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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.
Aug 27 2016
parent reply MGW <mgw yandex.ru> writes:
On Saturday, 27 August 2016 at 07:13:01 UTC, Nicholas Wilson 
wrote:
 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.
In "the D class extern(C++)" do't work phobos.
Aug 28 2016
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Sunday, 28 August 2016 at 08:20:52 UTC, MGW wrote:
 On Saturday, 27 August 2016 at 07:13:01 UTC, Nicholas Wilson 
 wrote:
 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.
In "the D class extern(C++)" do't work phobos.
? What has this got to do with Phobos? extern(C++) ImACPlusPlusClassDefinedInD class { //... }
Aug 28 2016