如何在vc++中使用c#Dll?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/980808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use c# Dll in vc++?
提问by Cute
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++??
我使用命令行(csc)创建了一个 dll。假设 dll 包含 Add(int,int) 方法。现在我想在 vc++ 中使用那个 add 函数??
How to do this?
这该怎么做?
Your Help will be Greatly Appreciated.
对你的帮助表示感谢。
Here is what I am doing.
这就是我正在做的事情。
vcproj; for this project i have right click and linker section add additional dependencies.added the path where dll resides.
vcproj; 对于这个项目,我有右键单击和链接器部分添加其他依赖项。添加了 dll 所在的路径。
main.cpp
主程序
using namespace Test
void main()
{
demo d;
d.add(5,5);
}
error namespace Test Does not exist.
错误命名空间测试不存在。
How to resolve this?
如何解决这个问题?
I need to use that dll as in unmanaged code
我需要在非托管代码中使用该 dll
回答by Matt Brindley
Your C# assembly needs the ComVisible attribute (among a few other things).
您的 C# 程序集需要 ComVisible 属性(以及其他一些内容)。
[assembly: ComVisible(true)]
There's a guide to doing this here.
这里有一个指南来做这件事。
回答by Dana Holt
From MSDN forums Calling C# from unmanaged C++:
从 MSDN 论坛调用 C# from unmanaged C++:
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.
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.
您想要做的是仅编译要加载 C# 代码 /clr 的文件,然后使用 C++/CLI 调用。你的应用程序的其余部分将保持原生,而你编译的那些 cpp 文件 /clr 将混合原生和 CLR。您应该能够使用 C++/CLI 语法调用 C# 库。
在文件的 cl.exe 命令行上抛出 /clr 后,在该文件中,您可以调用任何所需的托管代码,无论它是用 VB.net、C# 还是 C++/CLI 编写的。这是迄今为止执行您想做的事情的最简单方法(从您的本机 C++ 应用程序调用 C# 代码),尽管它确实有其警告和限制。总的来说,虽然“它只是有效”。此外,它比 p/invokes 更快。
回答by JaredPar
This highly depends on what type of C++ application you are building. If you are building a C++/CLI assembly then you need to make sure the project has a reference to the C# DLL. Once that is done you should be able to type the code as written assumping you have a definition of Demo like so.
这在很大程度上取决于您正在构建的 C++ 应用程序的类型。如果您正在构建 C++/CLI 程序集,则需要确保项目具有对 C# DLL 的引用。完成后,您应该能够输入所编写的代码,假设您有这样的 Demo 定义。
namespace Test {
public class demo {
public void add(int left, int right) {
...
}
}
}
If you are building a normal, non-managed, C++ project then you must use COM interop to access the assembly. This involves making the demo type in your C# project COMVisible and registering it for COM interop. After which you can access it like any other COM component.
如果您正在构建一个普通的、非托管的 C++ 项目,那么您必须使用 COM 互操作来访问程序集。这涉及使 C# 项目中的演示类型为 COMVisible 并将其注册为 COM 互操作。之后,您可以像访问任何其他 COM 组件一样访问它。