如何在VC ++中使用C#DLL? [英] How to use c# Dll in vc++?
问题描述
我创建使用命令行(CSC)的DLL。假设DLL包含添加(INT,int)方法。现在,我想用在VC中添加功能++ ??
I have created a dll using command line (csc). Suppose that dll contains Add(int,int) method. Now I want to use that add function in vc++??
如何做到这一点?
您的帮助将不胜鸭preciated。
Your Help will be Greatly Appreciated.
下面是我在做什么。
vcproj;这个项目我已经右键点击,链接部分添加额外的dependencies.added其中DLL所在的路径。
vcproj; for this project i have right click and linker section add additional dependencies.added the path where dll resides.
的main.cpp
using namespace Test
void main()
{
demo d;
d.add(5,5);
}
错误的命名空间试验不存在。的
error namespace Test Does not exist.
如何解决此问题?
我需要使用DLL。在非托管code
I need to use that dll as in unmanaged code
推荐答案
从MSDN论坛<一href="http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/5345a27a-a614-4a74-9f6d-ea7a999ddf83">Calling C#从非托管C ++ :
你想做的事是什么 编译只有在你想要的文件 加载C#code / CLR,然后调用 外出使用C ++ / CLI。其余的 应用程序将保持原生而CPP 您编译文件/ CLR将被混合 本机和CLR。你应该能够 只是打电话到C#库使用 C ++ / CLI语法。
What you want to do would be to compile only the files where you want to load the C# code /clr and then call away using C++/CLI. The rest of your app will remain native while those cpp files you compile /clr will be mixed native and CLR. You should be able to just call into your C# library using C++/CLI syntax.
在你扔/ CLR上的cl.exe 为一个文件的命令行,内 文件中,你可以调用任何管理code 你想要的,无论 这是写在VB.net,C#或 C ++ / CLI。这是迄今为止最容易 这样做你想做的事(调用C#是什么 从你的本地C ++应用程序,code) 虽然它确实有它的警告的和 限制。总的来说,虽然它 只是工程。此外,它的速度比 P /调用。
Once you throw /clr on the cl.exe command line for a file, within that file, you can call any managed code that you want, regardless of whether it was written in VB.net, C# or C++/CLI. This is by far the easiest way to do what you want to do (call C# code from your native C++ app), although it does have its caveat's and limitations. By and large though "It Just Works". Also, it's faster than p/invokes.
这篇关于如何在VC ++中使用C#DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!