Hi, I have a C# Windows service application that passes a callback function pointer to a C++ dll. I have defined both the function pointer at the C# side and C++ side to be __stdcall type. Everything works fine till the callback is triggered by the C++ dll and throws an Unhandled Exception Access Violation in 0x04cb0e. The debug stops in the threadex.c under the endthread call in the C# application.
public delegate void NotificationFunc(int notifycode, IntPtr Userdata);
[DllImport("notice.dll")]
void INotify(NotificationFunc notefunc,IntPtr Userdata);//ignore the IntPtr Userdata
.
.
.
NotificationFunc notefunc = new NotificationFunc(Noticallback);
INotify(notefunc, Intptr.zero);
//notice.dll triggers this callback thru the delegate passed in
void Noticallback(int notifycode, IntPtr userdata)
{
Swtich(notifycode)
{
//my actions
}
.
.//Error Exceptions happens here when trying to end the thread/call
}
I know i have to handle the clean up this callback resources as it is a one way call event. I have tried GC & GCHandle to prevent it from being GC but it seems there is always a memory leak or error.Can anyone help? Thanks