编辑:我已经发布了一个更好的实现,下面。我在这里留下这个回应是有道理的。从C#.NET应用程序调用Delphi DLL
我已经做了大量的搜索正确的方法在Delphi中编写一个DLL,并能够从C#调用它,传递和返回字符串。许多信息不完整或不正确。经过多次试验和错误,我找到了解决方案。
这是使用Delphi 2007和VS 2010编译的。我怀疑它在其他版本中也可以正常工作。
这是Delphi代码。请记住在项目中包含版本信息。
library DelphiLibrary;
uses SysUtils;
// Compiled using Delphi 2007.
// NOTE: If your project doesn't have version information included, you may
// receive the error "The "ResolveManifestFiles" task failed unexpectedly"
// when compiling the C# application.
{$R *.res}
// Example function takes an input integer and input string, and returns
// inputInt + 1, and inputString + ' ' + IntToStr(outputInt) as output
// parameters. If successful, the return result is nil (null), otherwise it is
// the exception message string.
// NOTE: I've posted a better version of this below. You should use that instead.
function DelphiFunction(inputInt : integer; inputString : PAnsiChar;
out outputInt : integer; out outputString : PAnsiChar)
: PAnsiChar; stdcall; export;
var s : string;
begin
outputInt := 0;
outputString := nil;
try
outputInt := inputInt + 1;
s := inputString + ' ' + IntToStr(outputInt);
outputString := PAnsiChar(s);
Result := nil;
except
on e : exception do Result := PAnsiChar(e.Message);
end;
end;
// I would have thought having "export" at the end of the function declartion
// (above) would have been enough to export the function, but I couldn't get it
// to work without this line also.
exports DelphiFunction;
begin
end.
这里的C#代码:
using System;
using System.Runtime.InteropServices;
namespace CsharpApp
{
class Program
{
// I added DelphiLibrary.dll to my project (NOT in References, but
// "Add existing file"). In Properties for the dll, I set "BuildAction"
// to None, and "Copy to Output Directory" to "Copy always".
// Make sure your Delphi dll has version information included.
[DllImport("DelphiLibrary.dll",
CallingConvention = CallingConvention.StdCall,
CharSet = CharSet.Ansi)]
public static extern
string DelphiFunction(int inputInt, string inputString,
out int outputInt, out string outputString);
static void Main(string[] args)
{
int inputInt = 1;
string inputString = "This is a test";
int outputInt;
string outputString;
// NOTE: I've posted a better version of this below. You should use that instead.
Console.WriteLine("inputInt = {0}, intputString = \"{1}\"",
inputInt, inputString);
var errorString = DelphiFunction(inputInt, inputString,
out outputInt, out outputString);
if (errorString != null)
Console.WriteLine("Error = \"{0}\"", errorString);
else
Console.WriteLine("outputInt = {0}, outputString = \"{1}\"",
outputInt, outputString);
Console.Write("Press Enter:");
Console.ReadLine();
}
}
}
我希望这个信息可以帮助别人不是要拉自己的头发和我一样多。
不是一个真正的问题,但+1 :)。 – 2010-11-12 09:57:22
我不熟悉德尔福,但知道如果可以将它转换为'COM'它很容易在c#中使用它,我做了一点搜索,并找到一些关于delphi和COM关系的资源:http:// delphi.about.com/library/weekly/aa122804a.htm – 2010-11-12 10:05:26
你应该改说你的问题,说“从C#.NET应用程序使用Delphi DLL的正确方法是什么?”然后用你的帖子的其余部分回答自己。见http://stackoverflow.com/faq(你可以回答你自己的问题)和在这里:http://meta.stackexchange.com/questions/12513/should-i-not-answer-my-own-questions – 2010-11-12 10:36:25