引发C ++事件并捕获C# [英] Raise C++ event and catch in C#

查看:66
本文介绍了引发C ++事件并捕获C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个托管的C ++(.NET)DLL,我想从其方法之一引发一个事件并将其捕获到C#Windows应用程序中.我已经玩了一段时间,但无法正常工作.

有人可以发布一条简短的消息,显示我的C ++ DLL和C#EXE中需要哪些代码行吗?

亲切的问候,
Chris

Hi All,
I have a managed C++ (.NET) DLL, and I want to raise an event from one of it''s methods and catch it in a C# windows application. I''ve been playing around for a while but can''t get it to work.

Can someone post a quick message that will show what lines of code I''d need in my C++ DLL and C# EXE?

Kind Regards,
Chris

推荐答案

您的delegate 未标记为public,因此它将默认为internal.因此,即使您的class public,并且class中的event 成员也是如此,event 类型本身也无法使用.将delegate 标记为也是public .
Your delegate is not marked public, so it will default to internal. So even though your class is public, and so is the event member within the class, the event type itself will not be accesible. Mark the delegate to be public too.


事件不会引发和捕获,它们也不例外.他们被解雇并处理. (不要说,这只是术语!我不确定.很多时候,术语表示一些误解.)现在,事件最终是委托和调用机制的容器,与常规方法没有什么不同进行互操作.

在托管代码的世界中,C ++/CLI(您是否真的在使用托管C ++"?这是用C ++/CLI替代的过时方言,就完全不同了)和C#之间没有障碍.引用(C ++/CLI ref)类型的值语义中的一个例外-它仅特定于C ++/CLI,因此对于C#,您仅需要使用引用语义(通过"^"类型和通过或"%").而且,当然,如果您创建混合模式C ++项目(托管+非托管),则只能通过System.Runtime.InteropServices.DLLImportAttribute(P/Invoke)将非托管代码与C#一起使用.
-SA
Events are not raised and caught, they are not exceptions. They are fired and handled. (Don''t say, this is just terminology! I am not sure. Very often terminology is indicative of some misconception.) Now, the events ultimately are containers for delegate and invocation mechanism, there is nothing different from regular methods when it comes to inter-operation.

In the world of managed code, there in no barrier between C++/CLI (are you really using "managed C++"? This is obsolete dialect replaced with C++/CLI, distinctly different) and C#. One exception in value semantic for reference (C++/CLI ref) types — it is only specific to C++/CLI, so for C# you need to use reference semantic only (through "^" types and objects obtained via gcnew or "%"). And, of course, if you make mixed-mode C++ project (managed+unmanaged) you can use unmanaged code with C# only via System.Runtime.InteropServices.DLLImportAttribute (P/Invoke).

—SA


// This is in form_load for example
MyType var = new MyType();
var.MyEvent += new System.EventHandler(this.MyHandler);
// this is outside form_load, the event handler:
private void MyHandler(object sender, System.EventArgs e)
{
    // handle var.MyEvent here.

}


这篇关于引发C ++事件并捕获C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆