我正在尝试将与 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 ==========
老实说,我真的不知道我是否按照建议正确编码了这段代码。