gpt4 book ai didi

C# - 在 C# 中的等效数据类型中编码(marshal) char **

转载 作者:行者123 更新时间:2023-11-30 16:40:18 24 4
gpt4 key购买 nike

我正在开发使用基于 C 的库的 C#.Net 应用程序。由于它需要 C# 包装器,我正在实现代码来编码基于 C 的库中的函数,以便可以在 C# 中访问并正确映射数据类型。

我在库中有一个函数,它通过引用获取 char 数组,该函数包含用其中的值更新数组的逻辑。因此,在函数调用时我们可以传递空数组并在调用后访问结果值。

目标:传递空字符数组并通过引用返回值

C-dll 函数:

int function_call(char ** var);

C函数调用代码:

char *name = NULL;
int val = function_Call(&name);

C# 包装器代码:

[DllImport("mylibrary.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int function_call([In] string[] val);

C#函数调用代码:

 string[] name = null;
int ret = function_call(name);

我遇到过类似的查询:

  1. How to get char** using C#? [duplicate]
  2. How do I marshal a pointer to an array of pointers to structures?

  3. marshaling char**

  4. C# equivalent to C const char**

都试过了,还是不行。返回空值。有没有一种简单的工作方法来编码(marshal) Char ** ?

最佳答案

您可以简单地将其编码为 IntPtr :

[DllImport("mylibrary.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int function_call(IntPtr val);

只是在函数调用方面,将内存分配给指针并访问指针保存的数据:将非托管内存块中的数据编码为泛型类型参数指定的类型。

关于C# - 在 C# 中的等效数据类型中编码(marshal) char **,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46738890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com