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

Dynamic event delegates in c++

1
Using VS2005, I dynamically create an event delegate. Code follows question.
My method gets the event's parameters and passes them onto a common event handler. My delegate gets called when expected and in turn, calls my common event handler. However, once in the common event handler code, the "this pointer" is wrong and hence I can't access other class properties and methods.
Even if I invoke the dynamic method directly, the "this pointer" is stuffed.
Any ideas on what I'm doing wrong (or invalid assumptions I'm making)?


Thanks for any help.



void EventHandler::CreateDynamicEventHandler()
{

System::Type ^ eventClass = m_eventSource->GetTarget()->GetType();

// Get event info and the type of delegate.
array<System::Type^>^ args;
EventInfo ^ eventInfo = eventClass->GetEvent(m_eventName);
System::Type ^ tDelegate = eventInfo->EventHandlerType;
MethodInfo^ invoke = tDelegate->GetMethod( "Invoke" );


// The generated delegate will call a standard method (CommonEventHandler)
// Get the method info for that method.
array<System::Type^>^ commonArgs = {System::String::typeid, array<System::Object^>::typeid};
MethodInfo ^ methodInfo = EventHandler::typeid->GetMethod("CommonEventHandler", commonArgs);


// Create dynamic method
array<System::Type^> ^ paramTypes = GetDelegateParameterTypes(tDelegate);
DynamicMethod ^ dynamicMethod = gcnew DynamicMethod("Dyna",
nullptr,
paramTypes,
::EventHandler::typeid);

// Generate method body
ILGenerator ^ ilGen = dynamicMethod->GetILGenerator();

// Create a local variable 'args'
int paramCount = paramTypes->Length;
LocalBuilder ^ localObj = ilGen->DeclareLocal(array<System::Object^>::typeid);

// create object array of proper length
ilGen->Emit(OpCodes::Ldc_I4, paramCount);
ilGen->Emit(OpCodes::Newarr, System::Object::typeid);
ilGen->Emit(OpCodes::Stloc_0);

// Now put all arguments in the object array
for (System::Int32 i= 0; i<paramCount; i++)
{
System::Byte b = System::Convert::ToByte(i);
ilGen->Emit(OpCodes::Ldloc_0); // Local variable - the array
ilGen->Emit(OpCodes::Ldc_I4, i); // Index into array
ilGen->Emit(OpCodes::Ldarg_S, b); // Value to save is in bth argument to this method
if (paramTypes[i]->IsValueType)
ilGen->Emit(OpCodes::Box, paramTypes[i]); // Box value types
ilGen->Emit(OpCodes::Stelem_Ref, i);
}

ilGen->Emit(OpCodes::Ldarg_0); // 'this' ptr (required for instance calls)
ilGen->Emit(OpCodes::Ldstr, m_eventName); // Just to help with debugging!

ilGen->Emit(OpCodes::Ldloc_0);
ilGen->EmitCall(OpCodes::Call, methodInfo, nullptr);
ilGen->Emit(OpCodes::Ret);

// Create delegate (Also completes dynamic method build)
System::Delegate ^ del = dynamicMethod->CreateDelegate(tDelegate);

// Add delegate to event's invocation list
MethodInfo ^ addHandler = eventInfo->GetAddMethod();
array<System::Object^> ^ addHandlerArgs = {del};
addHandler->Invoke(m_eventSource->GetTarget(), addHandlerArgs);

}
Jan 15 '07 #1
0 2675

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ian Richardson | last post by:
I'm writing some code which does one thing when onreadystatechange occurs, e.g. handle.onreadystatechange = function() { blah(handle,other_params) }; ....but sometimes I need to add another,...
2
by: Lore Leuneog | last post by:
Hi. You can add new delegates for an event with += and you can remove delegates from an event by using: -= but is there a way (method) to list all delegates added to one event??? Sincerely...
3
by: Bob | last post by:
C# newbie here.... studying about delegates and now events. I just don't get the purpose of declaring events using the event keyword. What does this do for me that can't be done using only...
4
by: Bob Rock | last post by:
Hello, I was wondering if callback delegates, called in response to a event, are executed in their own thread. I was suspecting the OS might spawn a new thread and have the delegate execute in...
3
by: Chua Wen Ching | last post by:
Hi there, I just read Chris Sells's article at http://www.codeproject.com/csharp/delegate_bedtime.asp?df=100&forumid=2983&select=922269#xx922269xx I wonder i can do this: 1) I want to...
1
by: MuZZy | last post by:
Hi, Is there a way to remove all event handlers for a control's event? Say, i have a button and i want to remove all button.Click events for it - i don't know how many of them was hooked to the...
2
by: Mark Broadbent | last post by:
I had a requirement to code dynamic delegates, so they attach detach from the button as needed ... then after time realised that the button is not retaining it's click handlers. To circumvent this...
1
by: Jeremy Martin | last post by:
Hi, I am currently learning c# so be gentle :) I am converting a Delphi.NET website to c# website and I am confused as to how events are handled. 2 Situations. 1. A component on a...
4
by: tshad | last post by:
I am just getting started with events and had a couple of questions on why they do what they do. If you have a textbox and you want to handle an event you can just do: ...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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...
0
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...

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.