如何在C#创建DLL和Delphi的XE6调用 [英] How to Create DLL in C# and call in Delphi XE6
问题描述
我使用文件/新建工程/类库中创建一个DLL在VS2013。然后我试图在Delphi动态加载。但Delphiis返回 NIL
的程序 GetProcAddress的
。
我的C#&放大器;德尔福代码看起来像什么我都贴在下面。在代码 GetProcAddress的
将返回 NIL
。请告知,如果我失去了一些东西。
C#代码
使用系统;
命名空间TestDLL
{
公共类的Class1
{
公共静态字符串EchoString(字符串为EString)
{
收益为EString;
}
}
}
Delphi代码
键入
TEchoString =功能(为EString:字符串):整数; STDCALL;
功能TForm1.EchoString(为EString:字符串):整数;
开始
dllHandle:=调用LoadLibrary('TestDLL.dll');
如果dllHandle<> 0,那么
开始
@EchoString:= GetProcAddress的(dllHandle,'EchoString');如果
分配(EchoString),那么
EchoString(为EString)//调用函数
,否则
结果:= 0;
FreeLibrary则(dllHandle);
端
,否则
开始
ShowMessage('DLL没有找到');
端;
端;
C#DLL是一个托管程序集,并且不出口通过经典的PE出口其功能。您的选择:
- 使用C ++ / CLI混合模式来包装C#。然后您可以导出功能在通常的方式非托管模式。
- 使用罗伯特·捷德公司的 UnmanagedExports 的。这也许是比C ++ / CLI包装更方便。
- 揭露管理功能的COM对象。
一旦你得到尽可能选择这些选项,你将不得不处理你的字符串
数据类型的滥用之一。这是一个私人的Delphi数据类型不适用于互操作。对于这个问题 PWideChar
的简单例子就足够了。
I created a DLL in VS2013 using File/New Project/Class Library. I then tried to load it dynamically in Delphi. But Delphiis returning NIL
for procedure GetProcAddress
.
My C# & Delphi code looks like what I have posted below. In the code GetProcAddress
is returning NIL
. Please advise if I am missing something.
C# Code
using System;
namespace TestDLL
{
public class Class1
{
public static string EchoString(string eString)
{
return eString;
}
}
}
Delphi Code
Type
TEchoString = function (eString:string) : integer;stdcall;
function TForm1.EchoString(eString:string):integer;
begin
dllHandle := LoadLibrary('TestDLL.dll') ;
if dllHandle <> 0 then
begin
@EchoString := GetProcAddress(dllHandle, 'EchoString') ;
if Assigned (EchoString) then
EchoString(eString) //call the function
else
result := 0;
FreeLibrary(dllHandle) ;
end
else
begin
ShowMessage('dll not found ') ;
end;
end;
A C# DLL is a managed assembly and does not export its functionality via classic PE exports. Your options:
- Use C++/CLI mixed mode to wrap the C#. You can then export functions in unmanaged mode in the usual way.
- Use Robert Giesecke's UnmanagedExports. This is perhaps more convenient than a C++/CLI wrapper.
- Expose the managed functionality as a COM object.
Once you get as far as choosing one of these options you will have to deal with your misuse of the string
data type. That's a private Delphi data type that is not valid for interop. For the simple example in the question PWideChar
would suffice.
这篇关于如何在C#创建DLL和Delphi的XE6调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!