简体   繁体   中英

Import C++/CLI dll using MEF

I have a C# interface and a C++\\CLI implementation which I want to use in my managed code.

C# code:

public interface IEngine
{
    void Run();
}

C++\\CLI code:

[Export(IEngine::typeid)]
public ref class Engine : public IEngine
{
    public:
        virtual void Run(){}
};

C# loader:

public class Loader
{
    public IEngine Load()
    {
        var path = @"D:\Engine\Dlls";
        var catalog = new AggregateCatalog(
                    newAssemblyCatalog(Assembly.GetExecutingAssembly().Location), 
                    new DirectoryCatalog(path, "*.dll"));
        var m_container = new CompositionContainer(catalog);
        return m_container.GetExportedValue<IEngine>();
    }
}

I want to be able to load the .dll by it's literal name or by its object name (as written above).
The code above throws an error when I call GetExportedValue of type ReflectionTypeLoadException :

 InnerException = null   
 Message = "Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."
 Source = "mscorlib"

Also, within LoaderExceptions there are 12 exceptions (all the same):

[System.IO.FileNotFoundException]   {System.IO.FileNotFoundException: Could not load file or assembly 'GNetTcp, Version=1.0.0.23326, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
File name: 'GNetTcp, Version=1.0.0.23326, Culture=neutral, PublicKeyToken=null'

=== Pre-bind state information ===
LOG: DisplayName = GNetTcp, Version=1.0.0.23326, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : ComputationDataTypes, Version=7.2.0.1, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Users\rotem-o\AppData\Local\Temp\4knkp2kk.w3r\60533983-b3f6-4ca4-bb72-9f8ea4a2e2a3.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug/GNetTcp.DLL.
LOG: Attempting download of new URL file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug/GNetTcp/GNetTcp.DLL.
LOG: Attempting download of new URL file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug/GNetTcp.EXE.
LOG: Attempting download of new URL file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug/GNetTcp/GNetTcp.EXE.
LOG: Attempting download of new URL file:///D:/NOVASW/SELECTIVE_MODELING_45/OUTPUT/BINARIES/REFERENCES/DLLSENGINE/GNetTcp.DLL.
LOG: Attempting download of new URL file:///D:/NOVASW/SELECTIVE_MODELING_45/OUTPUT/BINARIES/REFERENCES/DLLSENGINE/GNetTcp/GNetTcp.DLL.
LOG: Attempting download of new URL file:///D:/NOVASW/SELECTIVE_MODELING_45/OUTPUT/BINARIES/REFERENCES/DLLSENGINE/GNetTcp.EXE.
LOG: Attempting download of new URL file:///D:/NOVASW/SELECTIVE_MODELING_45/OUTPUT/BINARIES/REFERENCES/DLLSENGINE/GNetTcp/GNetTcp.EXE.
}   System.IO.FileNotFoundException

I couldn't find tutorials where there is a use of interface and managed\\unmanaged .dlls loading. Please address me if you know one.

How I should use it and what am I doing wrong?

Fusion Log told you what went wrong.

WRN: Native image will not be probed in LoadFrom context.

C++/CLI images are mixed mode, and since there may be side effects to loading it ( DllMain executes), MEF isn't going to risk it. You can still load the assembly explicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM