Problems with an managed callback from unmanaged c++ dll


hi. i'll shor possible, since english not first language. have unmanaged dll call following method:

[dllimport("gpixfs.dll", callingconvention = callingconvention.cdecl)] public static extern int gpixfs_asyncexecute(int hgpixfs, int lcomand, intptr lpcmddata, int dwtimeout, int hwnd, ref uint ulrequest);  

asyncexecute method calls callback function signature
typedef (callback * gpixfs_fn_notif ) (lpgpixfs_notif);  


callback function must return int signalize action taken:

0 processed
1 non processed
3 maintenance
4 normal

i declared delegate follows

 

[unmanagedfunctionpointer(callingconvention.cdecl)] public delegate int32 gpixfsnotification(ref gpixfs_notif lpnotif);

public event gpixfsnotification onnotifdeleg;

and body of delegate function:

public cdnadapter() { this.onnotifdeleg += new gpixfsnotification(gpi_onnotifdeleg); open(); }  public int32 gpi_onnotifdeleg(ref gpixfs_notif lpnotif) { return 0; } 

the problem is, every time run code, got access violation error , app crashes after line 'return 0' (at '}' more specific.) 

i have googled lot , have found out problem evolves using garbage collector, new world me. give me direction?

please, don't ask me why using ref parameters, wasnt me person coded c dll.

thanks in advance

the garbage collector releases objects which are no longer accessible managed code.

passing function pointer unmanaged code involves creating delegate , getting function pointer delegate.

since unmanaged code is, definition, not managed, garbage collector doesn't know that the delegate still needed , release managed code doesn't need it. calling callback after delegate released causes access violation.

to prevent this, have to store the delegate somewhere until no longer required.



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

How to Share webservice object to all user

BizTalk Server 2013 Azure VM Log Shipping and HA for hosts