Can i trigger an event to a C# app in a C++ DLL?
-
i m writting a C++ Win32 DLL which is called by a C# application. i want the DLL can trigger an event to the Calling C# app after some operations which might like the Win MSG mechanism: The DLL send a message to the C# app and the C# app handle the msg. the C# use the EventHandle/EventArgs on this issue. But can i trigger an event to C# app in a C++ Win32 DLL ? please help me.
-
i m writting a C++ Win32 DLL which is called by a C# application. i want the DLL can trigger an event to the Calling C# app after some operations which might like the Win MSG mechanism: The DLL send a message to the C# app and the C# app handle the msg. the C# use the EventHandle/EventArgs on this issue. But can i trigger an event to C# app in a C++ Win32 DLL ? please help me.
If you pass a handle (the
Handle
property for controls) to your unmanaged DLL (P/Invoke is easy enough, and I assume you know how to do it), you could always callSendMessage
in your unmanaged code to send a message to a control. In your control class (assuming you've extended a class), overrideWndProc
and handle the message. Don't forget to callbase.WndProc
to continue processing all messages that go through the window procedure for theControl
. Another way is to expose your .NET component as aCOM
control (following correct guidelines, which aren't too obvious when reading the .NET Framework SDK but any COM developers should know). Your unmanaged code could instantiate a new or reference an existing instances and call a method that raises an event defined on your .NET component. While my article isn't exactly about this, per se, it does offer a lot of insight into this method. Read Embedding .NET Controls in Java[^], which discusses bridging .NET and Java with C++ (JNI and COM). You shoudl find a lot of helpful information in there about what you're trying to do.Microsoft MVP, Visual C# My Articles