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

时间:2018-09-03 14:55:14

标签: c# c marshalling dllimport

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

这有效:

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();

我尝试将返回类型设置为封包LPArraySafeArraySafeArraySubType = VarEnum.VT_LPSTR,但是我无法使用它。使用LPArray似乎建议我为数组的大小(以及子类型LPStr)传入参数,但是在调用时我不知道大小。

如何实现?

1 个答案:

答案 0 :(得分:-2)

尝试以下操作:

        [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;
            }
        }

代码假定字符串数组存储为点数组。有时候在c中不是这种情况,那么下面的代码可能会起作用

            [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);
            }