从 c++ 传递参数到 c# 作为回调

发布于 2025-01-10 18:39:41 字数 1765 浏览 0 评论 0原文

我有一个 C++ 和 C# 应用程序相互通信。

我的设置方式如下:

c++ 项目在 c# 项目中引用如下

< img src="https://i.sstatic.net/YXrGd.png" alt="在此处输入图像描述">

现在它会执行与此问题无关的各种操作。

需求是,c#会调用c++,然后c++会做c#的回调事件。以下是我在 C# 中注册回调的方式:

                Add_CB_onVideoMediaReceived(OnVideoMediaReceived);

在 c++ 中,我有以下内容,它向事件添加回调

event OnVideoMediaReceived^ event_onVideoMediaReceived;

virtual void Add_CB_onVideoMediaReceived(OnVideoMediaReceived^ cb)
        {
            event_onVideoMediaReceived += cb;
        }

现在在 c++ 中,我调用此方法,我必须触发对 c# 的回调

unsigned int height = data->GetStreamHeight();
    unsigned int width = data->GetStreamWidth();
    unsigned int bufferLength = data->GetBufferLen();

    if (CMeetingRecordingControllerDotNetWrap::Instance)
    {
        if (data)
            CMeetingRecordingControllerDotNetWrap::Instance->_VideoMediaReceived(
                height,
                width,
                //data->GetStreamHeight(),
                bufferLength);
                //data->GetBuffer());
    }


void CMeetingRecordingControllerDotNetWrap::_VideoMediaReceived(unsigned int userId, unsigned int screenHeight,unsigned int screenWidth) {
        event_onVideoMediaReceived(userId, screenHeight,screenWidth);
    }

这基本上会触发 c#然而,我在唯一的第一个参数中得到了正确的值。对于参数的其余部分,例如,如果我传递的值是 300 作为整数,我在 C# 中得到一个非常长的数字,如 12333445444 而不是 300。

这是我的 C# 接收器代码是

  private void OnVideoMediaReceived(UInt32 userId, UInt32 screenHeight, UInt32 screenWidth)
        {
            Console.WriteLine("Username changed: user ID" + userId.ToString());
        }

I have a c++ and c# application talking to each other.

The way I have set things up is as follows:

the c++ project is referred to in the c# project as follows

enter image description here

Now it does various things which are not something related to this question.

The requirement is, c# will call c++ and then c++ will do a callback event of c#. Here is how I have registered a call back in C#:

                Add_CB_onVideoMediaReceived(OnVideoMediaReceived);

In c++, I have the following which is adding a callback to events

event OnVideoMediaReceived^ event_onVideoMediaReceived;

virtual void Add_CB_onVideoMediaReceived(OnVideoMediaReceived^ cb)
        {
            event_onVideoMediaReceived += cb;
        }

Now in c++, I am calling this method where I have to trigger a call back to c#

unsigned int height = data->GetStreamHeight();
    unsigned int width = data->GetStreamWidth();
    unsigned int bufferLength = data->GetBufferLen();

    if (CMeetingRecordingControllerDotNetWrap::Instance)
    {
        if (data)
            CMeetingRecordingControllerDotNetWrap::Instance->_VideoMediaReceived(
                height,
                width,
                //data->GetStreamHeight(),
                bufferLength);
                //data->GetBuffer());
    }


void CMeetingRecordingControllerDotNetWrap::_VideoMediaReceived(unsigned int userId, unsigned int screenHeight,unsigned int screenWidth) {
        event_onVideoMediaReceived(userId, screenHeight,screenWidth);
    }

That basically triggers the c# event, however, I get the correct value in the only first argument. For the rest of the argument, if for example, the value that I pass is 300 as an integer, I get a very long number in c# like 12333445444 instead of 300.

This is my c# receiver code is

  private void OnVideoMediaReceived(UInt32 userId, UInt32 screenHeight, UInt32 screenWidth)
        {
            Console.WriteLine("Username changed: user ID" + userId.ToString());
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文