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

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

问题描述

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

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

我们遇到了一个问题的版本。的确,运行C#程序时,它绝对想要使用它编译时所用的确切的C ++的dll版本。即如果使用C ++ DLL 1.2.3.4编译的C#程序,程序将拒绝使用C ++ DLL 1.2.3.5运行。

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.

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

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

我在哪里可以配置此在C#项目?

Where can I configure this in the C# project?

这个问题已取代了<一href="http://stackoverflow.com/questions/17595655/how-can-a-c-sharp-program-use-a-com-dll-of-any-version">that 之一,更多的相关COM。

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

推荐答案

这没有什么花哨的存在于C ++。使用并排侧清单技术上允许这一点,但你会知道这件事,因为你会键入的版本号在C#程序的清单。

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.

远更可能的解释是,你实际上是创造了一个C ++ / CLI装配。许多程序员混淆C ++ / CLI使用C ++。由于采用原生C ++ code语言允许容易犯的错误。但它实际上被编译成的混合模式的组件,同时包含IL和本地code的组装。正常的CLR版本检查会发生这样的组件,当CLR加载它,它只是幸福与一个确切的版本匹配。一个强大的DLL地狱应对措施。

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.

和正常的CLR版本扯皮选项可绕过这个检查,一个&LT; bindingRedirect&gt;在您app.exe.config文件元素。除了控制程序集版本号,你做你的C#code所以这是没有必要的方式。

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.

要检查,如果这个猜测是正确的,最简单的方法是使用项目+添加引用,然后选择DLL。如果没有得出任何投诉和大会被添加到您的C#项目的引用节点,那么你的知道的是一个正常的.NET程序集。不要忘了利用这一点,没有任何的PInvoke必需的。

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天全站免登陆