473,395 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
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(PrintMessageWithParame ter);
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#
}
..
..
..
Nov 16 '05 #1
0 1535

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael C | last post by:
Hi all, Is there an easy way to get the parameters of an asynchronous delegate call from the callback function? Here's an example of what I'm trying to do: private delegate ArrayList...
8
by: Paul | last post by:
Hi, I have the following code in a winform: .... private delegate void DownloadDelegate(string src, string dest); .... private void DownloadFile(string src, string dest) { WebClient wc =...
2
by: Daniel Brown | last post by:
I am registering a managed delegate to an unmanaged DLL for asynchronous callback. I understand the garbage collector will free the managed resource if they go out of scope but with the...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
3
by: suria` | last post by:
hi New to delegates, trying to figure out how to use a delegate to fix a bug after upgrading code from VB6 to VB.NET I have the following callback proc: ' in vb 6.0 ...
0
by: Richard B | last post by:
I have to call a third-party program from mine. The third-party program expects a string parameter(that will be converted to an integer) that is a handle to a callback function in my program. The...
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
0
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to...
13
by: Wilfried Mestdagh | last post by:
Hi, I have an application using a DLL and callbacks. It generate random the error "A callback was made on a garbage collected delegate". I found some articles that the pointer to the delegate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.