473,626 Members | 3,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(&Derived Class1::MyEvent ,instance_of_De rivedClass1,&Re ceiverClass::On MyEvent);
}

Thanks for the help

-------------------------------------------
Roy Chastain
KMSystems, Inc.
Nov 16 '05 #1
4 1140
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::Fire ingFunction 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
/vcconEventHandl ingInCOMPlus.as p

Events (Managed Extensions for C++ Specification)
http://msdn.microsoft.com/library/de...us/vcmxspec/ht
ml/vcManagedExtens ionsSpec_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::WriteL ine(S"Got Event");
};
void SomeFunction (DerivedClass1* pDerivedClass1)
{
__hook(&Derived Class1::MyEvent ,pDerivedClass1 ,&ReceiverClass ::OnMyEvent);
}
};

int _tmain()
{
DerivedClass1* pSource = new DerivedClass1;
ReceiverClass* pReceiver = new ReceiverClass;
pReceiver->SomeFunction(p Source);
pSource->FireingFunctio n();
}
//----------------------------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::Fir eingFunction 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
/vcconEventHandl ingInCOMPlus.as p

Events (Managed Extensions for C++ Specification)
http://msdn.microsoft.com/library/de...us/vcmxspec/ht
ml/vcManagedExtens ionsSpec_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::WriteL ine(S"Got Event");
};
void SomeFunction (DerivedClass1* pDerivedClass1)
{
__hook(&Derived Class1::MyEvent ,pDerivedClass1 ,&ReceiverClass ::OnMyEvent);
}
};

int _tmain()
{
DerivedClass1* pSource = new DerivedClass1;
ReceiverClass* pReceiver = new ReceiverClass;
pReceiver->SomeFunction(p Source);
pSource->FireingFunctio n();
}
//----------------------------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
2186
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 class with an event is put into an assembly and a __gc class in another assembly, which has an instance of the __value class as a data member, attempts to attach its own event handler to the __value class's event, everything compiles correctly but...
8
2256
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
12118
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 events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years namespace MyNamespac public delegate void MyDel() public class MyBase public virtual...
7
2192
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 a couple of layers removed from the subscriber. The publisher fires an event and doesn't know who (if anyone) is listening. The subscriber, on the other hand, wants the information, but doesn't want to know who published it (to fully decouple the...
1
1392
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 like this __hook(&SomeControl::SomeEvent:, controlInstance, &CSomeClass::EventHandler);
2
2509
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 pointer or reference to a derived type of the base class's return type. In .NET the overridden virtual function is similar, but an actual parameter of the function can be a derived reference from the base class's reference also. This dichotomy...
8
2197
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 existing code. The options I've considered so far: 1. Create a new MC++ GUI project and add the *.c files to them and mark them with pragma unamanged. However, the /clr option does not compile *.c files, so I rename them to *.cpp, and now they...
12
2240
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 API I must have a synchrone modul so I need to force a synchrone look to this asynchrone model of the API. I was thinking this would call for some waiting for events and since I
1
5847
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 please review it and tell me if I overlooked something. On the other hand, can you suggest a good C++/CLI 2.0 book for starters? Code purpose: To use native EnumWindows() and have its callback handled
0
8199
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8365
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.