C++ to C# event handling

冷暖自知 提交于 2019-12-12 14:29:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!