Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / July 2004

Tip: Looking for answers? Try searching our database.

Delegate Callback with parameter

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Budi - 12 Jul 2004 23:25 GMT
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#
}
.
.
.
Robert Jordan - 15 Jul 2004 12:26 GMT
> Hi,
>
[quoted text clipped - 3 lines]
> the application after being called .
> Any suggestion will help and really appreaciated

the callback must be declared __stdcall in your C++ code.

rob

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.