Thread overview | ||||||
---|---|---|---|---|---|---|
|
August 27, 2016 How to call a method of class D from function of C++ in DLL? | ||||
---|---|---|---|---|
| ||||
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++ |
August 27, 2016 Re: How to call a method of class D from function of C++ in DLL? | ||||
---|---|---|---|---|
| ||||
Posted in reply to MGW | 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.
|
August 28, 2016 Re: How to call a method of class D from function of C++ in DLL? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | 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.
|
August 28, 2016 Re: How to call a method of class D from function of C++ in DLL? | ||||
---|---|---|---|---|
| ||||
Posted in reply to MGW | 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
{
//...
}
|
Copyright © 1999-2021 by the D Language Foundation