Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have a managed C++ (.NET) DLL, and I want to raise an event from one of it's methods and catch it in a C# windows application. I've been playing around for a while but can't get it to work.

Can someone post a quick message that will show what lines of code I'd need in my C++ DLL and C# EXE?

Kind Regards,
Chris
Posted
Updated 8-Feb-11 20:00pm
v2
Comments
Manfred Rudolf Bihy 8-Feb-11 6:26am    
So why don't you show us what you tried?
cr101037 8-Feb-11 6:41am    
Ok, so here's what I've tried...In my C++ code:

//declare delegate (in my .h file)delegate void MyEventHandler(int);

//define event (in my .h file)event MyEventHandler^ Test;

//Fire event from my method (in my .cpp file)Test(35);

That compiles just fine, but not sure if it's right?And then in my C# code:

MyClass.Test += HandleCustomEvent;void HandleCustomEvent(int i){

// Do something useful here.}
Prerak Patel 8-Feb-11 6:46am    
MyClass.Test += new System.EventHandler(HandleCustomEvent);

Check the answer
Manfred Rudolf Bihy 8-Feb-11 6:51am    
When you said "And then in my C++ code" you probably meant to say C#? So your programs compile and run? What is the exact problem you are having. "I can't get it to work" is not very helpful. Please tell us what your exact problem is.
cr101037 8-Feb-11 7:05am    
Yeah sorry I meant C#, I've corrected that now. The error I'm receiving is "MyClass.Test is inaccessible due to its protection level" even though it's declared under public: in my C++ class. One thing I have just thought of, my class is defined as a ref class (public ref class MyClass), would that cause a problem?

Your delegate is not marked public, so it will default to internal. So even though your class is public, and so is the event member within the class, the event type itself will not be accesible. Mark the delegate to be public too.
 
Share this answer
 
Comments
Espen Harlinn 8-Feb-11 18:20pm    
Nicely spotted, a 5
Nish Nishant 8-Feb-11 18:45pm    
Thank you, Espen.
Sergey Alexandrovich Kryukov 8-Feb-11 18:21pm    
That's correct, my 5, let me clarify...
--SA
Nish Nishant 8-Feb-11 18:45pm    
Thank you.
Sergey Alexandrovich Kryukov 8-Feb-11 18:23pm    
For OP: 1) internal is the same as public with one difference: invisible outside declaring assembly; 2) protected internal is the same as protected with one difference: invisible even in derived classes outside declaring assembly; 3) for Reflection, all members are equally visible if you know how to look.
--SA
Events are not raised and caught, they are not exceptions. They are fired and handled. (Don't say, this is just terminology! I am not sure. Very often terminology is indicative of some misconception.) Now, the events ultimately are containers for delegate and invocation mechanism, there is nothing different from regular methods when it comes to inter-operation.

In the world of managed code, there in no barrier between C++/CLI (are you really using "managed C++"? This is obsolete dialect replaced with C++/CLI, distinctly different) and C#. One exception in value semantic for reference (C++/CLI ref) types — it is only specific to C++/CLI, so for C# you need to use reference semantic only (through "^" types and objects obtained via gcnew or "%"). And, of course, if you make mixed-mode C++ project (managed+unmanaged) you can use unmanaged code with C# only via System.Runtime.InteropServices.DLLImportAttribute (P/Invoke).

—SA
 
Share this answer
 
v3
Comments
Espen Harlinn 8-Feb-11 18:20pm    
Good effort, my 5
Nish Nishant 8-Feb-11 18:51pm    
>> Events are not raised and caught, they are not exceptions <<

Uhm, I think that's not correct. Events are "raised" or "fired" - and they both mean the same thing.

Exceptions are "thrown" and "caught".
Sergey Alexandrovich Kryukov 8-Feb-11 19:31pm    
See my comment below (failed to put in the tree under yours)
--SA
Sergey Alexandrovich Kryukov 8-Feb-11 19:31pm    
Eh... not quite. I agree that events are also "raised", but I suggest to avoid it, here is why: the terms are overloaded. The exception's "throw" keyword is a tradition going from C++ culture; in other (not just one, some are very influential and important) languages "raise" is used instead of throw (also "self" instead of "this"). We should better honor that, it helps to be more accurate. After all, we usually express many notions here in terms of Computer Science abstracted from particular language.
--SA
Nish Nishant 8-Feb-11 19:35pm    
Tell this to Microsoft. VB.NET actually has a keyword called RaiseEvent :-)

BTW I personally prefer and use Fire over Raise since I feel Fire is a more appropriate word.
C#
// This is in form_load for example
MyType var = new MyType();
var.MyEvent += new System.EventHandler(this.MyHandler);
// this is outside form_load, the event handler:
private void MyHandler(object sender, System.EventArgs e)
{
    // handle var.MyEvent here.

}
 
Share this answer
 
v2
Comments
cr101037 8-Feb-11 7:12am    
Hi Prerak,
This is the error I get:
'MyClass.Test' is inaccessible due to its protection level

And this is my class definition:

delegate void MyEventHandler(int);
public ref class MyClass
{

public:
MyClass();
~MyClass();

...

event MyEventHandler^ Test;
};
Sergey Alexandrovich Kryukov 8-Feb-11 18:40pm    
See answer by Nishant and my comment.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900