EDYCJA: Poniżej zamieściłem lepszą implementację tego. Zostawiłem to tutaj, więc odpowiedzi miałyby sens.Wywołanie biblioteki DLL Delphi z aplikacji C# .NET
Zrobiłem wiele poszukiwań dla prawidłowej metody pisania DLL w Delphi, i jest w stanie wywołać go z C#, przekazując i zwracając ciągi. Wiele informacji było niepełnych lub niepoprawnych. Po wielu próbach i błędach znalazłem rozwiązanie.
Został skompilowany przy użyciu Delphi 2007 i VS 2010. Podejrzewam, że będzie dobrze działać także w innych wersjach.
Oto kod Delphi. Pamiętaj o dołączeniu informacji o wersji do projektu.
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.
Oto kod 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();
}
}
}
Mam nadzieję, że ta informacja pomaga kogoś innego, aby nie trzeba ciągnąć swoje włosy tak samo jak ja.
naprawdę nie jest pytanie, ale +1 :). –
Nie jestem zaznajomiony z delphi, ale wiem, czy jest możliwe przekonwertowanie go na "COM' jego łatwego w użyciu w języku C#, zrobiłem małe wyszukiwanie i znajdę trochę zasobów, które są o delphi i relacji COM tutaj: http: // delphi.about.com/library/weekly/aa122804a.htm –
Powinieneś przeformułować swoje pytanie, aby powiedzieć "Jaki jest właściwy sposób korzystania z biblioteki DLL Delphi z aplikacji C# .NET?" a następnie odpowiedz sobie na resztę swojego postu. Zobacz http://stackoverflow.com/faq (możesz odpowiedzieć na własne pytanie) i tutaj: http://meta.stackexchange.com/questions/12513/should-i-not-answer-my-own-questions –