Skip to content

Instantly share code, notes, and snippets.

@shoover
Created August 15, 2008 18:18
Show Gist options
  • Save shoover/5618 to your computer and use it in GitHub Desktop.
Save shoover/5618 to your computer and use it in GitHub Desktop.
unsafe
{
byte** nativePackets = stackalloc byte*[packets.Length];
// Allocate native byte arrays.
for (int i = 0; i < packets.Length; i++)
{
nativePackets[i] = (byte *)Marshal.AllocHGlobal(packets[i].Length).ToPointer();
// TODO: try/finally, free memory allocated to date
}
try
{
Check(generate_packets(filename, nativePackets));
for (int i = 0; i < packets.Length; i++)
{
Marshal.Copy((IntPtr)nativePackets[i], packets[i], 0, packets[i].Length);
}
}
finally
{
for (int i = 0; i < packets.Length; i++)
{
Marshal.FreeHGlobal((IntPtr)nativePackets[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment