我有一个用 C 编写的 DLL 文件。我尝试在我的 C# 代码中使用 C DLL。 C 程序方法返回 int** 。 c中的int**和c#中的int[][]是一样的吗?
我在从 C 程序返回值时遇到错误。
c 方法
__declspec(dllexport) int someMethod
(
size_t *foo,
int **bar
)
{
*foo = 10;
**bar = 10;
}
我的c#代码
[DllImport(@"my.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = true, BestFitMapping = true, EntryPoint = "someMethod")]
public unsafe static extern int someMethod
(
out Int16 foo,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(JaggedArrayMarshaler))]
out int[][] bar
);
最佳答案
C 的 int**
和 C# 的 int[][]
在概念上相似,但在二进制级别不同:
-
int**
是指向指针的指针,指向非托管堆内存中的原始数据。 -
int[][]
是对托管数组的托管引用的托管引用;每个指向数组的标题(包含它的长度)而不是它的原始数据。
我不知道为 C# 编码什么最容易。可以SAFEARRAY
被嵌套?如果是这样,它可能很容易映射到 C# 数组。
肯定有效的解决方案是使用 C++/CLI DLL 作为两个代码之间的粘合剂,以转换 int**
进入cli::array<cli::array<int>^>^
(这涉及复制指向的数据),然后返回到 C#。
关于c# - c 中的 int** 和 c# 中的 int[][] 是一样的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16010944/