简体   繁体   English

在C#中处理来自非托管dll的异常

[英]Handling exception from unmanaged dll in C#

I have the following function written in C# 我有用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 函数decGetNominativePadeg在非托管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 并抛出异常: Attempted to read or write protected memory. This is often an indication that other memory is corrupt Attempted to read or write protected memory. This is often an indication that other memory is corrupt . 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. C#代码中的catch实际上并没有捕获它。 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." “CLR不再为托管代码中的异常处理程序提供损坏的进程状态的异常。”

.NET Framework 4 Migration Issues . .NET Framework 4迁移问题

Just add this to the config file: http://msdn.microsoft.com/en-us/library/dd638517.aspx 只需将其添加到配置文件: http//msdn.microsoft.com/en-us/library/dd638517.aspx

If the IntPtr result parameter is to receive the value from within the function, it must be marked ref. 如果IntPtr result参数是从函数中接收值,则必须将其标记为ref。

I don't see ptrs[1] being assigned any value before passing. 我没有看到ptrs[1]在传递之前被赋予任何值。

Try changing the definition to: 尝试将定义更改为:

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

The reason probably is that it's trying to write to "result" which is marked as input-only. 原因可能是它试图写入“结果”,标记为仅输入。

you have probably turned off debugging of unmanaged code. 你可能已经关闭了非托管代码的调试。

"Enable unmanaged code debugging option" must be check in project properties under the Debug section. “启用非托管代码调试选项”必须在“调试”部分下检查项目属性。 After this, the exception is shown in the debugging process. 在此之后,调试过程中会显示异常。

Problem solved. 问题解决了。 I found out that when using 4-th framework I have this problem, when using 3.5 - I don't. 我发现当使用第4个框架时我有这个问题,当使用3.5时 - 我没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM