我用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);
}
}
函数decGetNotifivePadeg位于非托管dll中
[DllImport("Padeg.dll", EntryPoint = "GetNominativePadeg")]
private static extern Int32 decGetNominativePadeg(IntPtr surnameNamePatronimic,
IntPtr result, ref Int32 resultLength);
并抛出一个异常:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt
。
C代码中的捕获实际上并没有捕获它。为什么?如何处理此异常?
谢谢你的帮助! 最佳答案
“CLR不再将损坏进程状态的异常传递给托管代码中的异常处理程序。”
.NET Framework 4 Migration Issues。
只需将此添加到配置文件:
http://msdn.microsoft.com/en-us/library/dd638517.aspx