双阵列托管到非托管 [英] Double array Managed to Unmanaged
问题描述
我是C ++编程的新手.我在做小型应用程序,该应用程序进行高频计算.应用程序是用C#编程的,但是我想修改C ++本机库,该库将由托管代码共同使用.并且可以在C#中实现.我正在尝试将指针固定到托管到非托管,但是我不确定如何继续进行固定
Hi,
I am newbie in C++ programming. I work on small app which does high frequecy calculations. Application is programmed in C# however I want to amend C++ native library that would be cosumed by managed code. And would be implementable in C#. I am trying to pin pointers to managed to unmanaged, I am however unsure how to proceed with pinning
System::Double^ FSStat::Average(System::Double data[], int x1, int x2 )
{
UnmanagedArrayPointer* DataArray = GetUnmanagedArrayPointer(data);
double* unRes = st->Average(DataArray->Data, x1, x2);
System::Double^ res = gcnew Double(*unRes);
delete unRes;
delete DataArray;
return res;
}
UnmanagedArrayPointer* FSStat::GetUnmanagedArrayPointer( System::Double data[] )
{
double* arr = new double[sizeof(data)];
for (int i = 0; i<sizeof(data); i++)
{
pin_ptr<double> pinned = &arr[i];
data[i]->CopyToBuffer(pinned);
}
UnmanagedArrayPointer* res = new UnmanagedArrayPointer(arr* , data.Length);
return res;
}
错误1错误C2227:``-> CopyToBuffer''的左侧必须指向类/结构/联合/通用类型E:\ C#\ Aerion Workstation \ FinStatsLib \ FSStat.cpp 35 1 FinStatsLib
有人可以建议我如何进行此操作.非常感谢您的帮助.
最好的问候,
Jakub
Error 1 error C2227: left of ''->CopyToBuffer'' must point to class/struct/union/generic type E:\C#\Aerion Workstation\FinStatsLib\FSStat.cpp 35 1 FinStatsLib
Could somebody advice me how may I proceed with this. Your help would be much appreciated.
With best regards,
Jakub
推荐答案
首先以下是错误的:
Firstly the following is wrong:
double* arr = new double[sizeof(data)];
这不会获得传递给此方法的数组的大小,而是指针的大小.您将需要一个额外的参数来指定数组大小.
其次,以下行是错误的:
This will not get the size of the array passed in to this method, but the size of a pointer. You will need an extra parameter that gives the array size.
Secondly, the following line is wrong:
data[i]->CopyToBuffer(pinned);
因为 System::Double
[
because System::Double
[^] does not contain a CopyToBuffer()
method.
据我所知,您可以固定指针(如何:固定指针和数组 [ ^ ])从 cli :: array [使用P/Invoke在C#中调用Win32 DLL [
As far as I know, you can pin a pointer (How to: Pin Pointers and Arrays[^]) from a cli::array[^], call unmanaged code and then get back the result.
And for such scenario, it is probably not even necessary to use C++/CLI as you can use P/Invoke from C#.
Calling Win32 DLLs in C# with P/Invoke[^]
这篇关于双阵列托管到非托管的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!