Re: callbacks
- From: "WAkthar" <wakthar@xxxxxxxxxxx>
- Date: Mon, 6 Jun 2005 12:06:50 +0100
Thanks Eyal,
My scenario is a little different. There are two people involved in the
project.
I am writing the UI part and my collegue is writing a dll which will
communicate with teh serial port.
What we have is the ability for me to call functions that communicate with
the serial port. Now when certain events happen at the serial port, I need
to be notified.
To implement is I thought about using delegates as callbacks. The dll is
also written in C#.
When I create an object from the dll, I pass the function pointer (delegate)
to it. When an event happens on the serial port, this delegate is called and
I can update the UI.
Can you show how I can implement something like this please??
Thanks in advance!
"Eyal Safran" <eyal@xxxxxxxxxxx> wrote in message
news:1117819926.671559.175300@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Guys guys...
> The guy is looking for code examples...
>
> I hope this will clear some stuff:
>
> On your Cpp side:
>
> You need to define a function pointer such as:
> typedef void (__stdcall *LPFN_MANAGED_CALLBACK)(unsigned int x,unsigned
> int y);
>
> ** don't forget the __stdcall
>
> and a method to receive the EventHandler from the Managed side such as:
> void MyCppMethod(LPFN_MANAGED_CALLBACK lpfnManagedCallback)
> {
> // To call the callback do:
> lpfnManagedCallback(10 /*x value*/, 20 /*y value*/);
> }
>
>
> On the C# side:
> 1) you should define a delegate such as:
> public delegate void PointEventHandler(uint x, uint y);
>
> 2) a member to hold the PointEventHandler so that it won't be
> collected:
> private PointEventHandler m_pointCallback;
>
> 3) a p/Invoke for the Cpp method:
> [DllImport("CppDll.dll", CallingConvention=CallingConvention.StdCall)]
> extern private static void CppMethod(PointEventHandler pointCB);
>
> 4) you need to create a method to handle the Cpp call:
> private void CppCallbackHandler(uint x, uint y)
> {
> Console.WriteLine("Received a call from Cpp with X={0} & y={1}", x,
> y);
> }
>
> 5) you need to create the callback:
> m_pointCallback = new PointEventHandler(CppCallbackHandler);
>
> 6) and now you can pass the callback to Cpp side:
> CppMethod(m_pointCallback);
>
> Cheers,
> Eyal Safran
>
.
- Follow-Ups:
- Re: callbacks
- From: Eyal Safran
- Re: callbacks
- References:
- callbacks
- From: WAkthar
- Re: callbacks
- From: Eyal Safran
- callbacks
- Prev by Date: Re: custom drawing tree view control
- Next by Date: Re: Change Oracle password through C#
- Previous by thread: Re: callbacks
- Next by thread: Re: callbacks
- Index(es):
Relevant Pages
|