【问题标题】:How to marshall pointer to array of char[256] from C++ to C#如何将指向 char[256] 数组的指针从 C++ 编组到 C#
【发布时间】:2012-07-25 09:13:26
【问题描述】:

我有具有以下签名的 C++ 方法:

typedef char TNameFile[256];

void Foo(TNameFile** output);

我已经没有如何编组它的想法了。

【问题讨论】:

  • 为什么有一个指向数组指针的指针?似乎其中至少有一个是不必要的。可能两者都有,在这种情况下,数组也可能是返回值。
  • @millimoose 它是指向在 Foo 中创建的 TNameFile 的输出数组的指针。这是第三方库。不能对签名和内容做任何事情
  • 使用static extern void Foo(IntPtr output)然后使用Marshal类遍历数组并读取内容
  • 你试过什么?您需要发送开始数组的地址的指针。数组已经(或多或少)是一个地址。
  • 我想说这两种间接方式都不是必需的......(除非你想在 C# 中分配内存并在 C++ 中分配给“输出”,这将导致世界痛苦)跨度>

标签: c# c++ interop pinvoke marshalling


【解决方案1】:

假设它们返回一个空字符串作为最后一个元素:

static extern void Foo(ref IntPtr output);

IntPtr ptr = IntPtr.Zero;
Foo(ref ptr);
while (Marshal.ReadByte(ptr) != 0)
{
   Debug.Print(Marshal.PtrToStringAnsi(ptr, 256).TrimEnd('\0'));
   ptr = new IntPtr(ptr.ToInt64() + 256);
}

编辑:因为我已经在我的智能手机上编写了上面的代码,所以我今天早上测试了代码,它似乎应该可以工作(我只需要添加TrimEnd('\0'))。这是我的测试用例:

class Program
{
    const int blockLength = 256;

    /// <summary>
    /// Method that simulates your C++ Foo() function
    /// </summary>
    /// <param name="output"></param>
    static void Foo(ref IntPtr output)
    {
        const int numberOfStrings = 4;
        byte[] block = new byte[blockLength];
        IntPtr dest = output = Marshal.AllocHGlobal((numberOfStrings * blockLength) + 1);
        for (int i = 0; i < numberOfStrings; i++)
        {
            byte[] source = Encoding.UTF8.GetBytes("Test " + i);
            Array.Clear(block, 0, blockLength);
            source.CopyTo(block, 0);
            Marshal.Copy(block, 0, dest, blockLength);
            dest = new IntPtr(dest.ToInt64() + blockLength);
        }
        Marshal.WriteByte(dest, 0); // terminate
    }

    /// <summary>
    /// Method that calls the simulated C++ Foo() and yields each string
    /// </summary>
    /// <returns></returns>
    static IEnumerable<string> FooCaller()
    {
        IntPtr ptr = IntPtr.Zero;
        Foo(ref ptr);
        while (Marshal.ReadByte(ptr) != 0)
        {
            yield return Marshal.PtrToStringAnsi(ptr, blockLength).TrimEnd('\0');
            ptr = new IntPtr(ptr.ToInt64() + blockLength);
        }
    }

    static void Main(string[] args)
    {
        foreach (string fn in FooCaller())
        {
            Console.WriteLine(fn);
        }
        Console.ReadKey();
    }
}

还有一个问题:谁来释放缓冲区?

【讨论】:

    【解决方案2】:

    如果您使用 C++/CLI 而不是本机 C++,这将使您的生活更轻松,您不必担心不安全的代码和编组:

    array<Byte>^ cppClass::cppFunction(TNameFile** input, int size)
    {
        array<Byte>^ output = gcnew array<Byte>(size);
    
        for(int i = 0; i < size; i++)
            output[i] = (**input)[i];
    
        return output;
    }
    

    如果您必须使用编组,请尝试使用 Marshal.PtrToStringAnsi,正如 WouterH 在他的回答中所建议的那样。

    【讨论】:

      猜你喜欢
      • 2021-01-26
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      相关资源
      最近更新 更多