将参数中的方法指针传递给DLL [英] Passing a method pointer in parameter to a DLL

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

问题描述

你好,
那是我的问题:我有一个用C ++编写的DLL,我尝试用C#进行报告.
因此,我创建了一个包含从DLL导出的方法的类:

Hello,
That is my problem: I have a DLL written in C++ that I try to reporting in C#.
So I created a class that contains methods exported from the DLL:

using System.Runtime.InteropServices;
 
namespace MyDLL
{
  public class myDLL
  {
    /// <summary>
    /// Changing window title.
    /// </summary>
    [DllImport("MyDLL.dll")]
    public static extern bool WndSetTitle(IntPtr hWnd,
      [MarshalAs(UnmanagedType.LPStr)] string pTitle);
  }
}


问题在于此DLL必须报告主程序的方法.因此,有一个方法参数接收一个指向该方法的指针.
类代码应如下所示:


The problem is that this DLL must reporting a method of the main program. There is therefore a method parameter receives a pointer to this method.
The class code should look like this:

using System.Runtime.InteropServices;
 
namespace MyDLL
{
  public class myDLL
  {
    /// <summary>
    /// Sending the pointer to the method to be executed by the DLL.
    /// </summary>
    [DllImport("MyDLL.dll")]
    public static extern bool SetProcHandler(IntPtr pfMyProc);
  }
}


但是我不知道如何通过将一个点设置为另一种方法来报告此方法:叹气:.

有关信息,函数调用代码类似于以下Pascal:


But I know not how reporting this method by passing in setting one point to another method :sigh: .

For info, function calling code looks like this Pascal:

// The exported DLL function declaration
function SetProcHandler(pfMyProc: Pointer): Boolean; stdcall; 
  external 'MyDLL.dll';

// The function to be executed by the DLL Declaration
function MyProc(Prm1, Prm2: Integer): Integer;
begin
  // ...
end;

// Pointer to the function to run sending
SetProcHandler(@MyProc);


因此,我希望不要对我的应用程序太困惑:-O.
预先感谢您的答复.


So, I hope not to be too confusing in my application :-O .
Thank you in advance for your response.

推荐答案

我终于设法向我的DLL发送了一个指针:).
我有点启发了我轮播源代码.
我的代码是这样的:
在DLL回调的API中:
I''ve finally managed to send a pointer to my DLL :).
I am a little inspired me a carousel source code.
My code is something like this:
In the DLL callback''s API:
using System.Runtime.InteropServices;
 
namespace MyDLL
{
  public class myDLL
  {
    /// <summary>
    /// Sending the callback method's pointer to the DLL.
    /// </summary>
    [DllImport("MyDLL.dll")]
    public static extern bool SetProcHandler([MarshalAs(UnmanagedType.FunctionPtr)] MyProc pfMyProc);
    
    /// <summary>
    /// Method used for DLL's messages.
    /// </summary>
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    public delegate int SendProc(int Prm1, int Prm2);
  }
}


在主程序中:


In the main program:

using MyDLL
namespace MyProgram
{
  public partial class Principale : Form
  {
    /// <summary>
    /// Method for DLL's callback
    /// </summary>
    static MyDLL.SendProc myProc = null;
    public Principale()
    {
      InitializeComponent();
      
      myProc = new MyDLL.SendProc(MyProc);
      
      // Sending method to the DLL
      MyDLL.SetProcHandler(myProc);
      
      // Prevents the pointer cleaning
      GC.KeepAlive(myProc);
    }
        
    public unsafe int MyProc(int Prm1, int Prm2)
    {
      return 0;
      //
    }    
  }
}


我仍在进行一点测试,看看它是否不会产生错误:〜.
我希望它对某人仍然有用:-D.


I''m still a little test to see if it that generates not errors :~.
I hope that it can still be useful to someone :-D.


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

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