Pinning char[] on P/Invoke call

青春壹個敷衍的年華 提交于 2019-12-02 05:09:39

问题


I have object pool of char buffers and passing this buffer on P/Invoke call. Do i need pinning buffer before call or not?

First approach:

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

Second approach:

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

回答1:


No, there is auto-pinning for reference types passed to PInvoke.

From 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.

so first approach is ok.

Only:

SomeMeth(buffer, 4095);

I do think it is wrong to sprinkle constants around the code...

SomeMeth(buffer, buffer.Length);

or

SomeMeth(buffer, buffer.Length - 1);

would be probably better.



来源:https://stackoverflow.com/questions/30286032/pinning-char-on-p-invoke-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!