从非托管的DLL在C#中的异常处理 [英] Handling exception from unmanaged dll in C#

查看:437
本文介绍了从非托管的DLL在C#中的异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果
我已经写在C#


I have the following function written in C#

public static string GetNominativeDeclension(string surnameNamePatronimic)
{
    if(surnameNamePatronimic == null) 
       throw new ArgumentNullException("surnameNamePatronimic");
IntPtr[] ptrs = null;
try
{
    ptrs = StringsToIntPtrArray(surnameNamePatronimic);

    int resultLen = MaxResultBufSize;
    int err = decGetNominativePadeg(ptrs[0], ptrs[1], ref resultLen);
    ThrowException(err);
    return IntPtrToString(ptrs, resultLen);
}
catch
{
    return surnameNamePatronimic;
}
finally
{
    FreeIntPtr(ptrs);
}



}

}

功能decGetNominativePadeg是在非托管的DLL

Function decGetNominativePadeg is in unmanaged dll


[DllImport("Padeg.dll", EntryPoint = "GetNominativePadeg")]
private static extern Int32 decGetNominativePadeg(IntPtr surnameNamePatronimic,
    IntPtr result, ref Int32 resultLength);

和抛出异常:
已尝试读取或写入受保护的内存。这通常是指示其他内存已损坏。结果
美中不足的是在C#代码实际上并没有抓住它。为什么?如何处理这个异常?结果
谢谢您的帮助!

and throws an exception: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
The catch that is in C# code doesn't actually catch it. Why? How to handle this exception?
Thank you for your help!

推荐答案

的CLR不再提供例外损坏的进程状态在托管代码中的异常处理程序。

"The CLR no longer delivers exceptions for corrupted process state to exception handlers in managed code."

NET Framework 4的迁移问题

就在这个添加到配置文件:
http://msdn.microsoft.com/en-us/library/dd638517.aspx

Just add this to the config file: http://msdn.microsoft.com/en-us/library/dd638517.aspx

这篇关于从非托管的DLL在C#中的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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