Virutal Events in MC++

R

Roy Chastain

I have a case where I think I need to use a Managed Virtual Event.

I have a base class that will fire the event. That class is private
to the assembly. The receiving class in another assembly can only
hook the event in the derived classes.

I have something that compiles but it does not work (the event handler
never gets called) and I really don't even have an idea as to what to
do to fix it.

All the examples dealing with virtual events appear to be very
incomplete and they use the __delegate keyword. I would really rather
use the method syntax if that is possible. If it is not possible to
use the method syntax, does it matter where I put the __delegate
declaration?

Assembly1
private __gc BaseClass {
public:
__event virtual void MyEvent (UInt32 event_value);
void FireingFunction (void)
{ MyEvent(3); }
};

public __gc DerivedClass1: public BaseClass {
__event virutal void MyEvent (UInt32 event_value);
}

Assembly2
public __gc ReceiverClass {
public:
void OnMyEvent (UInt32 event_value);
void SomeFunction (void)
{
__hook(&DerivedClass1::MyEvent,instance_of_DerivedClass1,&ReceiverClass::OnMyEvent);
}

Thanks for the help
 
T

Tian Min Huang

Hi Roy,

Thanks for your post. I am checking this issue and will update you with my
information.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
T

Tian Min Huang

Hello Roy,

I reviewed your post and code carefully, and now I'd like to share the
following information with you:

1. In the ReceiverClass, it seems to me that you only declare the prototype
of the event handler onMyEvent without its implementation. I believe that
the reason may be the reason why it was not got called. In addition, you
will need to instantiate both DerivedClass1 and ReceiveClass and hook them
up.

2. In addition, I suggest you that you can declare
BaseClass::FireingFunction as pure virtual funciton if you want to the
receiver class to hook the event in BaseClass-derived class only.

3. I believe the following articles are helpful:
Event Handling in Managed Code
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html
/vcconEventHandlingInCOMPlus.asp

Events (Managed Extensions for C++ Specification)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/ht
ml/vcManagedExtensionsSpec_10.asp

4. I modified your code as the following, and it works properly. Please
check it on your side.
//------------------------Assembly 1-----------------------
private __gc class BaseClass
{
public:
__event virtual void MyEvent (UInt32 event_value);
virtual void FireingFunction (void) = 0;
};
public __gc class DerivedClass1: public BaseClass
{
public:
__event virtual void MyEvent (UInt32 event_value);
virtual void FireingFunction (void)
{ MyEvent(3); }
};
//---------------------- end of------------------------------------

//------------------------Assembly 2-----------------------
public __gc class ReceiverClass {
public:
void OnMyEvent (UInt32 event_value)
{
Console::WriteLine(S"Got Event");
};
void SomeFunction (DerivedClass1* pDerivedClass1)
{
__hook(&DerivedClass1::MyEvent,pDerivedClass1,&ReceiverClass::OnMyEvent);
}
};

int _tmain()
{
DerivedClass1* pSource = new DerivedClass1;
ReceiverClass* pReceiver = new ReceiverClass;
pReceiver->SomeFunction(pSource);
pSource->FireingFunction();
}
//----------------------------end of---------------------------

If you have any problems or concerns, please feel free to let me know.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
R

Roy Chastain

Thank you for your efforts. Between the time that I posted my
question and now, I ended up having to redo the code a little more. I
now have a virtual event defined within an __interface using the
__delegate syntax.

I _hook the event using the &interfacename::event name and it works.
I think it was a case of beginners luck!

Question.
Does it matter where the __delegate is placed (within/without the
class/interface that uses it, etc)?

Thanks
 
T

Tian Min Huang

Hello Roy,

Thanks for your update. I am very glad to hear that the problem has been
resolved!
class/interface that uses it, etc)?

No, both situation will work with proper code. However, I strongly
recommend define __delegate globally public instead of within a
class/interface, so that other classes can also use it directly to declare
__event with the same signature.

Does this anwer your question? Please post here if you need any further
assistance.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top