I have the following situation: I have 2 c++ DLL files communicating with a C# application using events. The C# application passes function pointers within a Setup() method to both files which may later raise an event using this function pointer. The application is written for Windows CE 8 and the target framework Windows Embedded Compact V3.9 (.NET CF 3.9).
Each DLL communication is wrapped within a single class containing a Setup() method and a NativeMethods sub-class containing all DLL methods. Both DLL files have an ItemChanged
event.
Sample Code:
private delegate void EventDelegate(int item, int value);
private EventDelegate _eventCallback;
private IntPtr _eventCallbackAddress;
private void OnEvent(int item, int value)
{
Debug.WriteLine("Item: " + item + ", value: " + value);
}
private void Setup()
{
_eventCallback = new EventDelegate(OnEvent);
_eventCallbackAddress = Marshal.GetFunctionPointerForDelegate(_eventCallback); // NotSupportedException
try
{
NativeMethods.Configure(_eventCallbackAddress);
}
catch (Exception ex)
{
Debug.WriteLine(this, ex.Message);
}
}
private static class NativeMethods
{
[DllImport("A.dll", EntryPoint = "Configure", CallingConvention = CallingConvention.WinApi)]
public static extern void Configure(IntPtr eventCallback);
}
This snippet is used in both classes without changes except DllImport reference.
My problem is that after successfully passing classA.Setup()
method, I receive a System.NotSupportedException on Marshal.GetFunctionPointerForDelegate
method invocation in ClassB.Setup()
.
MSDN documentation did not help and I found no further documentation while crawling through the internet. Which is why I came here.
I've observed that the exception does not occur when calling Marshal.GetFunctionPointer
method for another "test" delegate, but it is still thrown on Marshal.GetFunctionPointer(_eventCallback)
private Delegate testDelegate;
private void Foo() { };
private void Setup()
{
testDelegate = new Action(Foo);
IntPtr p = Marshal.GetFunctionPointerForDelegate(testDelegate);
_eventCallback = new EventDelegate(OnEvent);
_eventCallbackAddress = Marshal.GetFunctionPointerForDelegate(_eventCallback); // NotSupportedException
try
{
NativeMethods.Configure(_eventCallbackAddress);
}
catch (Exception ex)
{
Debug.WriteLine(this, ex.Message);
}
Do you have any suggestions? Did I forget something?
Thank you.