Managed C# DLL called from native C main with callbacks
-
I have been struggling HARD to find a solution to this. In order to get some long-term platform compatibility (PC simulator, embedded target, etc.) I am implementing my main code in C, and a GUI emulator in C# as a DLL. I am able to make calls into the C# DLL from my native code by exposing the DLL functions as COM objects (I think...). Data are passed in and out of the library functions just fine. I would like to have callbacks made back to the native code when events are triggered in the C# code. You can imagine that when a button is clicked on my fancy C# GUI, my native code gets called. I cannot seem to get this callback mechanism working. Can anyone please give an example, syntax, etc. for assigning a native callback to a managed C# event (or something like that)? There are TONS of examples of using managed code to call un-managed DLLs, but very little going to the other way. Thanks in advance. -Stuart
-
I have been struggling HARD to find a solution to this. In order to get some long-term platform compatibility (PC simulator, embedded target, etc.) I am implementing my main code in C, and a GUI emulator in C# as a DLL. I am able to make calls into the C# DLL from my native code by exposing the DLL functions as COM objects (I think...). Data are passed in and out of the library functions just fine. I would like to have callbacks made back to the native code when events are triggered in the C# code. You can imagine that when a button is clicked on my fancy C# GUI, my native code gets called. I cannot seem to get this callback mechanism working. Can anyone please give an example, syntax, etc. for assigning a native callback to a managed C# event (or something like that)? There are TONS of examples of using managed code to call un-managed DLLs, but very little going to the other way. Thanks in advance. -Stuart
Stuart, you need to expose your event as described in this[^] example. This creates the COM hookup for the event handler that you can reference through your C code.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Stuart, you need to expose your event as described in this[^] example. This creates the COM hookup for the event handler that you can reference through your C code.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Nice one - I think that is going to come in handy to me some day too.
Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
-
Stuart, you need to expose your event as described in this[^] example. This creates the COM hookup for the event handler that you can reference through your C code.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Peter, this is helpful, but at least half of the challenge is on the native (un-managed) side. I think now I have a syntactic problem with registering my native C function with the managed event. Any more tips? Thanks!
-
Peter, this is helpful, but at least half of the challenge is on the native (un-managed) side. I think now I have a syntactic problem with registering my native C function with the managed event. Any more tips? Thanks!
That side is going to require you to hook up to the COM event in your C code. You might want to read this[^] article to see how C++ handles it (it should be easy enough to apply this to C).
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
That side is going to require you to hook up to the COM event in your C code. You might want to read this[^] article to see how C++ handles it (it should be easy enough to apply this to C).
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
"Calling" the managed functions isn't really a problem anymore. The real problem is passing in a reference to the unamanged event handler function into the managed event. Sorry to keep pushing on this, but does sound like you know what you're doing! Thanks again.
-
"Calling" the managed functions isn't really a problem anymore. The real problem is passing in a reference to the unamanged event handler function into the managed event. Sorry to keep pushing on this, but does sound like you know what you're doing! Thanks again.
Have you assigned the IConnectionPoint interface[^]? As you've implemented the event interface in your C#, the CLR Interop automatically applies the juice that hooks this together.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility