I have a strange situation where I must use C++ .net to handle some programming I must do that C# is not doing well with. I must allocate and access unmanaged memory and yes C# has the ability but it is not working as well as I had hoped, so I have created
my code for this part using C++ .net, which handles the unmanaged memory much better by the way. My problem is I need to send information from C++.net code to the C# code and I figured I could do either a event or a callback. Unfortunatly I am lost as to how
to create either of these methods as C++ is still confusing to me. I have never implemented a callback method before, but understand how to create a delegate on the C# side and pass it to the C++ code, but on the C++ side I am lost as to implementation of
this. Please can anyone help me. Again I need to have C++ .net code pass events to a C# code segement that will update the GUI for the user. Thanks
Jim
"It is kind of fun to do the impossible" - Walt Disney
ok, solved the evetn model and wished to share for more feedback.... The event model is really similar to C# so I created a C# project and added a C++ .net project to the solution. from there I added reference to the C++ .net project in my C# project which
gave me access to all events and methods. here is sample code: C# form 1.... Just a button and a label.
/// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label1; /// /// Required designer variable. /// private System.ComponentModel.Container
components = null; CREATED INSTANCE OF C++ OBJECT.......... PikaDialer.Class1 jim = new PikaDialer.Class1(); public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); WIRED THE EVENT TO MY FORM........ jim.OnEvent += new PikaDialer.eventHappened(WriteThis);
// // TODO: Add any constructor code after InitializeComponent call // } /// /// 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.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(96, 152); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click +=
new System.EventHandler(this.button1_Click); // // label1 // this.label1.Location = new System.Drawing.Point(104, 48); this.label1.Name = "label1"; this.label1.TabIndex = 1; this.label1.Text = "label1"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5,
13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.label1); 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 WriteThis(string x) { this.label1.Text = x.ToString(); } CALLED
A METHOD FROM THE C++ CODE THAT FIRES TEH EVENT........ private void button1_Click(object sender, System.EventArgs e) { jim.raiseEvent("TESTING EVENT"); } }
here is the c++ .net code. #pragma once using namespace System; namespace test1 { public __delegate void eventHappened(String* x); public __gc class Class1 { public: __event eventHappened* OnEvent; void raiseEvent(String* pMSG) { OnEvent(pMSG);
} }; }
Jim
"It is kind of fun to do the impossible" - Walt Disney
Member
40 Points
197 Posts
How to fire an event between C# and C++ .net
May 12, 2004 11:41 AM|ntjim|LINK
"It is kind of fun to do the impossible" - Walt Disney
Member
40 Points
197 Posts
Re: How to fire an event between C# and C++ .net
May 12, 2004 02:52 PM|ntjim|LINK
/// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; CREATED INSTANCE OF C++ OBJECT.......... PikaDialer.Class1 jim = new PikaDialer.Class1(); public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); WIRED THE EVENT TO MY FORM........ jim.OnEvent += new PikaDialer.eventHappened(WriteThis); // // TODO: Add any constructor code after InitializeComponent call // } /// /// 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.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(96, 152); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); // // label1 // this.label1.Location = new System.Drawing.Point(104, 48); this.label1.Name = "label1"; this.label1.TabIndex = 1; this.label1.Text = "label1"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.label1); 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 WriteThis(string x) { this.label1.Text = x.ToString(); } CALLED A METHOD FROM THE C++ CODE THAT FIRES TEH EVENT........ private void button1_Click(object sender, System.EventArgs e) { jim.raiseEvent("TESTING EVENT"); } }
here is the c++ .net code.#pragma once using namespace System; namespace test1 { public __delegate void eventHappened(String* x); public __gc class Class1 { public: __event eventHappened* OnEvent; void raiseEvent(String* pMSG) { OnEvent(pMSG); } }; }
"It is kind of fun to do the impossible" - Walt Disney