Register _set_purecall_handler function using P / Invoke in C #

I'm having trouble using _ set_purecall_handler with P / Invoke in C #.

Basically, this works:

(C ++)

_set_purecall_handler(MyPureCallHandler);

void MyPureCallHandler(void)
{
    // gets called
}

But it does not:

(WITH#)

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void PureCallHandler();

[DllImport("msvcr100.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr _set_purecall_handler([MarshalAs(UnmanagedType.FunctionPtr)] PureCallHandler handler);

_set_purecall_handler(MyPureCallHandler);

private void MyPureCallHandler()
{
    // *** doesn’t get called ***
}

I am not sure that my signature on the P / Invoke method is correct, but it does not cause any errors when calling the function (it just does not start the callback when the virtual call fails).

Background

We have several applications (C ++, C ++ / CLI, and C #) that use the same C # library to detect exceptions. This logs various handlers (AppDomain.CurrentDomain.UnhandledException, SetUnhandledExceptionFilter, etc.) and catches most exceptions.

However, it does not recognize pure virtual call errors, so we need to register the above function.

+1
1

, msvcr100d.dll (d = debug) msvcr100.dll , .

( , , /):

private const string DllName =
    #if DEBUG
        "msvcr100d.dll";
    #else
        "msvcr100.dll"; 
    #endif

public delegate void PureCallHandler();

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern PureCallHandler _set_purecall_handler(PureCallHandler handler);
+1

Source: https://habr.com/ru/post/1537021/


All Articles