Marks
Marks

Reputation: 3661

Reference to C# Assembly in different path from C++/CLI DLL

I want to write a plugin for a software, which loads normal 32Bit DLL's as plugins.

Because my experience in C# is much better, i planned to use a C++/CLI Assembly as wrapper which then delegates the function calls to the 'real' C# plugin.

But i have a problem with this. I tested that the C++/CLI DLL gets loaded correctly. Now i added the C# assembly as reference to the C++/CLI DLL. The problem is, that when I have the C# DLL in the same directory, the Software wants to load it as a plugin directly and the Software crashes. If i place the C# DLL in a subdirectory it can't be found by C++/CLI DLL.

I tried different solutions i found on the web to let the C++/CLI Assembly find the C# Assembly in a subdirectory, but they all didn't work:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <probing privatePath="DLL" />
  </assemblyBinding>
</runtime>

Did not work. It seems like the app.config is ignored when creating a C++/CLI DLL.

Adding AppDomain.CurrentDomain.AppendPrivatePath("DLL"); as first command in the assembly does not work as the DLL only has direct function calls and there is nothing like a real 'entry point'.

Another thing i found was placing the method Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args) in the DLL. But i couldn't get the compiler to recognize the Assembly keyword, although i used using namespace System::Reflection;.

So i would be thankful if someone has any idea how to get this working.

Upvotes: 1

Views: 2289

Answers (2)

Robert Giesecke
Robert Giesecke

Reputation: 4314

You may not have to use 2 assemblies (C++ and C#) if you use my Project Template for Unmanaged Exports.

I've heard of some people who wrote templates based on mine to create 3rd party plugins.
The newest one I've seen was one for Notepad++

Edit: Even if you create plain DLL with C#, you might still need to reference other assemblies. However, it seems that you can get around using C# pretty okay, so you can use AssemblyResolve from there, too.

Upvotes: 2

SLaks
SLaks

Reputation: 888223

Call Assembly::Load(path_to_managed_dll) before calling any functions that use the managed DLL.

Upvotes: 0

Related Questions