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

[c++/CLI] let an unmanaged class consume an event

Hello,

I have a managed type that defines an Event as one of its public
members. Is it possible for an unmanaged c++ class to register to that
event ? How can an unmanaged classmethod be called as a response to a
raised event ?

Within my unmanaged class I tried to following:

void MyUnManagedClass::InitializeAndRegister ()
{
RefClass^ rc = gcnew RefClass();
rc->SomeEvent += gcnew SomeEventHandlerDelegate
(this,&MyUnManagedClass::OnEvent);
}
void MyUnManagedClass::OnEvent()
{
// do something as a response to the event here
}

unfortuantely I get the compilererror: C3364 : delegate constructor
argument must be pointer to memeber function of managed class or global
function

does that mean only managed classes can register themselfes to an event?
If so, do you see any workaround to make unmanaged classes register
respond to events?

Do you have a simple example ?
Nov 17 '05 #1
2 3830
bonk wrote:
Hello,

I have a managed type that defines an Event as one of its public
members. Is it possible for an unmanaged c++ class to register to that
event ? How can an unmanaged classmethod be called as a response to a
raised event ?

Do you have a simple example ?


This is the exact opposite of what we were trying to do in the thread
above ("UINT16 error"). There the sender was unmanaged and the handler
was managed. Here the sender is managed and the handler is unmanaged.
Here, too, we need to introduce a thunk to forward the call, only in
this case the Thunk must be a managed class, so it can handle the
managed event:

delegate void MyEvent(int);

ref class Managed
// event sender
{
public:
event MyEvent^ SomeEvent;
void FireEvent()
{
SomeEvent(10);
}
};

class Unmanaged
// event receiver
{
public:
void OnEvent(int i) { }
};

ref class Thunk
// event forwarder
{
public:
Unmanaged* c;
void CallbackHandler(int i) { c->OnEvent(i); }
};

void Test()
{
Unmanaged u;
Managed^ m = gcnew Managed;
Thunk^ thunk = gcnew Thunk;
thunk->c = &u;
m->SomeEvent += gcnew MyEvent(thunk, &Thunk::CallbackHandler);
m->FireEvent();
}

Tom
Nov 17 '05 #2
The header msclr/event.h provides helper macros for this scenario.

"Tamas Demjen" <td*****@yahoo.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
bonk wrote:
Hello,

I have a managed type that defines an Event as one of its public members.
Is it possible for an unmanaged c++ class to register to that event ? How
can an unmanaged classmethod be called as a response to a raised event ?

Do you have a simple example ?


This is the exact opposite of what we were trying to do in the thread
above ("UINT16 error"). There the sender was unmanaged and the handler was
managed. Here the sender is managed and the handler is unmanaged. Here,
too, we need to introduce a thunk to forward the call, only in this case
the Thunk must be a managed class, so it can handle the managed event:

delegate void MyEvent(int);

ref class Managed
// event sender
{
public:
event MyEvent^ SomeEvent;
void FireEvent()
{
SomeEvent(10);
}
};

class Unmanaged
// event receiver
{
public:
void OnEvent(int i) { }
};

ref class Thunk
// event forwarder
{
public:
Unmanaged* c;
void CallbackHandler(int i) { c->OnEvent(i); }
};

void Test()
{
Unmanaged u;
Managed^ m = gcnew Managed;
Thunk^ thunk = gcnew Thunk;
thunk->c = &u;
m->SomeEvent += gcnew MyEvent(thunk, &Thunk::CallbackHandler);
m->FireEvent();
}

Tom

Nov 17 '05 #3

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

Similar topics

2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
8
by: Nadav | last post by:
Hi, I am writing a performence critical application, this require me to stick to unmanaged C++ as performance is much better using unmanaged C++ ( about 33% better ), Still, I am trying to avoid...
5
by: Lonewolf | last post by:
Hi, I'm not sure if this has been asked before so please pardon me if this is a repeated question. Basically I have some performance critical directshow codes which is implemented in native,...
81
by: Don Kim | last post by:
I've been looking for a new IT position, and so far, the majority of work with respect to the Windows platform is C#/.Net, with some vb.net requests every so often. Even many of the C++/MFC/ATL...
3
by: Cmtk Software | last post by:
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI managed c++ and from C#. I defined the following enum in a VS dll project set to be compiled with the /clr switch: ...
4
by: sebastian.dau | last post by:
Hello Newsgroup, I have a link error that I did not manage to fix. I basically consume a VC 8.0 C++ dll that exports a class with a method containing CString declaration as follows: class...
5
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I have a class that is writen in unmanaged pure native C++. This class files (h and cpp) are inserted to a managed C++ (VC++ 2005, C++/CLI) DLL compoenet. This DLL compoenet is used in a C#...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
5
by: John Doe | last post by:
I have a C++/CLI assembly that exposes an event, e.g: public ref class TestClass { public: event System::EventHandler^ someEvent; } My main C# project references this C++/CLI assembly, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.