Consequences of throwing an exception in an unmanaged callback delegate

What are the consequences or unobvious consequences of throwing an exception inside the delegate that is used during an unmanaged callback? Here is my situation:

Unmanaged C:

int return_callback_val(int (*callback)(void))
{
  return callback();
}

Managed C #:

[DllImport("MyDll.dll")]
static extern int return_callback_val(IntPtr callback);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int CallbackDelegate();

int Callback()
{
  throw new Exception();
}

void Main()
{
  CallbackDelegate delegate = new CallbackDelegate(Callback);
  IntPtr callback = Marshal.GetFunctionPointerForDelegate(delegate);
  int returnedVal = return_callback_val(callback);
}
+5
source share
2 answers

The native code will bomb on an unhandled exception, and the program terminates.

, __try/__catch . , . 0xe0434f4d. , , . . .

+7

, COM- - HRESULT.E_FAIL.

, , COM- , , , , COM- , , ( ).

0

All Articles