简体   繁体   English

如何从C#代码在Visual Studio中创建本机DLL?

[英]How to create native DLL in Visual Studio from C# code?

I have the source code of a C# program. 我有一个C#程序的源代码。 I want to create a DLL out of it which I want to use in C++. 我想创建一个我想在C ++中使用的DLL。

Is it possible to create a native DLL in Visual Studio 2008 which can be used in C++? 是否可以在Visual Studio 2008中创建可在C ++中使用的本机DLL?

native <-> .Net interop is one of my pet disciplines, which is why I needed this as straightforward and reliable as possible. 本地< - > .Net互操作是我的宠物学科之一,这就是为什么我需要尽可能简单和可靠。

The result was that I made me an MSBuild task which I just need to drag into a project to allow me to export static methods from pretty much any .Net language. 结果是我做了一个MSBuild任务 ,我只需要拖入一个项目,允许我从几乎任何.Net语言中导出静态方法。 And since the whole marshalling infrastructure works for exports as well, you can do pretty much anything with it that you want (like passing native/managed objects as IUnknown). 而且由于整个编组基础设施也适用于导出,因此您可以使用所需的任何内容(例如将本机/托管对象作为IUnknown传递)。

The resulting assembly will look to the consuming process like a real DLL, which means you can't have it to scale up to 64bit automatically anymore. 生成的程序集将像一个真正的DLL一样看待消费过程,这意味着你不能再让它自动扩展到64位。

However, since you do have native bits in your application, you already have this issue anyways. 但是,由于您的应用程序中确实存在本机位,因此您仍然存在此问题。 ;-) ;-)

When you do not specifiy the CPU target in your C# project, my task will emit a warning that it created a folder for all targets (x86,x64 and Itanium), and there you'll have *.dll and *.pdb for each platform. 如果没有在C#项目中指定CPU目标,我的任务将发出警告,表明它为所有目标(x86,x64和Itanium)创建了一个文件夹,并且每个目标都有* .dll和* .pdb平台。

If you want the program to be native, and not managed, you'll need to port it to C++, instead of using C#. 如果您希望程序是本机程序而不是托管程序,则需要将其移植到C ++,而不是使用C#。

That being said, you can compile it in C# into a library, and use it from C++ by using C++/CLI. 话虽这么说,你可以用C#将它编译成一个库,然后使用C ++ / CLI从C ++中使用它。 This just requires that you compile the files that use the C# library with the /clr flag. 这只需要使用/ clr标志编译使用C#库的文件。 This provides C++ access to the .NET framework, and lets you use libraries made in C# directly from C++. 这提供了对.NET框架的C ++访问,并允许您直接从C ++使用C#中创建的库。

Alternatively, you can use .NET's COM interop to expose the C# class(es) as COM objects, and then use those from native C++. 或者,您可以使用.NET的COM互操作将C#类公开为COM对象,然后使用本机C ++中的那些。

It is possible in Visual Studio 2008, but you're not going to be able to write it using C#. 它可以在Visual Studio 2008中使用,但您无法使用C#编写它。

To create a native DLL, you'll have to write your code using one of the unmanaged C++ project types. 要创建本机DLL,您必须使用其中一种非托管C ++项目类型编写代码。

You can expose the DLL to COM. 您可以将DLL公开给COM。 Have a look here for some examples . 看看这里的一些例子

yes you can. 是的你可以。
you need to create second project. 你需要创建第二个项目。
the project must be unmanaged (like "visual c++"->class library). 项目必须是非托管的(如“visual c ++” - >类库)。
the name of this procedure is "calling from unmanaged code to managed code" . 此过程的名称是“从非托管代码调用托管代码”

good to read unmanaged to managed (codeproject) 很好读取非托管到托管(codeproject)
you must be aware , that any computer that using your dll must have preinstalled DotNet and Visual C++ Redistributable Package 您必须知道 ,任何使用您的DLL的计算机必须预先安装DotNetVisual C ++ Redistributable Package

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

相关问题 从本地Visual C ++代码调用ac#.dll - Calling a c# .dll from native visual c++ code 如何从 Visual Studio.NET 或 Visual Studio 2005 中的本机 Visual C++ 代码调用托管 DLL - How to call a managed DLL from native Visual C++ code in Visual Studio.NET or in Visual Studio 2005 如何在Visual Studio 2008中创建.dll以在C#应用程序中使用? - How to create a .dll in Visual Studio 2008 for use in a C# App? 如何在 Visual Studio Code 中将 DLL 添加到 C# 程序中? - How to add a DLL to a C# program in Visual Studio Code? 如何从本机C(++)DLL调用.NET(C#)代码? - How to call .NET (C#) code from a native C(++) DLL? 如何在Visual Studio Code中以.csproj扩展名创建C#项目? - How to create C# project with .csproj extension in Visual Studio Code? 如何使用 Visual Studio Code 创建新的可执行 C# 项目 - How to create a new executable C# project with Visual Studio Code 如何使用 Visual Studio Code 创建 C# 项目? - How to create a C# project using Visual Studio Code? 如何在C#Visual Studio中使用子字符串从组合框创建代码? - How to create a code from comboboxes using substring in c# visual studio? 如何在Visual Studio C#2010 Express Edition中创建DLL文件? - How to Create a DLL file in Visual Studio C# 2010 Express edition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM