在C++中使用C#类时的EEFileLoadException(win32 app)

Ada*_*gen 27 c# managed-c++

出于部署原因,我试图使用IJW在C++中包装C#程序集,而不是使用COM Callable Wrapper.

我已经在其他项目上完成了,但是在这个项目上,我得到了一个EEFileLoadException.任何帮助,将不胜感激!

托管C++包装器代码(这是在DLL中):

extern "C" __declspec(dllexport) IMyObject* CreateMyObject(void)
{
    //this class references c# in the constructor
    return new CMyWrapper( );
}

extern "C" __declspec(dllexport)  void DeleteMyObject(IMyObject* pConfigFile)
{
    delete pConfigFile;
}

extern "C" __declspec(dllexport) void TestFunction(void)
{
    ::MessageBox(NULL, _T("My Message Box"), _T("Test"), MB_OK);
}
Run Code Online (Sandbox Code Playgroud)

测试代码(这是一个EXE):

typedef void* (*CreateObjectPtr)();
typedef void (*TestFunctionPtr)();

int _tmain testwrapper(int argc, TCHAR* argv[], TCHAR* envp[])
{
    HMODULE hModule = ::LoadLibrary(_T("MyWrapper"));
    _ASSERT(hModule != NULL);

    PVOID pFunc1 = ::GetProcAddress(hModule, "TestFunction");
    _ASSERT(pFunc1 != NULL);
    TestFunctionPtr pTest = (TestFunctionPtr)pFunc1;

    PVOID pFunc2 = ::GetProcAddress(hModule, "CreateMyObject");
    _ASSERT(pFunc2 != NULL);
    CreateObjectPtr pCreateObjectFunc = (CreateObjectPtr)pFunc2;

    (*pTest)();  //this successfully pops up a message box
    (*pCreateObjectFunc)();  //this tosses an EEFileLoadException

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

对于它的价值,事件日志报告以下内容:.NET运行时版本2.0.50727.143 - 致命执行引擎错误(79F97075)(80131506)

不幸的是,Microsoft没有关于该错误的信息.

Ada*_*gen 33

问题是DLL的位置.

  • C:\ dll文件\ managed.dll
  • C:\ dll文件\ wrapper.dll
  • C:\ EXE\my.exe

我通过将managed.dll复制到c:\ exe来确认这一点,并且它没有问题.显然,CLR不会在非托管DLL的路径中查找托管DLL,只会查找可执行文件所在的DLL.(或在GAC中).

由于不值得进入的原因,这就是我需要的结构,这意味着我需要让CLR找到托管dll.见下面的代码:

AssemblyResolver.h:

/// <summary>
/// Summary for AssemblyResolver
/// </summary>
public ref class AssemblyResolver
{
public:

static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
{
    Console::WriteLine( "Resolving..." );

    Assembly^ thisAssembly = Assembly::GetExecutingAssembly();
    String^ thisPath = thisAssembly->Location;
    String^ directory = Path::GetDirectoryName(thisPath);
    String^ pathToManagedAssembly = Path::Combine(directory, "managed.dll");

    Assembly^ newAssembly = Assembly::LoadFile(pathToManagedAssembly);
    return newAssembly;
}

};
Run Code Online (Sandbox Code Playgroud)

Wrapper.cpp:

#include "AssemblyResolver.h"

extern "C" __declspec(dllexport) IMyObject* CreateMyObject(void)
{
    try
    {
        AppDomain^ currentDomain = AppDomain::CurrentDomain;
        currentDomain->AssemblyResolve += gcnew ResolveEventHandler( AssemblyResolver::MyResolveEventHandler );

        return new CMyWrapper( );
    }
    catch(System::Exception^ e)
    {
        System::Console::WriteLine(e->Message);

        return NULL;
    }
}
Run Code Online (Sandbox Code Playgroud)


Ada*_*gen 9

第一个问题是确保Debugger类型设置为mixed.然后你得到有用的例外.

  • 这个建议可能节省了我一个小时的调试时间. (2认同)

A.B*_*.B. 5

对于使用混合模式 dll(您的 EXE)的本机应用程序,请将 **“调试器类型”更改为“混合”模式。(转到项目属性 -> 配置属性 -> 调试)

还有一些其他要点(可能与您无关),但根据我的经验,它们可能会导致问题。- 在 Windows 8(安全性更高)上,请尝试以管理员身份启动您的 VS。- 确保对于 x86 配置,您使用的是 x86 二进制文件。- 注意 StrongName 验证,如果您在托管 C++ 中使用的 C# 程序集已签名,请考虑对混合模式 dll 进行签名。

希望这会有所帮助。