字符串数组作为从C到C的返回值?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (183)

如何将一个字符串数组(char**)从本机C函数返回到string[]C#中的托管类型?

这有效:

C:
char* get_text() {
    size_t length = ...;
    char *text = (char *)CoTaskMemAlloc((length + 1) * sizeof(char));
    // populate text...
    return text;
}

C#:
[DllImport(LibName, EntryPoint = "get_text", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.LPStr)]
private static extern string GetText();

但是,我无法开展工作:

C:
char** get_texts() {
    size_t count = ...;
    char **texts = (char **)CoTaskMemAloc(count * sizeof(char*));
    for (size_t i = 0; i < count; ++i) {
        size_t length = ...;
        texts[i] = (char *)CoTaskMemAlloc((length + 1) * sizeof(char));
        // populate texts[i]
    }
    return texts;
}

C#:
[DllImport(LibName, EntryPoint = "get_texts", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(???)]
private static extern string[] GetTexts();

我已经尝试将返回类型设置为marshal as LPArraySafeArraywith SafeArraySubType = VarEnum.VT_LPSTR,但我无法让它工作。使用LPArray似乎建议我应该传入数组大小的参数(以及子类型,这是LPStr),但我不知道调用时的大小。

怎么能实现这一目标?

提问于
用户回答回答于

试试以下:

        [DllImport("LibName", EntryPoint = "get_texts", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr GetTexts();

        static void Main(string[] args)
        {
            List<string> results = new List<string>();

            IntPtr data = GetTexts();

            IntPtr tmpPtr = IntPtr.Zero;
            while(data != null)
            {
                string str = Marshal.PtrToStringAnsi(data);
                results.Add(str);
                data += 1;
            }
        }

代码假定字符串数组存储为点数组。在某些时候,情况并非如此,然后下面的代码可能会起作用

            [DllImport("LibName", EntryPoint = "get_texts", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
            private static extern IntPtr GetTexts();

            static void Main(string[] args)
            {
                List<string> results = new List<string>();

                IntPtr data = GetTexts();

                IntPtr tmpPtr = IntPtr.Zero;
                Boolean first = true;
                string str = "";
                do
                {
                    if (first)
                    {
                        str = Marshal.PtrToStringAnsi(data);
                        first = false;
                    }
                    results.Add(str);
                    data += str.Length + 1;
                    str = Marshal.PtrToStringAnsi(data);

                } while (str.Length > 0);
            }

扫码关注云+社区

领取腾讯云代金券