VS C++ InvokeRequired 问题/委托无效

发布于 2024-12-13 06:01:31 字数 1291 浏览 2 评论 0原文

我是 C++ 新手,正在使用 VS2010。 有人可以检查下面的代码并帮助解决它吗?每次函数 UpdateDataGrid(unsigned char CANPacket[15]) 被调用时,以下消息将显示在新窗口中,并且应用程序将关闭。

An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll
Additional information: Object of type 'System.Byte' cannot be converted to type 'System.Byte*'.

我必须在这个项目中使用 unsinged char 数据类型,而不是 String^ 。 有什么方法可以纠正我的代码吗?

//Piece of my code

namespace VCCDC {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;

public ref class Form1 : public System::Windows::Forms::Form
{
    delegate void UpdateDataGridCallback(unsigned char CanPacket[15]);


    private: void UpdateDataGrid(unsigned char CANPacket[15]) {

        if (this->dataGridView1->InvokeRequired) {

            UpdateDataGridCallback^ d = gcnew UpdateDataGridCallback(this,&VCCDC::Form1::UpdateDataGrid);
            this->Invoke(d,gcnew unsigned char(CANPacket[15]));
        }

        else {
            //Update dataGridView1 with new data

        }

    }
}
}]

I am new to C++ and Im using VS2010.
Could someone check the code below and help to solve it? Everytime the function UpdateDataGrid(unsigned char CANPacket[15])
is called the following message is shown in a new window and the application closes.

An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll
Additional information: Object of type 'System.Byte' cannot be converted to type 'System.Byte*'.

I have to use unsinged char data type and not String^ in this project.
Is there any way to correct my code?

//Piece of my code

namespace VCCDC {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;

public ref class Form1 : public System::Windows::Forms::Form
{
    delegate void UpdateDataGridCallback(unsigned char CanPacket[15]);


    private: void UpdateDataGrid(unsigned char CANPacket[15]) {

        if (this->dataGridView1->InvokeRequired) {

            UpdateDataGridCallback^ d = gcnew UpdateDataGridCallback(this,&VCCDC::Form1::UpdateDataGrid);
            this->Invoke(d,gcnew unsigned char(CANPacket[15]));
        }

        else {
            //Update dataGridView1 with new data

        }

    }
}
}]

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻风 2024-12-20 06:01:31

将该行更改

this->Invoke(d,gcnew unsigned char(CANPacket[15]));

为“

this->Invoke(d,CANPacket));

您已经有一个 unsigned char 指针”,将其传递。使用gcnew,您打算创建另一个,但这是不必要的。

也是由此 gcnew 行引起的错误。您必须使用 Byte 参数构造 Byte*。你的也是Byte*

Change the line

this->Invoke(d,gcnew unsigned char(CANPacket[15]));

to

this->Invoke(d,CANPacket));

You already have an unsigned char pointer, pass it through. With gcnew you intent to create another one, which is unnecessary.

Also error caused by this gcnew line. You have to construct the Byte* with a Byte parameter. Yours is Byte* too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文