0
Reply

calling C# method (Delegate) from Win32 DLL

btan100

btan100

Jul 12 2004 7:04 PM
1.7k
We try callback C# method from C++ DLL using delegate, using delegate without argument works fine for me however the CallBackFunctionWithParameter 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.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; namespace FunctionCallbacks { public class Form1 : System.Windows.Forms.Form { delegate void delegateMethod (); delegate void delegateMethodWithParameter(int i); private System.Windows.Forms.Button button1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; [DllImport("..\\..\\..\\C++\\Debug\\C++.dll")] private static extern void CallBackFunction (delegateMethod method); [DllImport("..\\..\\..\\C++\\Debug\\C++.dll")] private static extern void CallBackFunctionWithParameter (delegateMethodWithParameter method); public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(88, 120); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { } private void button1_Click(object sender, System.EventArgs e) { delegateMethod d = new delegateMethod(PrintMessage); delegateMethodWithParameter f = new delegateMethodWithParameter(PrintMessageWithParameter); CallBackFunction (d); //in C++ dll CallBackFunctionWithParameter(f); //in C++ dll } public void PrintMessage ( ) { MessageBox.Show ("Hello"); } public void PrintMessageWithParameter(int i) { MessageBox.Show(i.ToString()); } } } ======================================================================= In Win32 DLL =======================================================================. . . //takes a function pointer as an argument extern "C" __declspec(dllexport) void CallBackFunction (void(*callFunction())) { callFunction (); //call 'PrintMessage()' method in C# } //takes a function pointer as an argument extern "C" __declspec(dllexport) void CallBackFunctionWithParameter (void (*callFunctionWithParameter) (int i)) { callFunctionWithParameter(111); // call 'PrintMessage()' method // in C# } . . .