Delegate Callback with parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Budi

    Delegate Callback with parameter

    Hi,

    We try callback C# method from C++ DLL using delegate, using delegate
    without argument works fine for me however the
    CallBackFunctio nWithParameter which call delegate with parameter crash
    the application after being called .
    Any suggestion will help and really appreaciated


    Thanks

    Budi


    Here is the code :

    In C#
    =============== =============== =============== =============== ===========
    using System;
    using System.Drawing;
    using System.Collecti ons;
    using System.Componen tModel;
    using System.Windows. Forms;
    using System.Data;
    using System.Runtime. InteropServices ;

    namespace FunctionCallbac ks
    {
    public class Form1 : System.Windows. Forms.Form
    {
    delegate void delegateMethod ();
    delegate void delegateMethodW ithParameter(in t i);
    private System.Windows. Forms.Button button1;

    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.Componen tModel.Containe r components = null;

    [DllImport("..\\ ..\\..\\C++\\De bug\\C++.dll")]
    private static extern void CallBackFunctio n (delegateMethod method);

    [DllImport("..\\ ..\\..\\C++\\De bug\\C++.dll")]
    private static extern void CallBackFunctio nWithParameter
    (delegateMethod WithParameter method);


    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeCompo nent();
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Disp ose();
    }
    }
    base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeCompo nent()
    {
    this.button1 = new System.Windows. Forms.Button();
    this.SuspendLay out();
    //
    // button1
    //
    this.button1.Lo cation = new System.Drawing. Point(88, 120);
    this.button1.Na me = "button1";
    this.button1.Ta bIndex = 0;
    this.button1.Te xt = "button1";
    this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
    //
    // Form1
    //
    this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
    this.ClientSize = new System.Drawing. Size(292, 266);
    this.Controls.A dd(this.button1 );
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHan dler(this.Form1 _Load);
    this.ResumeLayo ut(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.Run (new Form1());
    }

    private void Form1_Load(obje ct sender, System.EventArg s e)
    {
    }

    private void button1_Click(o bject sender, System.EventArg s e)
    {
    delegateMethod d = new delegateMethod( PrintMessage);
    delegateMethodW ithParameter f = new
    delegateMethodW ithParameter(Pr intMessageWithP arameter);
    CallBackFunctio n (d); //in C++ dll
    CallBackFunctio nWithParameter( f); //in C++ dll
    }


    public void PrintMessage ( )
    {
    MessageBox.Show ("Hello");
    }

    public void PrintMessageWit hParameter(int i)
    {
    MessageBox.Show (i.ToString());
    }
    }
    }





    =============== =============== =============== =============== ===========

    In Win32 DLL
    =============== =============== =============== =============== ===========.
    ..
    ..
    //takes a function pointer as an argument
    extern "C" __declspec(dlle xport) void CallBackFunctio n
    (void(*callFunc tion()))
    {
    callFunction (); //call 'PrintMessage() ' method in C#
    }


    //takes a function pointer as an argument
    extern "C" __declspec(dlle xport) void CallBackFunctio nWithParameter
    (void (*callFunctionW ithParameter) (int i))
    {
    callFunctionWit hParameter(111) ; // call 'PrintMessage() ' method
    // in C#
    }
    ..
    ..
    ..
Working...