在C#中使用P / Invoke注册_set_purecall_handler函数 [英] Registering _set_purecall_handler function using P/Invoke in C#

查看:347
本文介绍了在C#中使用P / Invoke注册_set_purecall_handler函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 _set_purecall_handler 在C#中使用P / Invoke。



基本上,这是有效的:



  _set_purecall_handler(MyPureCallHandler); 

void MyPureCallHandler(void)
{
//调用
}

但这不会:



(C#)

  [UnmanagedFunctionPointer(CallingConvention.Cdecl)] 
私人委托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()
{
// ***不调用***
}

我不知道我的P / Invoke方法签名是否正确,但它不会抛出任何错误,当我调用函数

背景



p>我们有一些应用程序(C + +,C + + / CLI和C#)使用单个C#库来捕获异常。这注册各种处理程序(AppDomain.CurrentDomain.UnhandledException,SetUnhandledExceptionFilter等)并捕获大多数异常。



但是,它不捕获纯虚拟调用错误,因此我们需要注册上述函数。

解决方案

尝试和错误后,我发现引用 msvcr100d.dll (d = debug)而不是msvcr100.dll像我在调试器下工作。 p>

这里是我的源代码(我不知道这是否是好的做法,但我已经在调试/发布模式下成功测试):

  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);


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 this doesn’t:

(C#)

[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’m not sure if my P/Invoke method signature is correct, but it doesn't throw any errors when I call the function (it just doesn't fire the callback on a pure virtual call error).

Background

We have a number of apps (C++, C++/CLI and C#) that use a single C# library for catching exceptions. This registers various handlers (AppDomain.CurrentDomain.UnhandledException, SetUnhandledExceptionFilter, etc) and catches most exceptions.

However, it doesn't catch pure virtual call errors and so we need to register the above function.

解决方案

After trial and error, I found that referencing msvcr100d.dll (d = debug) instead of msvcr100.dll worked as I was under the debugger.

Here's my source (I don't know if this is good practice, but I've successfully tested under debug / release mode):

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);

这篇关于在C#中使用P / Invoke注册_set_purecall_handler函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆