0

我正在尝试将与 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 ==========

老实说,我真的不知道我是否按照建议正确编码了这段代码。

4

3 回答 3

1

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

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];
于 2013-05-11T19:37:48.403 回答
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);
于 2013-05-11T19:49:40.010 回答
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);
于 2019-08-01T09:26:17.797 回答