Side by Side dependency between C++ and C#

1.4k views Asked by At

I'm making a browser plugin using FireBreath Framework. Most of logic is written on C# and to call it from browser I've made a C++ wrapper. Browsers call C++ Native code which calls "proxy" Managed C++ code which calls an actual logic in C# project.

So I have 3 dlls:

  • Head native C++ dll which depends on Managed C++;
  • Managed C++ which depends on C#;
  • C# dll which contains main logic.

All 3 dlls installed to a user directory (c:\Users\\AppData\Roaming\MyCompany\MyApp\1.0.0.0)

Problem is browser doesn't load C# dll. I use Side by Side manifest to declare dependencies.

I tried to make a separate manifest file to declare an assembly:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <assemblyIdentity name="MyAssembly" processorArchitecture="*" type="win32" version="1.0.0.0"/>
    <file name="FirstDependency.dll"/>
    <file name="SecondDependency.dll"/>
</assembly> 

and added link to this dependency to the head dll (Native C++):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <dependency>
    <dependentAssembly>
      <assemblyIdentity name="MyAssembly" processorArchitecture="*" type="win32" version="1.0.0.0"/>
    </dependentAssembly>
  </dependency>
</assembly>

Also I tried to declare dependency directly in the head dll (Native C++):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <file name="FirstDependency.dll"/>
  <file name="SecondDependency.dll"/>
</assembly>

Tried to link dependent dlls using #pragma directive:

 #pragma comment(linker, "\"/manifestdependency:type='win32' name='FirstDependency' version='1.0.0.0' processorArchitecture='X86' language='*'\"")
 #pragma comment(linker, "\"/manifestdependency:type='win32' name='SecondDependency' version='1.0.0.0' processorArchitecture='X86' language='*'\"")

I checked dependencies using Dependency Walker and it confirmed what dependency between Managed C++ and C# doesn't exist.

Plugin has access to the head dll (Native C++) and it loads Managed C++ as well BUT when Managed C++ calls C# dll - plugin fails, C# assembly can't be found.

If I put C# dll in the same directory with browser application (firefox.exe or chrome.exe) - it works.

Looks like Side By Side dependency doesn't work between Managed C++ & C#.

How I can load dependent dlls for my plugin?

2

There are 2 answers

0
Andrey Osipov On

Solved it.

Added ResolveEventHandler handler to C++/CLI proxy assembly which loads C# assembly from a user directory.

2
Dr. Andrew Burnett-Thompson On

I'm afraid I know nothing about Firebreath or assembly dependency manifest, but have a potential workaround for you.

Have you considered using C++/CLI to provide a wrapper between the C++ native code and C#? If you are compiling on Windows in VisualStudio a C++ lib can be made to allow mixed managed/native code just by setting the /clr switch. Then you can reference the C# assembly directly from your mixed C++/CLI dll and invoke it directly. So long as the C# assembly is in the same directory it should work.

In fact, you could go one further and define the entire assembly as mixed C++/CLI - importing all the managed elements into this DLL. If you already have extensive code in C# I wouldn't advise doing this but it is something to consider for the future.

Best regards,