title: Why do we see a ModuleLoadExceptionHandlerException when unit testing
body: <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>Exception
  System.TypeInitializationException: The type initializer for '&lt;Module&gt;' threw an exception. ---&gt; &lt;CrtImplementationDetails&gt;.ModuleLoadExceptionHandlerException: A nested exception occurred after the primary exception that caused the C++ module to fail to load.
   ---&gt; System.Runtime.Serialization.SerializationException: Serialization error.
     at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
     at &lt;CrtImplementationDetails&gt;.DoCallBackInDefaultDomain(IntPtr function, Void* cookie)
     at &lt;CrtImplementationDetails&gt;.DoCallBackInDefaultDomain(IntPtr , Void* )
     at &lt;CrtImplementationDetails&gt;.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 518
     at &lt;CrtImplementationDetails&gt;.LanguageSupport._Initialize(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 721
     at &lt;CrtImplementationDetails&gt;.LanguageSupport.Initialize(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 875
     --- End of inner exception stack trace ---
     at &lt;CrtImplementationDetails&gt;.ThrowNestedModuleLoadException(Exception innerException, Exception nestedException)
     at &lt;CrtImplementationDetails&gt;.ThrowNestedModuleLoadException(Exception , Exception )
     at &lt;CrtImplementationDetails&gt;.LanguageSupport.Cleanup(LanguageSupport* , Exception innerException) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 841
     at &lt;CrtImplementationDetails&gt;.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()
  </code></pre>
  
  <p>This looks like a failure to load the assembly by the test runner (Gallio.Echo in this case). </p>
  
  <p>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.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>EDIT</strong> </p>
  
  <p>I was going to post the console app code that broke here, but I have recompiled it and it now works! Here it is:</p>
  
  <pre><code>#include "stdafx.h"
  
  using namespace System;
  
  #include "SCacheAssignment.h"
  
  int main(array&lt;System::String ^&gt; ^args)
  {
      Console::WriteLine(L"Hello World");
      SCacheAssignment* assignment = new SCacheAssignment(NULL, false);
      assignment-&gt;id = 2;
      Console::WriteLine(L"Hello World 2 " + assignment-&gt;id);
      return 0;
  }
  </code></pre>
  
  <p>When I use his code is a unit test:</p>
  
  <pre><code>#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-&gt;id = 2;
      Console::WriteLine(L"Hello World 2 " + assignment-&gt;id);
  }
  </code></pre>
  
  <p>It breaks. This is the only test in the test assembly.</p>
  
  <p>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?</p>