473,320 Members | 1,884 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,320 software developers and data experts.

Virutal Events in MC++

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_Derived Class1,&ReceiverClass::OnMyEvent);
}

Thanks for the help

-------------------------------------------
Roy Chastain
KMSystems, Inc.
Nov 16 '05 #1
4 1126
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.

Nov 16 '05 #2
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/de...us/vccore/html
/vcconEventHandlingInCOMPlus.asp

Events (Managed Extensions for C++ Specification)
http://msdn.microsoft.com/library/de...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,&Rec eiverClass::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.

Nov 16 '05 #3
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

On Sun, 12 Oct 2003 14:31:09 GMT, ti******@online.microsoft.com (Tian
Min Huang) wrote:
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/de...us/vccore/html
/vcconEventHandlingInCOMPlus.asp

Events (Managed Extensions for C++ Specification)
http://msdn.microsoft.com/library/de...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,&Rec eiverClass::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.


-------------------------------------------
Roy Chastain
KMSystems, Inc.
Nov 16 '05 #4
Hello Roy,

Thanks for your update. I am very glad to hear that the problem has been
resolved!
Does it matter where the __delegate is placed (within/without the

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.

Nov 16 '05 #5

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

Similar topics

5
by: Edward Diener | last post by:
This has occurred in MC++, but since there is very little response on that NG, I am also reporting it here in the hope that someone can find me a workaround, and report it to MS. If a __value...
8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
7
by: George Carl | last post by:
Perhaps this is a dumb question, but I've tried Google and the newsgroups, and can't figure out a satisfactory answer yet I'd like to make publish/subscribe scenario where the publishing object is...
1
by: Vladimir Scherbina Nickolaevich | last post by:
hello all, I need to hook some event handler in Managed C++code into event, that is declared, and is fired in C# code. as i understand, this maybe achieved via __hook, but when i write code...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
8
by: WebSnozz | last post by:
I have an application written in C that does a lot of low level stuff. It does a lot of things like casting from void*'s. I want to create a new GUI for it in either C# or MC++, but reuse the...
12
by: Aidal | last post by:
Hi NG. I'm creating a little app. that has to use a 3rd party API. The functions in this API has no return value but trigger events like OK or NOT OK with event args. Where I need to use this...
1
by: lallous | last post by:
Hello I don't have C++/CLI 2.0 reference handy and the code below was written after long hours of research and shallow readings. From what I see, the code works and looks logical to me. Can you...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.