2011-11-02 154 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*