简体   繁体   English

我可以在C#项目中使用C ++编写的DLL吗?

[英]Can I use DLL written in C++ in my C# project?

The problem is - I want to write a template class in C++, put it in a DLL, and then call it from my C# project. 问题是 - 我想用C ++编写一个模板类,把它放在一个DLL中,然后从我的C#项目中调用它。 Is it possible? 可能吗? Can you please provide any references or articles on about how to do it? 能否请您提供有关如何做的任何参考或文章?

EDIT 编辑
I want DLL to be written in unmanaged C++ 我希望DLL在非托管 C ++中编写

Template class could not be exported. 无法导出模板类。 It does not exist until someone instantiate it. 直到有人实例化它才会存在。 You should explicitly instantiate it and then export it as usual class. 您应该显式实例化它,然后将其导出为通常的类。

I think this question may help you out: 我认为这个问题可以帮到你:

Use C++ CLI template class in C# 在C#中使用C ++ CLI模板类

You create it just as with any other DLLs - the main idea behind DLLs is that it can be created in any programming language, and be used with every other. 你可以像创建任何其他DLL一样创建它 - DLL背后的主要思想是它可以用任何编程语言创建,并且可以与其他所有DLL一起使用。 Just remember that C++ is unmanaged, so it has to be treated carefully. 请记住,C ++是不受管理的,因此必须谨慎对待。 Look for instance here (MSDN forum). 在这里寻找(MSDN论坛)。

One more link. 还有一个链接。

In general, use DllImport decorator to import functions from DLL file you've created in C++. 通常,使用DllImport装饰器从您在C ++中创建的DLL文件中导入函数。 Example from MSDN: 来自MSDN的示例:

using System.Runtime.InteropServices; // DllImport
public class Win32 {
  [DllImport("User32.Dll")]
  public static extern void SetWindowText(int h, String s);
}

By using C++/CLI you can expose your C++ classes as .NET classes where they use compatible features. 通过使用C ++ / CLI,您可以将C ++类公开为.NET类,并使用兼容的功能。 You won't, however, be able to expose your template definition, but may be able to use a concrete class that specializes that template. 但是,您不能公开模板定义,但可以使用专门化该模板的具体类。

When you build a C++/CLI class you can reference it just like any other .NET assembly. 构建C ++ / CLI类时,您可以像任何其他.NET程序集一样引用它。

As far as I recall there's a bit of a problem. 据我所知,有一点问题。 It is possible to Have C# use a C++ Dll (managed and unmanaged) It is possible to have unmanaged C++ use a C# Dll (you need to do this via COM and an interface). 有可能让C#使用C ++ Dll(托管和非托管)可以让非托管C ++使用C#Dll(您需要通过COM和接口执行此操作)。

I'll see if I can find more detailed information 我会看看能否找到更详细的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM