Frontend and backend communication

Jacob Carlborg doob at me.com
Tue Nov 29 04:24:18 PST 2011


On 2011-11-29 12:53, Dainius (GreatEmerald) wrote:
> I seem to have another problem with the function pointer approach. I
> am trying to set up a function that would pass the function pointers
> from C to D, and DMD refuses to compile it:
>
> If I have the function pointer struct with D calling convention
> pointers, like this:
>
>      struct S_FrontendFunctions {
>          void function(int) SoundPlay;
>          void function() RedrawScreen;
>      }
>      S_FrontendFunctions FrontendFunctions;
>
> And I try to set it in D like this:
>
>      FrontendFunctions.SoundPlay = function(int){};
>      FrontendFunctions.RedrawScreen = function(){};
>
> And have a function for transferring the pointer from C like this:
>
>      extern (C):
>      void SetRedrawScreen(void function() RedrawScreen)
>      {
>          FrontendFunctions.RedrawScreen = RedrawScreen;
>      }
>
> DMD throws an error in the last function:
>
>      Error: cannot implicitly convert expression (RedrawScreen) of type
> extern (C) void function() to void function()
>
> Now if I define the two function pointers as extern(C) like this:
>
>      struct S_FrontendFunctions {
>          extern (C) void function(int) SoundPlay;
>          extern (C)  void function() RedrawScreen;
>      }
>
> DMD still complains, but this time about when I set the pointers from
> D directly:
>
>      Error: cannot implicitly convert expression (__funcliteral3) of
> type void function() pure nothrow @safe to extern (C) void function()
>
> Any ideas about how to make it work from both D and C sides?

In stead of doing this:

FrontendFunctions.SoundPlay = function(int){};
FrontendFunctions.RedrawScreen = function(){};

Do something like this:

extern (C)
{
     void playSound (int) {};
     void redrawScreen () {};
}

FrontendFunctions.SoundPlay = &playSound;
FrontendFunctions.RedrawScreen = &RedrawScreen;

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list