double* 和 double** 是 blittable 类型吗? C#
我有一个关于将 C++ 数组编组到 C# 的问题。 double* 会自动转换为 double[] 吗?
我知道 double 是 blittable 类型,因此 C++ 中的 double 与 C# 中的 double 相同。 那么 double** 呢,它会转换为 double[,] 吗?
我有以下非托管函数: int get_values(double** param,int sz)
其中 param 是指向双精度数组的指针,sz 是其大小。
我怎样才能将此函数DLLImport到C#?
提前致谢
I have a question regarding marshalling of C++ arrays to C#.
Does the double* automatically convert to double[]?
I know double is a blittable type, so double from C++ is the same as double from C#.
And what about double**, does it convert to double[,] ?
I have the following unmanaged function:
int get_values(double** param,int sz)
where param is a pointer to array of doubles and sz it's size.
How can I DLLImport this function to C#?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该声明毫无意义。如果函数采用指向双精度数组的指针,则有意义,但声明将是
其中 size 将给出客户端分配的数组的大小,并且函数的返回值指示实际将多少双精度复制到数组中。等效的 P/Invoke 声明是:
在调用函数之前,您必须使用 new 分配数组到您承诺的大小。
但 double** 参数是一个难题。这可能意味着该函数返回一个指向双精度数组的指针,但大小参数就没有意义了。因为它是拥有数组并控制其大小的函数。或者这可能意味着客户端传递一个二维数组,但只有一个大小参数是没有意义的。
请用对该函数功能的正确解释来更新您的问题。
The declaration makes no sense. It would make sense if the function takes a pointer to an array of doubles, but then the declaration would be
Where size would give the size of the array allocated by the client and the function's return value indicates how many doubles were actually copied into the array. The equivalent P/Invoke declaration would be:
You have to allocate the array with new to the size you promised before calling the function.
But the double** argument is a hangup. It could mean that the function returns a pointer to an array of doubles but then the size argument makes little sense. Since it is the function that owns the array and controls its size. Or it could mean that the client passes a two-dimensional array, but then having only one size argument makes no sense.
Please update your question with the correct interpretation of what the function does.
本文内容如下:
This article says the following:
在函数参数类型的上下文中,
double*
和double[]
是相同的:在其他上下文中它们不是:
这对于
double** 也同样适用
与double*[]
,但请注意double[][]
(不带大小)是不同的,带大小意味着指向数组的指针(您必须指定除一维之外的所有维度)。听起来确实像是您在不需要时使用了
double**
。仅将数组作为double*
传递对您有用吗?(我知道我没有回答您的 DLLImport 问题,我不知道答案。)
In the context of a function's parameter type,
double*
anddouble[]
are identical:In other contexts they aren't:
This holds similarly for
double**
vsdouble*[]
, but notedouble[][]
(without sizes) is different, and with sizes means a pointer-to-array (you have to specify all but one dimension).It does sound like you're using a
double**
when it's not needed. Does just passing the array as adouble*
work for you?(I know I didn't answer your DLLImport question, I don't know the answer.)