c ++ / cli:无法转换参数1

时间:2013-05-11 19:25:35

标签: c# c++ .net visual-studio-2010 command-line-interface

我正在尝试将使用dll的c#代码转换为c ++,因此我的c ++应用程序可以使用dll。所以我在这里开发一个c ++ / cli应用程序

请查看以下c#代码

private void SendUSBData(byte[] Data)
{
    if (this.USB.SpecifiedDevice != null)
    {
        this.USB.SpecifiedDevice.SendData(Data);
    }
}

SendData()是dll中可用的方法。以下是它在dll中的定义

public void SendData(byte[] data);

好的,现在在我的c ++代码中,我试图像这样转换c#代码

void MissileLauncher::sendUSBData(unsigned char *data)
{
    if(usb.SpecifiedDevice!=nullptr)
    {
        //System::Byte
        usb.SpecifiedDevice->SendData(&data);
    }
}

运行此代码时出现以下错误

1>------ Build started: Project: CallToCSharp, Configuration: Debug Win32 ------
1>  MissileLauncher.cpp
1>MissileLauncher.cpp(200): error C2664: 'UsbLibrary::SpecifiedDevice::SendData' : cannot convert parameter 1 from 'unsigned char **' to 'cli::array<Type,dimension> ^'
1>          with
1>          [
1>              Type=unsigned char,
1>              dimension=1
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我也尝试了以下内容

void MissileLauncher::sendUSBData(array<System::Byte>^data)
{
    if(usb.SpecifiedDevice!=nullptr)
    {
        //System::Byte
        usb.SpecifiedDevice->SendData(&data);
    }
}

它给了我以下错误

1>------ Build started: Project: CallToCSharp, Configuration: Debug Win32 ------
1>  MissileLauncher.cpp
1>MissileLauncher.cpp(125): error C2664: 'MissileLauncher::sendUSBData' : cannot convert parameter 1 from 'unsigned char *' to 'cli::array<Type> ^'
1>          with
1>          [
1>              Type=unsigned char
1>          ]
1>MissileLauncher.cpp(159): error C2664: 'MissileLauncher::sendUSBData' : cannot convert parameter 1 from 'unsigned char *' to 'cli::array<Type> ^'
1>          with
1>          [
1>              Type=unsigned char
1>          ]
1>MissileLauncher.cpp(164): error C2664: 'MissileLauncher::sendUSBData' : cannot convert parameter 1 from 'unsigned char *' to 'cli::array<Type> ^'
1>          with
1>          [
1>              Type=unsigned char
1>          ]
1>MissileLauncher.cpp(166): error C2664: 'MissileLauncher::sendUSBData' : cannot convert parameter 1 from 'unsigned char *' to 'cli::array<Type> ^'
1>          with
1>          [
1>              Type=unsigned char
1>          ]
1>MissileLauncher.cpp(186): error C2664: 'MissileLauncher::sendUSBData' : cannot convert parameter 1 from 'unsigned char *' to 'cli::array<Type> ^'
1>          with
1>          [
1>              Type=unsigned char
1>          ]
1>MissileLauncher.cpp(188): error C2664: 'MissileLauncher::sendUSBData' : cannot convert parameter 1 from 'unsigned char *' to 'cli::array<Type> ^'
1>          with
1>          [
1>              Type=unsigned char
1>          ]
1>MissileLauncher.cpp(200): error C2664: 'UsbLibrary::SpecifiedDevice::SendData' : cannot convert parameter 1 from 'cli::array<Type> ^*' to 'cli::array<Type> ^'
1>          with
1>          [
1>              Type=unsigned char
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我怎样摆脱这个?有什么想法吗?

更新

如答案中所提出的,尝试了下面的代码

void MissileLauncher::sendUSBData(unsigned char *data)
{
    if(usb.SpecifiedDevice!=nullptr)
    {
        int N = 10;
            cli::array<char>^ newData = gcnew cli::array<char>(N);

        for (int i = 0; i < N; ++i)
            newData[i] = data[i];

        //System::Byte
        usb.SpecifiedDevice->SendData(newData);
    }
}

现在,它给了我以下错误

1>------ Build started: Project: CallToCSharp, Configuration: Debug Win32 ------
1>  MissileLauncher.cpp
1>MissileLauncher.cpp(206): error C2664: 'UsbLibrary::SpecifiedDevice::SendData' : cannot convert parameter 1 from 'cli::array<Type> ^' to 'cli::array<Type,dimension> ^'
1>          with
1>          [
1>              Type=char
1>          ]
1>          and
1>          [
1>              Type=unsigned char,
1>              dimension=1
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

老实说,我真的不知道是否正确编码了这段代码。如果没有,那么请纠正我。请帮忙!

3 个答案:

答案 0 :(得分:1)

您需要将数据从char*转换为cli::array<char>^cli::array模板是在C ++ / CLI中实现System::Array的模板。为此,您需要知道数据的长度:

int N = /* length of data */
cli::array<System::Byte>^ newData = gcnew cli::array<System::Byte>(N);
for (int i = 0; i < N; ++i)
    newData[i] = data[i];

答案 1 :(得分:0)

你的第二个版本几乎是正确的。这是来自问题:

void MissileLauncher::sendUSBData(array<System::Byte>^data)
{
    if(usb.SpecifiedDevice!=nullptr)
    {
        //System::Byte
        usb.SpecifiedDevice->SendData(&data);
    }
}

这是修复:

        usb.SpecifiedDevice->SendData(/* & <- take that out */ data);

答案 2 :(得分:0)

通过使用以下代码,将c样式字节数组转换为CLR字节数组:

using namespace System::Runtime::InteropServices;

const char* from = xxxx; //(or unsigned char/signed char)
size_t length = strlen(from); 
array<System::Byte>^ target = gcnew array<unsigned char>(length);
Marshal::Copy((System::IntPtr)from, target, 0, length);