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

We create a dll written in C ++, providing access to some equipment. We also have a C # program that uses this DLL.

We have a version problem. In fact, when running a program in C #, he absolutely wants to use the exact version of the dll for C ++ that he used when compiling. That is, if a 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 would like to instruct the C # program to use any C ++ dll with version 1.2.anything.

Where can I customize this in a C # project?

This question has been replaced by the fact that one is more related to COM.

+6
source share
3 answers

Nothing like this exists in C ++. Using side by side, the manifest technically resolves this, but you would know that, since you typed the version number in the manifest of your C # program.

The more likely explanation is that you actually created the C ++ / CLI assembly. Many programmers confuse C ++ / CLI with C ++. A slight mistake, because this language allows you to use your own C ++ code. But it actually compiles into a mixed-mode assembly, an assembly containing both IL and native code. A regular check of the CLR version is performed for such an assembly, when the CLR loads it, it is only happy with the exact version. Strong DLL Hell Measure.

And the usual CLR version interrupt option is available to circumvent this check, the <bindingRedirect> element in the app.exe.config file. As well as controlling the version number of the assembly, how do you do this for your C # code, so this is optional.

The easiest way to verify this assumption is to use Project + Add Reference and select a DLL. If this does not cause any complaint, and the assembly is added to the node link of your C # project, then you know that this is a normal .NET assembly. Remember to take advantage of this, no pinworm is required.

+3
source

Load the dll at runtime and use reflection to invoke its methods.

 Assembly assembly = Assembly.LoadFrom("C:\\test.dll"); Assembly.GetTypes(); Activator.CreateInstance(type); 
+1
source

I do not think that you can configure your program to use version 1.2. * and no one else. If you do not write the code for this yourself. Another possibility is not to change the tag of the C ++ dll version, but it seems you do not want this.

A solution that excludes version dependency will use dllimport. You can load any DLL written in C ++. It is version dependent. See an example from msdn and the link at the end:

 using System; using System.Runtime.InteropServices; class Example { // Use DllImport to import the Win32 MessageBox function. [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type); static void Main() { // Call the MessageBox function using platform invoke. MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0); } 

}

Description on MSDN

+1
source

Source: https://habr.com/ru/post/949195/


All Articles