使用stdcall创建可以在Delphi应用程序中导入的C#DLL - 可能?

时间:2022-06-27 16:11:32

I have a program that I need to create a DLL for, hopefully in C#. The program is written in Delphi and I have an interface file to code to. The interface uses the stdcall calling convention.

我有一个程序,我需要创建一个DLL,希望在C#中。该程序是用Delphi编写的,我有一个代码的接口文件。该接口使用stdcall调用约定。

Is it possible to create a C# DLL that conforms to the interface and can be used in the Delphi application?

是否可以创建符合接口的C#DLL并可以在Delphi应用程序中使用?

Is there some sample code that demonstrates how to code the C# DLL to a stdcall interface method?

是否有一些示例代码演示如何将C#DLL编码为stdcall接口方法?

9 个解决方案

#1


This is not possible in pure C#, but this is an article that shows how to add an unmanaged export table to your C# library, which can then be used in any other language. Note that the hordes of references to Blitz should not put you off - they relate to the author's own context and have nothing to do with the basic concept and how it works.

这在纯C#中是不可能的,但是这篇文章展示了如何将非托管导出表添加到C#库,然后可以使用任何其他语言。请注意,大量的闪电战参考不应该让你失望 - 它们与作者自己的背景有关,与基本概念及其运作方式无关。

There is also a section in Brian Long's one conference paper. In a twist that you could see as somewhat ironic, Delphi.Net actually supported unmanaged exports directly despite C# not doing so. I have no idea if this is true of Delphi Prism as well.

Brian Long的一篇会议论文中还有一节。尽管C#没有这样做,但Delphi.Net实际上支持非托管导出,这让你觉得有点讽刺。我不知道Delphi Prism是否也是如此。

#2


I have been down this road before. The solution I picked was to create a NEW C# assembly (I later ported this to Prism) which exposed via com interop the functionality I needed to reach. I was finding by black boxing the API calls into something simpler, I was able to reduce the number of classes I had to deal with across the interop barrier.

我之前一直在这条路上。我选择的解决方案是创建一个新的C#程序集(我后来将其移植到Prism),它通过com interop暴露了我需要达到的功能。我发现通过黑色拳击API调用更简单的东西,我能够减少我必须处理跨越互操作屏障的类的数量。

I did look at Hydra, but it was overkill for what I was trying to do... which was access a 3rd party SDK which was presented in .net assemblies to process data. If you are looking at embedding the functionality (gui objects, ect) in your application then you should give Hydra some consideration.

我确实看过Hydra,但是对于我试图做的事情来说太过分了......这是访问第三方SDK,它在.net程序集中呈现以处理数据。如果您正在考虑在您的应用程序中嵌入功能(gui对象,等等),那么您应该考虑Hydra。

I did use Managed.VCL for a very early version of the system, but later abandoned it for the Prism/C# com interop approach which was simpler to deploy, and more stable.

我确实使用Managed.VCL作为系统的早期版本,但后来放弃了Prism / C#com互操作方法,它更易于部署,更稳定。

#3


Have a look at Hydra

看看九头蛇

#4


Out of curiosity, why are you hoping to write a .dll that's intended to be used from a native application in C#?

出于好奇,你为什么希望编写一个打算在C#中使用本机应用程序的.dll?

Managed C++, Delphi for .Net and now Delphi Prism all support this out of the box using unmanaged exports. By design, C# and VB.net don't. Not sure why. But as Cobus mentioned, you can kind of hack around this. Do so at your own risk.

托管C ++,Delphi for .Net和Delphi Prism都支持使用非托管导出的开箱即用。按照设计,C#和VB.net不会。不知道为什么。但正如Cobus所说,你可以解决这个问题。这样做自担风险。

In addition to Hydra from RemObjects, AToZed is introducing CrossTalk.

除了RemObjects的Hydra之外,AToZed还推出了CrossTalk。

#5


You need to make the assembly (=C# DLL) accessible to COM, which is called Interop.

您需要使COM可以访问程序集(= C#DLL),这称为Interop。

See MSDN articles Assembly to Type Library Conversion and Packaging an Assembly for COM which describe the technical background and the utilities to perform the required operations.

请参阅MSDN文章装配到类型库转换和打包COM装配,其中描述了执行所需操作的技术背景和实用程序。

#6


I found a post from Robert Giesecke on the Delphi Prism newsgroups. In it, he announces a project you can add to a solution that lets you export arbitrary functions from a .Net DLL simply by adding the DllExport attribute to them. It supports marshaling just like DllImport. He demonstrates it with a Prism project, but I imagine it would work on C# classes as well. The post was made in March, so I'm not sure whether the attachment will still be available. The May release of Prism obviates such a tool since it supports unmanaged exports by itself.

我在Delphi Prism新闻组中找到了Robert Giesecke的帖子。在其中,他宣布了一个项目,您可以添加到一个解决方案中,只需向其添加DllExport属性,即可从.Net DLL中导出任意函数。它像DllImport一样支持编组。他用Prism项目演示了它,但我想它也适用于C#类。该帖子是在三月制作的,所以我不确定附件是否仍然可用。 5月发布的Prism消除了这种工具,因为它本身支持非托管出口。

#7


I'm assuming here that the Delphi app is not a .NET based app and therefore you need to host the .NET runtime in a Win32 process.

我假设Delphi应用程序不是基于.NET的应用程序,因此您需要在Win32进程中托管.NET运行时。

CorBindToRuntimeEx is a function inside MSCorEE.dll, which holds the .NET runtime. With it you can host the runtime and then create objects inside it and interact with them.

CorBindToRuntimeEx是MSCorEE.dll中的一个函数,它保存.NET运行时。有了它,您可以托管运行时,然后在其中创建对象并与它们进行交互。

#8


I'm quite sure this can not be done directly. You'll have to either write a layer in C++/CLI or expose the C# code as an ActiveX interface. But that second option may not meet your interface.

我很确定这不能直接完成。您必须在C ++ / CLI中编写图层或将C#代码公开为ActiveX接口。但是第二个选项可能不符合您的界面。

#9


This is not directly possible. C# is managed code. This means that it requires a very specific runtime environment in order to function, an environment which Delphi could not directly provide to it. It is not like C where you simply find the address and calling convention of the function and call it.

这不是直接可能的。 C#是托管代码。这意味着它需要一个非常特定的运行时环境才能运行,这是Delphi无法直接提供给它的环境。它不像C,你只需要找到函数的地址和调用约定并调用它。

However, it is possible to host the Common Language Runtime inside of a Delphi application (or any other Windows application). I have no idea how to do this. I just know that it's possible. (It's quite likely that's what this 'Hydra' that Steve mentioned will do.)

但是,可以在Delphi应用程序(或任何其他Windows应用程序)中托管公共语言运行时。我不知道该怎么做。我只知道这是可能的。 (这很可能是史蒂夫提到的'Hydra'所做的。)

#1


This is not possible in pure C#, but this is an article that shows how to add an unmanaged export table to your C# library, which can then be used in any other language. Note that the hordes of references to Blitz should not put you off - they relate to the author's own context and have nothing to do with the basic concept and how it works.

这在纯C#中是不可能的,但是这篇文章展示了如何将非托管导出表添加到C#库,然后可以使用任何其他语言。请注意,大量的闪电战参考不应该让你失望 - 它们与作者自己的背景有关,与基本概念及其运作方式无关。

There is also a section in Brian Long's one conference paper. In a twist that you could see as somewhat ironic, Delphi.Net actually supported unmanaged exports directly despite C# not doing so. I have no idea if this is true of Delphi Prism as well.

Brian Long的一篇会议论文中还有一节。尽管C#没有这样做,但Delphi.Net实际上支持非托管导出,这让你觉得有点讽刺。我不知道Delphi Prism是否也是如此。

#2


I have been down this road before. The solution I picked was to create a NEW C# assembly (I later ported this to Prism) which exposed via com interop the functionality I needed to reach. I was finding by black boxing the API calls into something simpler, I was able to reduce the number of classes I had to deal with across the interop barrier.

我之前一直在这条路上。我选择的解决方案是创建一个新的C#程序集(我后来将其移植到Prism),它通过com interop暴露了我需要达到的功能。我发现通过黑色拳击API调用更简单的东西,我能够减少我必须处理跨越互操作屏障的类的数量。

I did look at Hydra, but it was overkill for what I was trying to do... which was access a 3rd party SDK which was presented in .net assemblies to process data. If you are looking at embedding the functionality (gui objects, ect) in your application then you should give Hydra some consideration.

我确实看过Hydra,但是对于我试图做的事情来说太过分了......这是访问第三方SDK,它在.net程序集中呈现以处理数据。如果您正在考虑在您的应用程序中嵌入功能(gui对象,等等),那么您应该考虑Hydra。

I did use Managed.VCL for a very early version of the system, but later abandoned it for the Prism/C# com interop approach which was simpler to deploy, and more stable.

我确实使用Managed.VCL作为系统的早期版本,但后来放弃了Prism / C#com互操作方法,它更易于部署,更稳定。

#3


Have a look at Hydra

看看九头蛇

#4


Out of curiosity, why are you hoping to write a .dll that's intended to be used from a native application in C#?

出于好奇,你为什么希望编写一个打算在C#中使用本机应用程序的.dll?

Managed C++, Delphi for .Net and now Delphi Prism all support this out of the box using unmanaged exports. By design, C# and VB.net don't. Not sure why. But as Cobus mentioned, you can kind of hack around this. Do so at your own risk.

托管C ++,Delphi for .Net和Delphi Prism都支持使用非托管导出的开箱即用。按照设计,C#和VB.net不会。不知道为什么。但正如Cobus所说,你可以解决这个问题。这样做自担风险。

In addition to Hydra from RemObjects, AToZed is introducing CrossTalk.

除了RemObjects的Hydra之外,AToZed还推出了CrossTalk。

#5


You need to make the assembly (=C# DLL) accessible to COM, which is called Interop.

您需要使COM可以访问程序集(= C#DLL),这称为Interop。

See MSDN articles Assembly to Type Library Conversion and Packaging an Assembly for COM which describe the technical background and the utilities to perform the required operations.

请参阅MSDN文章装配到类型库转换和打包COM装配,其中描述了执行所需操作的技术背景和实用程序。

#6


I found a post from Robert Giesecke on the Delphi Prism newsgroups. In it, he announces a project you can add to a solution that lets you export arbitrary functions from a .Net DLL simply by adding the DllExport attribute to them. It supports marshaling just like DllImport. He demonstrates it with a Prism project, but I imagine it would work on C# classes as well. The post was made in March, so I'm not sure whether the attachment will still be available. The May release of Prism obviates such a tool since it supports unmanaged exports by itself.

我在Delphi Prism新闻组中找到了Robert Giesecke的帖子。在其中,他宣布了一个项目,您可以添加到一个解决方案中,只需向其添加DllExport属性,即可从.Net DLL中导出任意函数。它像DllImport一样支持编组。他用Prism项目演示了它,但我想它也适用于C#类。该帖子是在三月制作的,所以我不确定附件是否仍然可用。 5月发布的Prism消除了这种工具,因为它本身支持非托管出口。

#7


I'm assuming here that the Delphi app is not a .NET based app and therefore you need to host the .NET runtime in a Win32 process.

我假设Delphi应用程序不是基于.NET的应用程序,因此您需要在Win32进程中托管.NET运行时。

CorBindToRuntimeEx is a function inside MSCorEE.dll, which holds the .NET runtime. With it you can host the runtime and then create objects inside it and interact with them.

CorBindToRuntimeEx是MSCorEE.dll中的一个函数,它保存.NET运行时。有了它,您可以托管运行时,然后在其中创建对象并与它们进行交互。

#8


I'm quite sure this can not be done directly. You'll have to either write a layer in C++/CLI or expose the C# code as an ActiveX interface. But that second option may not meet your interface.

我很确定这不能直接完成。您必须在C ++ / CLI中编写图层或将C#代码公开为ActiveX接口。但是第二个选项可能不符合您的界面。

#9


This is not directly possible. C# is managed code. This means that it requires a very specific runtime environment in order to function, an environment which Delphi could not directly provide to it. It is not like C where you simply find the address and calling convention of the function and call it.

这不是直接可能的。 C#是托管代码。这意味着它需要一个非常特定的运行时环境才能运行,这是Delphi无法直接提供给它的环境。它不像C,你只需要找到函数的地址和调用约定并调用它。

However, it is possible to host the Common Language Runtime inside of a Delphi application (or any other Windows application). I have no idea how to do this. I just know that it's possible. (It's quite likely that's what this 'Hydra' that Steve mentioned will do.)

但是,可以在Delphi应用程序(或任何其他Windows应用程序)中托管公共语言运行时。我不知道该怎么做。我只知道这是可能的。 (这很可能是史蒂夫提到的'Hydra'所做的。)