C#程序如何使用任何版本的C ++ DLL? [英] How can a C# program use a C++ dll of any version?

查看:120
本文介绍了C#程序如何使用任何版本的C ++ DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在创建一个用C ++编写的dll,提供对某些硬件的访问。我们也有一个使用这个DLL的C#程序。



我们在版本中出现问题。实际上,当运行C#程序时,它绝对希望使用编译时使用的确切的C ++ dll版本。即如果C#程序是使用C ++ dll 1.2.3.4编译的,那么程序将拒绝运行C ++ dll 1.2.3.5。



我想指示C#程序使用任何C ++ dll与版本1.2.anything。



我可以在C#项目中配置这个?



此问题已被取代那一个,更多与COM有关。

解决方案

C ++中没有这个想法。使用并排的清单技术上允许这一点,但您会知道它,因为您将在C#程序的清单中键入版本号。



更可能的解释是您实际上创建了一个C ++ / CLI程序集。许多程序员把C ++ / CLI与C ++混淆。因为该语言允许使用本机C ++代码,所以容易出错。但是它实际上被编译为一个混合模式的组件,一个包含IL和本机代码的程序集。当CLR加载它时,对于这样的程序集发生正常的CLR版本检查,它只对准确的版本匹配感到满意。一个强大的DLL Hell对抗。



正常的CLR版本争吵选项可用于绕过此检查,一个< bindingRedirect> 元素在您的app.exe.config文件。除了控制程序集版本号,您为C#代码执行此操作的方式,因此这不是必需的。



检查这个猜测是否准确的最简单的方法是通过使用Project + Add Reference并选择该DLL。如果没有提出任何投诉,并将程序集添加到C#项目的References节点,那么您知道它是一个普通的.NET程序集。不要忘记利用这一点,不需要pinvoke。


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.

解决方案

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.

这篇关于C#程序如何使用任何版本的C ++ DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆