When passing a managed byte[] array through PInvoke to be filled in by Win32, does it need to be pinned?

前端 未结 3 674
迷失自我
迷失自我 2020-12-03 02:40

Suppose you\'re calling a Win32 function that will fill in your byte array. You create an array of size 32, empty. Then pass it in to the Win32 function to be filled int, an

3条回答
  •  长情又很酷
    2020-12-03 03:18

    according to msdn Marshaling Arrays of Types only an array passed by reference can be written to by unmanaged code. So it appears that you must declare the array parameter [out] or [in,out] if you want to fill it in on the unmanaged side.

    This page http://msdn.microsoft.com/en-us/library/aa719896(VS.71).aspx manages to go on and on without ever explicitly saying that the marshaller pins the arrays during the call from managed to unmanaged, but much of what it describes wouldn't work if the marshaller didn't pin.

提交回复
热议问题