【发布时间】:2014-09-01 07:38:01
【问题描述】:
我正在使用带有以下代码的混合程序集:
#include "stdafx.h"
#pragma managed
using namespace System::Security::Cryptography;
array<System::Byte, 1> ^ComputeHashS(array<System::Byte, 1> ^Data) {
RIPEMD160Managed^ r = gcnew RIPEMD160Managed();
return r->ComputeHash(Data);
}
#pragma unmanaged
BYTE *DoWork(BYTE *Data) {
BYTE *n = ComputeHashS(Data);
return DoSomething(n, 20);
}
其中 DoSomething(array, len) 是一个 UNMANAGED C++ 函数。但是,我收到以下错误:
argument of type "BYTE *" is incompatible with parameter of type "cli::array<unsigned char, 1> ^".
我是 C++/CLI 的新手,尤其是混合模式程序集,我该如何解决这个错误?
【问题讨论】:
-
您不能将该函数编译为非托管代码。您不能使用 pin_ptr,当其他代码访问 BYTE* 时,该数组将不再被固定。返回指向数组的原始指针最明显的问题是调用者不知道数组可能有多长,并且不能可靠地释放为数组分配的内存。在考虑从托管代码调用函数之前,您需要先解决这些问题。
标签: arrays c++-cli clr managed