C # cómo obtener Byte[] de IntPtr
Tengo un .dll (no es mío) que tiene un delegado. Esta función de devolución de llamada delegada es:
"CallBackFN (ushort opCOde, IntPtr payload , uint size, uint localIP)"
¿Cómo puedo convertir IntPtr a Byte []? Creo que la carga útil es en realidad Byte []. Si no es Byte [] y es otra cosa perdería algunos datos?
5 answers
¿Has investigado a Marshal?¿Entendido?
Http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.copy.aspx
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-30 13:30:41
Si se trata de bytes:
byte[] managedArray = new byte[size];
Marshal.Copy(pnt, managedArray, 0, size);
Si no es bytes, el parámetro size in de Marshal.Copiar es el número de elementos en la matriz, no el tamaño de byte. Por lo tanto, si tuviera una matriz int[] en lugar de una matriz byte [], tendría que dividir por 4 (bytes por int) para obtener el número correcto de elementos a copiar, asumiendo que su parámetro de tamaño pasado a través de la devolución de llamada se refiere a # de bytes.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-30 13:32:45
Si necesitas rendimiento, úsalo directamente:
unsafe {
byte *ptr = (byte *)buffer.ToPointer();
int offset = 0;
for (int i=0; i<height; i++)
{
for (int j=0; j<width; j++)
{
float b = (float)ptr[offset+0] / 255.0f;
float g = (float)ptr[offset+1] / 255.0f;
float r = (float)ptr[offset+2] / 255.0f;
float a = (float)ptr[offset+3] / 255.0f;
offset += 4;
UnityEngine.Color color = new UnityEngine.Color(r, g, b, a);
texture.SetPixel(j, height-i, color);
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-02 13:36:56
De acuerdo con esta pregunta de desbordamiento de pila , puede hacer lo siguiente:
var byteArray = new byte[dataBlockSize];
System.Runtime.InteropServices.Marshal.Copy(payload, byteArray, 0, dataBlockSize);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:02:39
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-30 13:31:09