从非托管的DLL在C#中的异常处理

从非托管的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