C ++ / CLI:无法转换参数

时间:2013-05-11 17:18:23

标签: c# .net visual-studio-2010 c++-cli

我有一个小问题,我会先解释一下。我正在尝试使用C ++ / CLI将使用C#dll的C#代码转换为C ++,因此我的C ++应用程序可以使用C#dll。以下是C#代码的一部分

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 个答案:

答案 0 :(得分:1)

我发现转换存在两个问题。首先,您错误地将事件处理程序添加到事件中。它应该类似于以下内容:

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

其次,事件处理程序的签名不正确。您需要对参数使用跟踪引用,这些参数使用^表示:

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

希望有所帮助。