EEFileLoadException when using C# classes in C++(win32 app)

前端 未结 6 838
闹比i
闹比i 2020-12-05 13:25

For deployment reasons, I am trying to use IJW to wrap a C# assembly in C++ instead of using a COM Callable Wrapper.

I\'ve done it on other projects, but on this o

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 14:23

    In case anyone else stumbles upon this question, and you are using a dynamic assembly name: make sure you are stripping the assembly name, it may contain version, culture and other content that you may not use.

    I.e., your MyResolveEventHandler should be in the form of:

    static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
    {
        Console::WriteLine( "Resolving..." );
    
        String^ assemblyName = args->Name;
    
        // Strip irrelevant information, such as assembly, version etc.
        // Example: "Acme.Foobar, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        if( assemblyName->Contains(",") ) 
        {
            assemblyName = assemblyName->Substring(0, assemblyName->IndexOf(","));
        }
    
        Assembly^ thisAssembly = Assembly::GetExecutingAssembly();
        String^ thisPath = thisAssembly->Location;
        String^ directory = Path::GetDirectoryName(thisPath);
        String^ pathToManagedAssembly = Path::Combine(directory, assemblyName );
    
        Assembly^ newAssembly = Assembly::LoadFile(pathToManagedAssembly);
        return newAssembly;
    }
    

提交回复
热议问题