从C#传递方法指针到Delphi DLL [英] Passing method pointer from C# to Delphi DLL

查看:81
本文介绍了从C#传递方法指针到Delphi DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将字符串作为PChar传递给Delphi构建的DLL时,我遇到了一些问题,感谢JensMühlenhoff解决了它.

现在我还有另一个问题-

如果Delphi声明是常规类型的过程,则在传递给DLL时,我已经成功地回调了c#方法,但是如果Delphi声明是方法类型的过程,则会出现尝试读取或写入受保护的内存"错误.

我尝试搜索...

这是Delphi声明

TCallBack = procedure ( s : String) of object;stdcall;

这是C#代码

[DllImport(
    "DLLTest.dll",
    CallingConvention = CallingConvention.StdCall,
    CharSet = CharSet.Ansi,
    EntryPoint = "DLL_Test"
)]
public static extern void DLL_Test(IntPtr p, [MarshalAs(UnmanagedType.LPStr)] string Location, int AIntValue);

public delegate void MethodCallBackEvent(string s);
public event MethodCallBackEvent Info;

public void GetInfo(string s)
{
    MessageBox.Show("Info: " + s);
}

用作

            Info = GetInfo; //or Info = new MethodCallBackEvent(GetInfo);
            IntPtr p = Marshal.GetFunctionPointerForDelegate(Info);

            DLL_Test(p, "location message", 10);

解决方案

这是一个有效的示例. DllTest1正在使用正常的函数回调. DllTest2期望将回调作为C#的直接函数指针(需要在Delphi方面进行小技巧),而DllTest3期望将Delphi方法回调指针作为(需要C#方面进行小技巧).

// 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#
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);
        [DllImport("Test.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "DllTest3")]
        public static extern void DllTest3(Method m, [MarshalAs(UnmanagedType.LPStr)] string s, int i);

        public delegate void Callback([MarshalAs(UnmanagedType.LPStr)] string s);
        public delegate void MethodCallback(IntPtr self, [MarshalAs(UnmanagedType.LPStr)] string s);
        public static void ShowInfo(string s)
        {
            Console.WriteLine("Info: " + s);
        }
        public static void ShowMethodInfo(IntPtr self, string s)
        {
            Console.WriteLine("Info: " + s);
        }


        static void Main(string[] args)
        {
            Method m;
            Callback info = ShowInfo;
            MethodCallback methodInfo = ShowMethodInfo;
            IntPtr p = Marshal.GetFunctionPointerForDelegate(info);
            IntPtr pm = Marshal.GetFunctionPointerForDelegate(methodInfo);

            // function callback example
            DllTest1(p, "test", 42);
            // method callback example 1
            DllTest2(pm, "test", 42);
            // method callback example 2
            m.code = pm;
            m.data = IntPtr.Zero;
            DllTest3(m, "test", 42);
        }
    }
}

I've had some problems passing string as PChar to Delphi built DLL, and resolved it thanks to Jens Mühlenhoff.

Now I have another issue -

I've made successful callback of c# method when passed to DLL if the Delphi declaration is a regular type procedure, but if Delphi declaration is a method type procedure I get "Attempted to read or write protected memory" error.

I tried searching...

Here is Delphi declaration

TCallBack = procedure ( s : String) of object;stdcall;

Here is C# code

[DllImport(
    "DLLTest.dll",
    CallingConvention = CallingConvention.StdCall,
    CharSet = CharSet.Ansi,
    EntryPoint = "DLL_Test"
)]
public static extern void DLL_Test(IntPtr p, [MarshalAs(UnmanagedType.LPStr)] string Location, int AIntValue);

public delegate void MethodCallBackEvent(string s);
public event MethodCallBackEvent Info;

public void GetInfo(string s)
{
    MessageBox.Show("Info: " + s);
}

used as

            Info = GetInfo; //or Info = new MethodCallBackEvent(GetInfo);
            IntPtr p = Marshal.GetFunctionPointerForDelegate(Info);

            DLL_Test(p, "location message", 10);

解决方案

Here is a working example. DllTest1 is using a normal function callback. DllTest2 expects the callback as a direct C# function pointer (requires a small hack on the Delphi side), and DllTest3 expects a Delphi method callback pointer (requires a small hack on the C# side).

// 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#
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);
        [DllImport("Test.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "DllTest3")]
        public static extern void DllTest3(Method m, [MarshalAs(UnmanagedType.LPStr)] string s, int i);

        public delegate void Callback([MarshalAs(UnmanagedType.LPStr)] string s);
        public delegate void MethodCallback(IntPtr self, [MarshalAs(UnmanagedType.LPStr)] string s);
        public static void ShowInfo(string s)
        {
            Console.WriteLine("Info: " + s);
        }
        public static void ShowMethodInfo(IntPtr self, string s)
        {
            Console.WriteLine("Info: " + s);
        }


        static void Main(string[] args)
        {
            Method m;
            Callback info = ShowInfo;
            MethodCallback methodInfo = ShowMethodInfo;
            IntPtr p = Marshal.GetFunctionPointerForDelegate(info);
            IntPtr pm = Marshal.GetFunctionPointerForDelegate(methodInfo);

            // function callback example
            DllTest1(p, "test", 42);
            // method callback example 1
            DllTest2(pm, "test", 42);
            // method callback example 2
            m.code = pm;
            m.data = IntPtr.Zero;
            DllTest3(m, "test", 42);
        }
    }
}

这篇关于从C#传递方法指针到Delphi DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆