如何在我的C ++项目中添加C#dll? [英] How do I add a C# dll into my C++ project?

查看:120
本文介绍了如何在我的C ++项目中添加C#dll?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个c ++库,我需要从C#库(便携式类库)调用一个函数。



任何人都可以帮我做这个?

I am trying to write a c++ library and i need to call a function from a C# library(portable class library).

Can any body help me doing this?

推荐答案

请看我对这个问题的评论;目前尚不清楚C ++究竟是什么意思。



但是,这里有一个想法:无论如何你使用C#,你也应该使用C ++ / CLI 即使所有或大部分C ++代码都是本机的。您可以创建混合模式 C ++项目(托管+非托管)并使用托管部分,尤其是使用其他.NET程序集。您的混合模式项目将创建一个可执行模型,从CLI的角度来看,它可以用作常规CLI程序集,例如,由其他程序集引用。但是,就你的问题而言,这可能不是你的目标。它只是允许您使用C#代码。即使您需要在项目的非托管部分中使用它,也可以使用非托管代码中的C#程序集包装托管的ref类,并使用其他非托管代码。



本质上很难以非托管代码以任何其他方式使用CLI程序集。(相反的问题,在CLI中使用非托管代码)很容易。)通常的方法是使用COM组件,但是为什么要使用.NET或非托管代码中不需要的过时技术来污染安装和代码以及系统注册表?



实际上,有一种不同的方式使用IL实际上允许导出到非托管的这一事实,但这也更难;请看我过去的答案:

在MFC中加载C#DLL [ ^ ],

如何在Visual Basic 6.0中使用Visual Basic 2008中创建的dll [ ^ ],

调用托管DLL编写在C#中来自Unmanged Code VC ++ [ ^ ],

.Net中的API:托管代码或非托管代码 [ ^ ]。
因此,使用C ++ / CLI,可能在混合模式项目中,只要涉及C ++,就是解决此类问题的最佳方法。



另见:

http://en.wikipedia.org/wiki/ C%2B%2B / CLI [ ^ ],

http://www.ecma- international.org/publications/standards/Ecma-372.htm [ ^ ],

http:/ /www.gotw.ca/publications/C++CLIRationale.pdf [ ^ ]。



-SA
Please see my comment to the question; it's not clear what exactly do you mean by C++.

However, here is the idea: once you use C# anyway, you should also use C++/CLI even if all or most of the C++ code is native. You can create a mixed-mode C++ project (managed + unmanaged) and use managed part for, in particular, using other .NET assemblies. Your mixed-mode project will create an executable model, which, from the CLI point of view, can be used as a regular CLI assembly, for example, referenced by other assemblies. But, as your question is concerned, this might be not your goal. It simply allows you to use C# code. Even if you need to use it in unmanaged part of the project, you can wrap your managed "ref" class using C# assembly in unmanaged code and use in other unmanaged code.

(It is inherently difficult to use a CLI assembly in unmanaged code in any other way. (The opposite problem, using unmanaged code in CLI is easy enough.) The usual approach is using COM component, but why contaminate the installation and your code, as well as system registry, with obsolete technology which you don't need in .NET or unmanaged code?

Actually, there is a different way using the fact that IL actually allows to be exported to unmanaged, but this is also more difficult; please see my past answers:
loading C# DLL in MFC[^],
How can I use a dll created in Visual Basic 2008 in Visual Basic 6.0[^],
Call Managed DLL written in C# from Unmanged Code VC++[^],
API's in .Net: Managed or UnManaged Code[^].
So, using C++/CLI, possibly in a mixed-mode project is the best way to solve such problem as soon as C++ is involved.

See also:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^].

—SA


其他答案是对的,请使用C ++ / CLI r DLL,它将能够毫无问题地使用C#DLL。



我要添加的另一件事是,如果你需要你的C ++ DLL完全native,然后你可以使用C#DLL周围的包装器DLL来使它工作。包装器DLL将是C ++ / CLI并将其入口点导出到非托管(本机)部分。对于我们有.EXE但没有源的项目,我不得不这样做。 .EXE可以通过专有脚本语言运行DLL,因此我能够使本机.EXE调用C#DLL。
The other answers are right, use C++/CLI for your DLL and it will be able to use the C# DLL without a problem.

The other thing I would add, is that if you need your C++ DLL to be completely native, then you can use a wrapper DLL around the C# DLL to make it work. The wrapper DLL would be C++/CLI and export its entry points in the unmanaged (native) part. I have had to do this for a project where we had a .EXE but no source. The .EXE could run DLLs through a proprietary scripting language, so I was able to make the native .EXE call C# DLLs.


这篇关于如何在我的C ++项目中添加C#dll?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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