I am calling a .NET assembly from C++.
This works perfectly for any test .NET 4.0 projects that I call.
However, when calling a large project with 20 sub-assemblies, it fails with the error below:
Failed to execute assembly: 0x80004003. GetLastError=126. dwReturn=1.
Here is the C++ code that generates the error:
DWORD dwReturn;
hr = pCLR->ExecuteInDefaultAppDomain(szApplication, szEntryType, szEntryMethod, szParameter, &dwReturn);
if (FAILED(hr))
{
// Fails if I try the production assembly, with 20 subassemblies.
printf(" Failed to execute assembly: 0x%X. GetLastError=%d. dwReturn=%d.\n", hr, GetLastError(), dwReturn);
}
else
{
// Works 100% if I plug in a small toy assembly in .NET 4.0.
wprintf(L" Assembly returned: %d\n", dwReturn);
}
Your managed code died with a NullReferenceException. Its Exception.HResult value is 0x80004003, E_POINTER.
Clearly that's a very common reason for managed code to die. In the transition from managed code back to native code, you'll lose a lot of context for the exception. That puts a burden on the managed code to do some essential error logging, at least the all-mighty Exception.StackTrace. The .NET 4 AppDomain.FirstChanceException event can be useful.
Debug the problem by enabling managed code debugging, Project + Properties, Debugging, Debugger Type = Mixed. And Debug + Exceptions, Thrown checkbox for CLR exceptions so the debugger stops the program when the exception is thrown.
User contributions licensed under CC BY-SA 3.0