是否可以像 VS C++ 中那样从 C# DLL 导出函数?
在 VS C/C++ 中,您可以使用 extern "C" __declspec(dllexport) -函数声明- 。
如何在 C# dll 中完成此操作?是否有与上述代码等效的 C# 代码?
编辑:更多信息
我正在尝试为 Notepad++ 创建一个加载项,并且我想使用 C#,但到目前为止我看到的常见方法是使用旧版 C++ 代码以及上述导出调用Notepad++ 期望导入和调用的一些函数。有一个使用 C# 的示例应用程序,但这仍然需要一个加载程序 DLL,我从下面的评论/答案中认为这是 C# 的唯一方法。
In VS C/C++ you could use extern "C" __declspec(dllexport) -function declaration-
.
How do I accomplish this in a C# dll? Is there C# code equivalent to the above code?
Edit: More info
I am trying to create an add in for Notepad++ and I want to use C#, but the common way I've seen so far is to use legacy C++ code with the above call to export a few of the functions that Notepad++ expects to import and call. There is an example app using C#, but this still requires a loader DLL, which I assume from the comments/answers below is the only way for C#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
非托管导出 =>
https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagementexports
DLL导出 => https://github.com/3F/DllExport
Unmanaged Exports =>
https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports
DLLExport => https://github.com/3F/DllExport
我以前见过人们这样做,但它需要 ildasm,添加 MSIL
.export
指令,然后重新组装。 名为dll_tool
的程序可以执行这些步骤为你。如果要构建具有本机导出和托管导出的混合模式 DLL,则应该使用专为此目的而设计的 C++/CLI。
I've seen people do this before, but it required ildasm, adding the MSIL
.export
directive, and then reassembling. A program nameddll_tool
can do these steps for you.If you want to build a mixed-mode DLL with both native and managed exports, you should be using C++/CLI, which is specially designed for this purpose.
是的,可以像 C++ 一样从 C# dll 导出函数!您需要插件 非托管导出 (DllExport for .Net) 的帮助来实现此目的过程,或使用类似的方法,例如 将 C#/VB.NET 中的函数导出为本机代码。
请参阅导出代码C# DLL 到 Metatrader Build 600+ 的工作示例,使用 Robert Giesecke 的用于非托管导出的 C# 项目模板将 C# DLL 导出到与 C++ 非常相似的旧应用程序 (Metatrader)。
此外,您可能会发现 本机和 .NET 互操作性 很有趣,尽管它主要面向从以下位置访问本机代码:在 .NET 中而不是相反。
Yes, it is possible to export functions from a C# dll in much the same way that C++ does it! You need a little help from an add-in Unmanaged Exports (DllExport for .Net) that facilitates this process, or from a similar method such as Exporting functions in C#/VB.NET to native code.
Please see Code to Export C# DLL to Metatrader Build 600+ for a working example using Robert Giesecke's C# Project Template for Unmanaged Exports to export a C# dll to a legacy application (Metatrader) that has a great deal of similarity to C++.
Additionally, you might find Native and .NET Interopability interesting though it is mostly geared toward accessing native code from within .NET rather than the other way around.
现在有一个名为 DNNE 的开源包,又名“Native Exports for .NET”,可在此处使用: https://github.com/AaronRobinsonMSFT/DNNE
就目前而言,它需要安装 .NET6,但它也支持导出 .NET Framework 4.8 .dll 函数。
There's now an open source package called DNNE aka "Native Exports for .NET" available here: https://github.com/AaronRobinsonMSFT/DNNE
As for today, it' requires .NET6 installed, but it does support exporting .NET Framework 4.8 .dll functions as well.
不,您不能像在 C 和 C++ 中那样做到这一点。
但是您可以创建 COM API 来实现然后可以在 C 和 C++ 代码中使用的功能。
请参阅这些文章
No, you cannot do that in the same sense as you do in C and C++.
But you can create COM API to achieve that which then you can use in C and C++ code.
See these articles