在 P/Invoke 调用上固定 char[]

Pinning char[] on P/Invoke call

我有 char 缓冲区的对象池,并在 P/Invoke 调用时传递此缓冲区。我是否需要在通话前固定缓冲区?

第一种方法:

[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, '[=11=]'));
    CharBufferPool.Free(buffer);
    return result;
}

第二种方法:

[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, '[=12=]'));
    }
    CharBufferPool.Free(buffer);
    return result;
}

不,传递给 PInvoke 的引用类型会自动固定。

来自https://msdn.microsoft.com/en-us/magazine/cc163910.aspx#S3

When the runtime marshaler sees that your code is passing to native code a reference to a managed reference object, it automatically pins the object.

所以第一种方法就可以了。

仅:

SomeMeth(buffer, 4095);

我确实认为在代码周围散布常量是错误的...

SomeMeth(buffer, buffer.Length);

SomeMeth(buffer, buffer.Length - 1);

可能会更好。