我们有一个包含VC ++(使用MFC)和C ++ / CLI类的混合模式程序集。它是一个MFC扩展DLL,并在运行时加载到我们的MFC可执行文件中,一切正常。
当我们从另一个C ++ / CLI程序集对那里的非托管类进行单元测试时,每当我们尝试创建一个非托管类的实例(通过new)时,我们都会看到以下异常:
Exception
System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> <CrtImplementationDetails>.ModuleLoadExceptionHandlerException: A nested exception occurred after the primary exception that caused the C++ module to fail to load.
---> System.Runtime.Serialization.SerializationException: Serialization error.
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr function, Void* cookie)
at <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr , Void* )
at <CrtImplementationDetails>.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 518
at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 721
at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 875
--- End of inner exception stack trace ---
at <CrtImplementationDetails>.ThrowNestedModuleLoadException(Exception innerException, Exception nestedException)
at <CrtImplementationDetails>.ThrowNestedModuleLoadException(Exception , Exception )
at <CrtImplementationDetails>.LanguageSupport.Cleanup(LanguageSupport* , Exception innerException) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 841
at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 883
at .cctor() in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 922
--- End of inner exception stack trace ---
at MSMContactDataTests.LoadUnknownItemTest()
这看起来像是由测试运行器加载程序集失败(在本例中为Gallio.Echo)。
我还创建了一个小型C ++ / CLI控制台应用程序并尝试了相同的功能。我可以正确地创建程序集中包含的ref类的实例,但是当我尝试创建一个unmanged类时,我得到了相同的异常。
有什么想法吗?
编辑
我打算发布破解的控制台应用程序代码,但我已经重新编译它现在可以了!这是:
#include "stdafx.h"
using namespace System;
#include "SCacheAssignment.h"
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
SCacheAssignment* assignment = new SCacheAssignment(NULL, false);
assignment->id = 2;
Console::WriteLine(L"Hello World 2 " + assignment->id);
return 0;
}
当我使用他的代码时是一个单元测试:
#include "stdafx.h"
#include "PBSMSDataStoreUnitTests2.h"
#include "SCacheAssignment.h"
using namespace PBSMSDataStoreUnitTests2;
void Class1::SomeTest()
{
Console::WriteLine(L"Hello World");
SCacheAssignment* assignment = new SCacheAssignment(NULL, false);
assignment->id = 2;
Console::WriteLine(L"Hello World 2 " + assignment->id);
}
它破了。这是测试装配中唯一的测试。
控制台应用程序是CLR控制台应用程序,其中CLR类库中的测试程序集。据我所知,他们使用相同的编译器选项。为什么一个工作而一个不工作?
答案 0 :(得分:1)
您的单元测试程序集也是C ++ / CLI吗?
(根据新信息进行编辑)
趣。我想知道是否可能是由于托管静态构造函数在混合模式DLL中失败?或者在构建全局原生变量时出现了什么问题?
我发现在此Connect问题中提到了同样的问题: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=316549
但是没有解决方案或正确的诊断,我很害怕。