如何在 Visual Studio 2008 中创建 .dll 以在 C# 应用程序中使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/877888/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:08:21  来源:igfitidea点击:

How to create a .dll in Visual Studio 2008 for use in a C# App?

c#c++visual-studio-2008dll

提问by Jon Cage

I have a C++ class I'd like to access from a C# application. I'll need to access the constructor and a single member function. Currently the app accepts data in the form of stl::vectors but I can do some conversion if that's not likely to work?

我有一个 C++ 类,我想从 C# 应用程序访问。我需要访问构造函数和单个成员函数。目前该应用程序接受stl::vectors形式的数据,但如果这不太可能奏效,我可以做一些转换吗?

I've found a few articles online which describe how to call C++ DLLs and some others which describe how to make .dll projects for other purposes. I'm struggling to find a guide to creating them in Visual Studio 2008 for use in a C# app though (there seem to be a few for VS 6.0 but the majority of the options they specify don't seem to appear in the 2008 version).

我在网上找到了几篇文章,它们描述了如何调用 C++ DLL,还有一些文章描述了如何为其他目的制作 .dll 项目。我正在努力寻找在 Visual Studio 2008 中创建它们以在 C# 应用程序中使用的指南(VS 6.0 似乎有一些,但他们指定的大多数选项似乎没有出现在 2008 版本中)。

If anyone has a step-by-step guide or a fairly basic example to get going from, I'd be very grateful.

如果有人有分步指南或相当基本的示例,我将不胜感激。

采纳答案by Bojan Resnik

The easiest way to interoperate between C++ and C# is by using managed C++, or C++/CLI as it is called. In VisualStudio, create a new C++ project of type "CLR Class Library". There is some new syntax for the parts that you want to make available to C#, but you can use regular C++ as usual.

在 C++ 和 C# 之间进行互操作的最简单方法是使用托管 C++,或称为 C++/CLI。在 VisualStudio 中,创建一个类型为“CLR 类库”的新 C++ 项目。对于要提供给 C# 的部分,有一些新语法,但您可以像往常一样使用常规 C++。

In this example, I'm using std::vector<int>just to show that you can use standard types - however, in an actual application, I'd prefer to use the .NET types where possible (in this case a System::Collections::Generic::List<int>).

在这个例子中,我std::vector<int>只是为了表明您可以使用标准类型 - 但是,在实际应用程序中,我更喜欢在可能的情况下使用 .NET 类型(在本例中为 a System::Collections::Generic::List<int>)。

#pragma unmanaged
#include <vector>
#pragma managed

public ref class CppClass
{
public:
   CppClass() : vectorOfInts_(new std::vector<int>)
   {}

   // This is a finalizer, run when GC collects the managed object
   !CppClass()
   { delete vectorOfInts_; }

   void Add(int n)
   { vectorOfInts_->push_back(n); }

private:
    std::vector<int>* vectorOfInts_;
};

EDIT:Changed the class to hold the vector by pointer instead of by value.

编辑:更改了类以通过指针而不是值来保存向量。

回答by Number8

Do you have an existing C++ exe that you want to re-build as a dll? Or is all you have the C++ class?
If all you have is the C++ class, you might think about building a .Net dll. While you couldn't pass stl types from C# to C++, I am pretty sure you can pass managed .Net types.
If you must use the stl types in the class, you could convert the data in the C++ class, or the calling app.

您是否有要重新构建为 dll 的现有 C++ exe?或者你只有 C++ 类?
如果您只有 C++ 类,您可能会考虑构建一个 .Net dll。虽然您无法将 stl 类型从 C# 传递到 C++,但我很确定您可以传递托管的 .Net 类型。
如果必须在类中使用 stl 类型,则可以在 C++ 类或调用应用程序中转换数据。

回答by ChrisBD

Use of unmanaged C++ Dlls can be very tedious in C#.

在 C# 中使用非托管 C++ Dll 可能非常乏味。

An "easier" route is to place it in a COM wrapper.

“更简单”的方法是将其放置在 COM 包装器中。

回答by Anzurio

If such DLL is unmanaged. You will have to use P/invoke. A p/invoke function definition looks like this:

如果此类 DLL 是非托管的。您将不得不使用 P/invoke。p/invoke 函数定义如下所示:

[DllImport("Library_Name.dll", EntryPoint = "function")]
public static extern void function();

If, on the other hand, is a managed (C++/CLI) DLL (or assembly). You can access it by adding a reference to it on your .NET project.

另一方面,如果是托管 (C++/CLI) DLL(或程序集)。您可以通过在 .NET 项目中添加对它的引用来访问它。

EDIT:I think I didn't answer your question at all. But to create a managed C++ DLL to be accessed from .NET, create a new project an choose: Visual C++/CLR/Class Library. And then, just add its output DLL to your C# project.

编辑:我想我根本没有回答你的问题。但是要创建可从 .NET 访问的托管 C++ DLL,请创建一个新项目并选择:Visual C++/CLR/Class Library。然后,只需将其输出 DLL 添加到您的 C# 项目中。

That'd do it. :)

就可以了。:)

回答by Jon Cage

I've found a tutorialwhich works. A quick note though, you have to be careful to copy the .dll into the same directory as the executable. I messed around with trying to tell the executable where the DLL was but gave up in the end.

我找到了一个有效的教程。但是,请注意,您必须小心将 .dll 复制到与可执行文件相同的目录中。我试图告诉可执行文件 DLL 的位置,但最终放弃了。