Loading Mixed-Mode C++/CLI .dll (and dependencies) dynamically from unmanaged c++

后端 未结 2 1698
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-03 03:59

I have a managed C++ assembly I\'m loading dynamically in an unmanaged c++ application through a standard LoadLibrary() call. The managed C++ assembly has dependencies on se

2条回答
  •  感情败类
    2021-01-03 04:50

    I think what you're looking for is a custom assembly resolver. I had to use one to do what I think you are trying to do -- I wanted to locate some of the DLLs in a folder that wasn't in the tree of the initial unmanaged DLL (which loaded managed code eventually).

    Step 1 is to make a function you can call to set up the resolver:

    void PrepareManagedCode()
    {
        // Set up our resolver for assembly loading
        AppDomain^ currentDomain = AppDomain::CurrentDomain;
        currentDomain->AssemblyResolve += gcnew ResolveEventHandler(currentDomain_AssemblyResolve);
    }  // PrepareManagedCode()
    

    Then the resolver. This example has a global ourFinalPath which would in your case be the extra folder you were using:

    /// 
    /// This handler is called only when the CLR tries to bind to the assembly and fails
    /// 
    /// Event originator
    /// Event data
    /// The loaded assembly
    Assembly^ currentDomain_AssemblyResolve(Object^ sender, ResolveEventArgs^ args)
    {
        sender;
    
        // If this is an mscorlib, do a bare load
        if (args->Name->Length >= 8 && args->Name->Substring(0, 8) == L"mscorlib")
        {
            return Assembly::Load(args->Name->Substring(0, args->Name->IndexOf(L",")) + L".dll");
        }
    
        // Load the assembly from the specified path
        String^ finalPath = nullptr;
        try
        {
            finalPath = gcnew String(ourAssemblyPath) + args->Name->Substring(0, args->Name->IndexOf(",")) + ".dll";
            Assembly^ retval = Assembly::LoadFrom(finalPath);
            return retval;
        }
        catch (...)
        {
        }
    
        return nullptr;
    }
    

提交回复
热议问题