Wednesday, 15 June 2011

C++ to C# Callback Struct Marshalling -



C++ to C# Callback Struct Marshalling -

i'm having problem in project.

i'm using c++ .dll compiled ( have no chance alter it, recompile or modify it) have .h there (bad documented) info. dll used interact device connected via usb. device configurated , using function dll provide have absoulute certainty it's answering dll call. th marshalling c++ , c# quite easy until have utilize callback register event thrown device.

this significative part of c++ .h have utilize in c# (first , sec event false names)

//first event info typedef struct tagfirst_event { int a; int b; int c; } first_event; //secondevent info typedef struct tagsecond_event { int e; int f; } second_event; //first event callback? typedef void (callback *firts_event_listener)(first_event *event, void *userdata); //second event callback? typedef void (callback *second_event_listener)(second_event *event, void *userdata); //register given callback in struct listener int winapi registerevents(listener *listenergroup ); typedef struct taglistener { firts_event_listener first_listener; second_event_listener second_listener; void * userdata; } listener;

i have problem marshalling have c++ .dll phone call c# functions have defined handle event thrown device.

i've followed many illustration found have not able utilize functionality in proper way. c# application crash without farther info (i think because error in c++ dll , i'm not able grab error).

can have illustration of implementation given code snippet above?

i searched deep , have found similar question no 1 of help me. i'm here clarification, in advice!

well, don't know how much marshaling have done, assume did create binary-compatible types in c# firstevent , secondevent info types.

for function pointers, need intptr.

after (which i'll expand if didn't create binary-compatible types), have create delegate binary compatible:

delegate void firsteventlistener(ref firstevent firstevent, intptr userdata); delegate void secondeventlistener(ref secondevent secondevent, intptr userdata);

then, can create delegate instance, actual handler, , utilize [marshal.getfunctionpointerfordelegate](http://msdn.microsoft.com/en-us/library/at4fb09f.aspx) pointer (as intptr),

rember maintain delegate instance live (ie, hold reference it) long callback called.

c# c++ dll marshalling

No comments:

Post a Comment