gpt4 book ai didi

c# - C++/cli : Cannot convert parameter 1

转载 作者:行者123 更新时间:2023-11-28 07:37:25 24 4
gpt4 key购买 nike

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

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

最佳答案

您需要转换来自 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];

关于c# - C++/cli : Cannot convert parameter 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16500892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com