如何从C++;到C# 我有C++签名方法,有以下签名:< /P> typedef char TNameFile[256]; void Foo(TNameFile** output);

如何从C++;到C# 我有C++签名方法,有以下签名:< /P> typedef char TNameFile[256]; void Foo(TNameFile** output);,c#,c++,interop,pinvoke,marshalling,C#,C++,Interop,Pinvoke,Marshalling,我已经想不出该怎么做了 > P>如果使用C++ + CLI而不是本机C++,那么您的生活会变得简单很多,您不必担心不安全的代码和MARSSELTEN: array<Byte>^ cppClass::cppFunction(TNameFile** input, int size) { array<Byte>^ output = gcnew array<Byte>(size); for(int i = 0; i < size; i++)

我已经想不出该怎么做了

> P>如果使用C++ + CLI而不是本机C++,那么您的生活会变得简单很多,您不必担心不安全的代码和MARSSELTEN:
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;
}
array^cppClass::cppFunction(TNameFile**input,int size)
{
数组^output=gcnew数组(大小);
对于(int i=0;i

如果你必须使用编组,那么试着按照沃特在回答中的建议使用

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

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();
    }
}
类程序
{
常数int blockLength=256;
/// 
//模拟C++ c++()函数的方法
/// 
/// 
静态void Foo(参考IntPtr输出)
{
常量int numberOfStrings=4;
字节[]块=新字节[块长度];
IntPtr dest=output=Marshal.AllocHGlobal((numberOfStrings*blockLength)+1);
for(int i=0;i

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

为什么有指向数组指针的指针?看起来至少其中一个是不必要的。两者都有可能,在这种情况下,数组也可能是返回值。@millimoose它是指向在Foo内部创建的TNameFile的输出数组的指针。这是第三方图书馆。无法对签名和内容执行任何操作请使用
静态外部void Foo(IntPtr输出)
然后使用
封送
类遍历数组并读取内容您尝试了哪些操作?您需要发送启动数组的地址的指针。一个数组已经(或多或少)是一个地址。我想说两个间接寻址都不是必需的。。。(除非你想在C中分配内存,并分配给C++中的“输出”,这将导致一个痛苦的世界)