问题描述
我们有一个包含VC ++(使用MFC)和C ++ / CLI类的混合模式程序集。它是一个MFC扩展DLL,并在运行时加载到我们的MFC可执行文件,一切正常。
We have a mixed mode assembly that contains both VC++ (using MFC) and C++/CLI classes. It is an MFC Extension dll and is loaded into our MFC executable at runtime and all works well.
当我们来单元测试非托管的类在那里从另一个C ++ / CLI程序集,我们每次尝试创建一个非托管类的实例(通过new)时会看到以下异常:
When we come to unit test the unmanaged classes in there from another C++/CLI assembly, we see the following exception everytime we try to create an instance (via new) of an unmanaged class:
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在这种情况下)。
This looks like a failure to load the assembly by the test runner (Gallio.Echo in this case).
我也创建了一个小的C ++ / CLI控制台应用程序,并尝试有效的同样的事情。我可以正确地创建一个ref类包含在程序集中的实例,但是当我尝试新建一个未经绑定的类,我得到相同的异常。
I have also created a small C++/CLI console app and tried effectively the same thing. I can correctly create an instance of a ref class contained in the assembly, but when I try to new up an unmanged class, I get the same exception.
任何想法?
EDIT
我要发布控制台应用程式程式码,但我已经重新编译它,它现在工作!这是:
I was going to post the console app code that broke here, but I have recompiled it and it now works! Here it is:
#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;
}
当我使用他的代码是一个单元测试:
When I use his code is a unit test:
#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);
}
这是测试程序集中的唯一测试。
It breaks. This is the only test in the test assembly.
控制台应用程序是CLR控制台应用程序,其中在CLR类库中的测试程序集。据我所知,他们使用相同的编译器选项。为什么一个工作和一个不工作?
The console app is a CLR Console Application where the test assembly in an CLR Class Library. As far as I can tell, they use the same compiler options. Why does one work and one not?
推荐答案
您的单元测试程序集也是C ++ / CLI?
Is your unit test assembly also C++/CLI?
(根据新信息编辑)
吸引人。我想知道是否可能是由于管理的静态构造函数在混合模式DLL失败?
Intriguing. I wonder if it could be due to a managed static constructor failing in the mixed-mode DLL? Or something going awry in constructing a global native variable?
我发现在这个Connect问题中提到了同样的问题:
I found mention of the same problem in this Connect issue:http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=316549
但没有解决方案或正确诊断,恐怕。
but no solution or proper diagnosis, I'm afraid.
这篇关于为什么我们在单元测试时看到ModuleLoadExceptionHandlerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!