本文介绍了将char []固定在P/Invoke通话上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有char缓冲区的对象池,并在P/Invoke调用中传递了此缓冲区.在呼叫之前是否需要固定缓冲区?

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

第一种方法:

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

第二种方法:

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

推荐答案

否,传递给PInvoke的引用类型会自动固定.

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

来自 https://msdn.microsoft.com/zh-CN /magazine/cc163910.aspx#S3 :

所以第一种方法还可以.

so first approach is ok.

仅:

SomeMeth(buffer, 4095);

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

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

SomeMeth(buffer, buffer.Length);

SomeMeth(buffer, buffer.Length - 1);

可能会更好.

这篇关于将char []固定在P/Invoke通话上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:23