Pinning char [] bei P / Invoke call

Ich habe einen Objektpool von Zeichenpuffern und übergebe diesen Puffer beim Aufruf von P / Invoke. Brauche ich Pinning-Puffer vor dem Anruf oder nicht?

Erste Ansatz

[DllImport("Name", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
private static extern void SomeMeth(char[] text, int size);

public static string CallSomeMeth()
{
    char[] buffer = CharBufferPool.Allocate();
    SomeMeth(buffer, 4095);
    string result = new string(buffer, 0, Array.IndexOf(buffer, '\0'));
    CharBufferPool.Free(buffer);
    return result;
}

Zweiter Ansatz:

[DllImport("Name", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
private static unsafe extern void SomeMeth(char* text, int size);

public static unsafe string CallSomeMeth2()
{
    char[] buffer = CharBufferPool.Allocate();
    string result;
    fixed (char* buff = buffer)
    {
        SomeMeth(buff, 4095);
        result = new string(buffer, 0, Array.IndexOf(buffer, '\0'));
    }
    CharBufferPool.Free(buffer);
    return result;
}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage