2013-05-11 38 views
0

我有一個小問題,我會先解釋一下。我正嘗試將使用C++ dll的C#代碼轉換爲C++代碼,因此我的C++應用程序可以使用C#dll。以下是C#代碼C++/CLI:無法轉換參數

private void USB_OnSpecifiedDeviceRemoved(object sender, EventArgs e) 
     { 
      this.DevicePresent = false; 
     } 

this.USB.OnSpecifiedDeviceRemoved += new EventHandler(this.USB_OnSpecifiedDeviceRemoved); 

以下的部分是我的C++轉換

usb.OnSpecifiedDeviceRemoved += System::EventHandler(this->USB_OnSpecifiedDeviceRemoved(nullptr,nullptr)); 


    void MissileLauncher::USB_OnSpecifiedDeviceRemoved(System::Object sender, System::EventArgs e) 
    { 

    } 

當我運行我的C++代碼,我收到以下錯誤

1>------ Build started: Project: CallToCSharp, Configuration: Debug Win32 ------ 
1> MissileLauncher.cpp 
1>MissileLauncher.cpp(109): error C2664: 'MissileLauncher::USB_OnSpecifiedDeviceRemoved' : cannot convert parameter 1 from 'nullptr' to 'System::Object' 
1>   No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 
1>MissileLauncher.cpp(109): fatal error C1903: unable to recover from previous error(s); stopping compilation 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

爲什麼發生這種情況?有任何想法嗎?

回答

1

我看到您的轉換的兩個問題。首先,您錯誤地將事件處理程序添加到事件中。它應該類似於以下內容:

usb.OnSpecifiedDeviceRemoved += 
    gcnew System::EventHandler(this, 
     &MissileLauncher::USB_OnSpecifiedDeviceRemoved); 

其次,事件處理程序的簽名不正確。您需要使用的參數,這些參數使用^表示跟蹤引用:

void MissileLauncher::USB_OnSpecifiedDeviceRemoved(System::Object ^sender, 
                System::EventArgs ^e) 

希望有所幫助。

+0

什麼樣的幫助很大是這個!真棒! – 2013-05-11 17:33:02

相關問題