2011-11-02 75 views
0

我是新来的C + +和Im使用VS2010。 有人可以检查下面的代码并帮助解决它吗?每次调用功能UpdateDataGrid(unsigned char CANPacket[15]) 被调用时,以下消息将显示在新窗口中并且应用程序关闭。VS C++问题与InvokeRequired /委托无效

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 

     } 

    } 
} 
}] 

回答

0

更改线路

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

this->Invoke(d,CANPacket)); 

你已经有一个unsigned char指针,让它通过。随着gcnew你打算创建另一个,这是不必要的。

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