Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a C# program use a C++ dll of any version?

We're creating a dll, written in C++, providing access to some hardware. We also have a C# program that uses this dll.

We're having an issue with the versions. Indeed, when running the C# program, it absolutely wants to use the exact C++ dll version it used when compiling. I.e. if the C# program was compiled using C++ dll 1.2.3.4, then the program will refuse to run with C++ dll 1.2.3.5.

I'd like to instruct the C# program to use any C++ dll with version 1.2.anything.

Where can I configure this in the C# project?

This question has been superseded by that one, more related to COM.

like image 250
Didier Trosset Avatar asked Jul 11 '13 07:07

Didier Trosset


People also ask

Can I recharge my car AC myself?

Topping off a car ac system can usually put you back in the deep freeze. That's a job you can do yourself if your vehicle was made after 1993 and is filled with R-134a refrigerant (check the label under the hood or the specifications section of your owner's manual to be sure).

How does AC make air cool?

As the liquid refrigerant inside the evaporator coil converts to gas, heat from the indoor air is absorbed into the refrigerant, thus cooling the air as it passes over the coil. The indoor unit's blower fan then pumps the chilled air back through the home's ductwork out into the various living areas.


2 Answers

Nothing this fancy exists in C++. Using a side-by-side manifest technically permits this but you would have known about it since you would have typed the version number in the manifest of your C# program.

The far more likely explanation is that you actually created a C++/CLI assembly. Many programmers confuse C++/CLI with C++. Easy mistake since that language permits using native C++ code. But it actually gets compiled to a mixed-mode assembly, an assembly that contains both IL and native code. The normal CLR version checking occurs for such an assembly when the CLR loads it, it is only happy with an exact version match. A strong DLL Hell counter-measure.

And the normal CLR version wrangling option is available to bypass this check, a <bindingRedirect> element in your app.exe.config file. As well as controlling the assembly version number the way you do it for your C# code so this isn't necessary.

The easiest way to check if this guess is accurate is by using Project + Add Reference and select the DLL. If that doesn't draw any complaint and the assembly gets added to the References node of your C# project then you know it is a normal .NET assembly. Don't forget to take advantage of that, no pinvoke required.

like image 193
Hans Passant Avatar answered Oct 15 '22 17:10

Hans Passant


Load the dll at runtime and use reflection to call it's methods.

Assembly assembly = Assembly.LoadFrom("C:\\test.dll");

Assembly.GetTypes();

Activator.CreateInstance(type);
like image 29
Sam Leach Avatar answered Oct 15 '22 16:10

Sam Leach