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

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

问题描述

我有以下函数用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

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



并抛出异常:
尝试读或写保护的内存。这通常表明其他内存已损坏

C#代码中的catch实际上并没有捕获到。为什么?如何处理这个异常?

谢谢你的帮助!

解决方案

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



.NET Framework 4迁移问题



只需将其添加到配置文件中:
http://msdn.microsoft.com/en-us/library/dd638517.aspx


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

}

Function decGetNominativePadeg is in unmanaged dll


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

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!

解决方案

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

.NET Framework 4 Migration Issues.

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

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

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