Click here to Skip to main content
15,446,510 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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);
  }
}

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:
C#
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);
  }
}

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:
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);

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

1 solution

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:
C#
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:
C#
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;
      //
    }    
  }
}

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900