Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have a native C++ application which uses a managed C++ assembly and loads it with LoadLibrary() without problems. I had to do two things, however, before LoadLibrary() worked:</p> <ul> <li>Make sure that the current directory is the one where the managed assembly resides (use chdir() to change directory)</li> <li>In the managed assembly, the first function invoked by native code only defines the handler for <code>AppDomain::CurrentDomain-&gt;AssemblyResolve</code> event which explicitly loads assemblies from the folder of the managed application. It then invokes <em>another managed function</em> to do the rest of the initialization.</li> </ul> <p>The reason for the last point is that CLR attempts to load an assembly dependency only if a function uses it. So I had to ensure that types in non-system assemblies are not referenced before the <code>AssemblyResolve</code> handler has been defined. </p> <pre><code>ref class AssemblyResolver { public: /// The path where the assemblies are searched property String^ Path { String^ get() { return path_; } } explicit AssemblyResolver(String^ path) : path_(path) { /* Void */ } Assembly^ ResolveHandler(Object^ sender, ResolveEventArgs^ args) { // The name passed here contains other information as well String^ dll_name = args-&gt;Name-&gt;Substring(0, args-&gt;Name-&gt;IndexOf(',')); String^ path = System::IO::Path::Combine(path_, dll_name+".dll"); if ( File::Exists(path) ) return Assembly::LoadFile(path); return nullptr; } private: String^ path_; }; extern "C" __declspec(dllexport) void Initialize() { String^ path = "The path where the managed code resides"; AssemblyResolver^ resolver = gcnew AssemblyResolver(path); AppDomain::CurrentDomain-&gt;AssemblyResolve += gcnew ResolveEventHandler( resolver, &amp;AssemblyResolver::ResolveHandler ); FunctionWhichUsesOtherManagedTypes(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload