Function pointer in DLL call - how to handle in C#? -
i'm using native dll called out of c# application. 1 of dll-functions defined this:
int set_line(const int width,fct_line_callback callback)
fct_line_callback defined as
typedef int (*fct_line_callback)(double power,void *userdata);
so how can use function out of c#-application? there way define c# method used callback-function dll-call?
thanks!
you have declare delegate type matches native function pointer. should this:
[unmanagedfunctionpointer(callingconvention.cdecl)] delegate int fct_line_callback(double power, intptr userdata);
now can write pinvoke declaration:
[dllimport("foo.dll", callingconvention = callingconvention.cdecl)] extern static int set_line(int width, fct_line_callback callback);
if callback can made while set_line() executing calling native function simple:
public void setline(int width) { set_line(width, mycallback); } private void mycallback(double power, intptr userdata) { // etc... }
however, if callback can made after set_line() executed, in other words when native code stores function pointer, have make sure garbage collector cannot collect delegate object. simplest way storing object in static variable:
static class wrapper { private static fct_line_callback callback = mycallback; public static void setline(int width) { set_line(width, callback); } private static int mycallback(double power, intptr userdata) { // etc... } }
Comments
Post a Comment