问题
So I've got my C# WinForm Application, from it I call my C++ CLI MFC dll library.
But there are some events on my C++ library, even this events happens in native (non-CLI) part of this library. And I need to call some code from my C# application and get some data maybe right there on this event.
so when this native function is called from client side :
bool __stdcall ClassName::WorkQuery()
{
......
switch(pp->code)
{
case READCOMPLEX:
..........
I need to bring the Data from C# so I need to register this as event somehow.
回答1:
So the problem is you need to run C# code in response to MFC event (or C++ callback), right?
Why can't you just register a native handler to the native event/callback, which then just invokes a .NET event, which can be the consumed from C#.
回答2:
You can create a .NET event from the C++/CLI event handle, and then use it natively. Just use EventWaitHandle or the derived AutoResetEvent and ManualResetEvent.
来源:https://stackoverflow.com/questions/7609225/c-to-c-sharp-event-handling