PC Review


Reply
Thread Tools Rate Thread

Delegate Callback with parameter

 
 
Budi
Guest
Posts: n/a
 
      12th Jul 2004
Hi,

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;

/// <summary>
/// Required designer variable.
/// </summary>
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();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
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 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

/// <summary>
/// The main entry point for the application.
/// </summary>
[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#
}
..
..
..
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
write delegate & callback in dll James Wong Microsoft Dot NET 5 28th Jul 2006 11:20 AM
delegate for unmanaged callback =?Utf-8?B?dGIyMDAw?= Microsoft Dot NET Compact Framework 5 10th Feb 2006 06:10 AM
callback Proc: need delegate obj. Pls help suria` Microsoft VB .NET 2 24th Nov 2003 09:02 AM
Delegate and callback problem Coder Microsoft Dot NET Framework 5 9th Sep 2003 07:00 PM
Delegate and callback problem Coder Microsoft Dot NET Compact Framework 5 9th Sep 2003 07:00 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:05 AM.