简体   繁体   English

使用.NET 4.5和混合程序集时出现未知异常

[英]Unknown Exception when using .NET 4.5 and Mixed Assemblies

I have a problem when the new .Net Framework (version 4.5) is installed. 安装新的.Net Framework(版本4.5)时出现问题。 I am working on a plugin assembly that is accessed through native code. 我正在研究通过本机代码访问的插件程序集。 The library is build using Visual Studio 2010 and with .NET 4.0 as target plattform. 该库是使用Visual Studio 2010并以.NET 4.0作为目标平台构建的。

The following code snippet throws an exception in .NET 4.5 and is working fine in .NET 4.0. 以下代码段在.NET 4.5中引发异常,并在.NET 4.0中正常运行。 Any idea how to fix this issue? 任何想法如何解决此问题?

#pragma managed(push, off)
#pragma managed(push, on)

static bool ManagedTestFunction() {
    return false;
}

#pragma managed(pop)

static void CheckIfManagedUnmanagedTransitionIsPossible() 
{
    try 
    {
        ManagedTestFunction();
    }
    catch (...) 
    {
        throw misc::mwException(0, _T("The program is not working if .NET Framework 4.5 is installed. Please revert to previous version .NET 4.0!"));
    }
};

Best regards 最好的祝福

Michael 麦可

我将看看最初引发了什么异常-应该为您提供一个线索,说明为什么代码无法在.NET Framework 4.5下工作。

finally I solve the problem myself. 最后我自己解决了这个问题。 It is maybe a conceptional problem due to some optimization in .NET 4.5. 由于.NET 4.5中的一些优化,这可能是一个概念性问题。 Concider the following example: 考虑以下示例:

// managed class
ref class A
{
}

// native class holding a reference to A
class B 
{
    msclr::gcroot<A^> m_refA;
};

When first crossing the native-managed boundary, the new .NET Framework tries to resolve all managed types that are reference in native classes independent whether they are actually used or not. 当首次越过本机托管的边界时,新的.NET Framework将尝试解析本机类中引用的所有托管类型,而与它们是否实际使用无关。 At that time, it is not yet possible to subscribe to the AppDomain.ResolveAssembly event. 那时,尚无法预订AppDomain.ResolveAssembly事件。 If the framework cannot resolve a type, the crash happens. 如果框架无法解析类型,则会发生崩溃。

A possible solution is modify class B such that it stores a reference to a System::Object^ instead and to cast dynamically using "cli_safe((System::Object^) m_refA)" 一种可能的解决方案是修改类B,以便它存储对System :: Object ^的引用,并使用“ cli_safe((System :: Object ^)m_refA)”进行动态转换。

Best regards 最好的祝福

Michael 麦可

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

相关问题 混合模式程序集(C ++ / CLI项目)是否可以在.NET Core上运行? - Will mixed mode assemblies (C++/CLI projects) work on .NET Core? NET 4.0与DLL一起使用时出现未知错误 - Unknown error when using .NET 4.0 with dll 出现异常,在每个 function 中再次使用 class object 时出现未知信号错误 - Exception has occured, unknown signal error when using class object again inside each function .NET 4.0在XP上提供了Unknown Exception,但在Windows 7上运行良好 - .NET 4.0 gives Unknown Exception on XP but it working good on Windows 7 在 Visual Studio Code 上调试时出现未知信号异常 - Unknown signal exception when debugging on Visual Studio Code 在Win32 C ++(非托管)应用程序中使用C ++ \\ C#.Net程序集\\ DLL - Using C++\C# .Net assemblies\DLL's in win32 C++ (unmanaged) application 使用strncpy时发生异常 - Exception when using strncpy 混合双打时使用int和unsigned int之间的速度差异 - Speed difference between using int and unsigned int when mixed with doubles 当选项字符串包含混合语言字符时,boost :: program_option :: store抛出异常 - boost::program_option::store throws exception when option string contains mixed language characters 使用线程时的未知类型名称 - Unknown Type Name when using threads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM