I have a .dll(not my own) that has a delegate. This delegate Callback function is:
"CallBackFN(ushort opCOde, IntPtr payload, uint size, uint localIP)"
How can i convert IntPtr to Byte[]? I think that payload is actually Byte[]. If it's not Byte[] and it's something else would i lose some data?
Have you looked into Marshal.Copy?
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.copy.aspx
Span<byte>
may be a better solution as it provides most features you need from a byte array. It is faster as you won't need to allocate and copy to a new buffer and safer as you will not have to use directly the pointer.It's included in.NET Core 2.1 and a nuget package (System.Memory) for .NET Framework 4.5 + and .NET Core 2.0 +;
According to this Stack Overflow question, you can do the following:
If you need performance, use it directly:
You can use Marshal.Copy Method (IntPtr, Byte[], Int32, Int32)
If it's a
byte[]
array:If it's not
byte[]
, the size parameter in of Marshal.Copy is the number of elements in the array, not the byte size. So, if you had an int[] array rather than a byte[] array, you would have to divide by 4 (bytes per int) to get the correct number of elements to copy, assuming your size parameter passed through the callback refers to # of bytes.