C#的调用Delphi的DLL没有问题,DLL回调时遇到了麻烦,网上找了个方法,,解决了这个问题
Delphi部分,列举了三种回调函数定义
[delphi]
library test;
uses
SysUtils;
{$R *.res}
type
TCallback = procedure (P: PChar); stdcall;
TMethodCallback = procedure (P: PChar) of object; stdcall;
procedure DllTest1(Callback: TCallback; P: PChar; I: Integer); stdcall;
var
S: string;
begin
S := Format(‘DllTest1 ‘‘%s‘‘ %d‘, [P, I]);
if Assigned(Callback) then
Callback(PChar(S));
end;
procedure DllTest2(_Callback: Pointer; P: PChar; I: Integer); stdcall;
var
Callback: TMethodCallback absolute _Callback;
S: string;
begin
S := Format(‘DllTest2 ‘‘%s‘‘ %d‘, [P, I]);
if Assigned(Callback) then
Callback(PChar(S));
end;
procedure DllTest3(Callback: TMethodCallback; P: PChar; I: Integer); stdcall;
var
S: string;
begin
S := Format(‘DllTest3 ‘‘%s‘‘ %d‘, [P, I]);
if Assigned(Callback) then
Callback(PChar(S));
end;
exports
DllTest1,
DllTest2,
DllTest3;
begin
end.
C#部分
[csharp]
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace DllTest
{
class Program
{
public struct Method
{
public IntPtr code;
public IntPtr data;
}
[DllImport("Test.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "DllTest1")]
public static extern void DllTest1(IntPtr p, [MarshalAs(UnmanagedType.LPStr)] string s, int i);
[DllImport("Test.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "DllTest2")]
public static extern void DllTest2(IntPtr p, [MarshalAs(UnmanagedType.LPStr)] string s, int i);