2009-07-26 23:22:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2010-05-19 23:42:10 +00:00
|
|
|
|
using System.Security;
|
2009-07-26 23:22:00 +00:00
|
|
|
|
|
|
|
|
|
namespace Jellyfish.Library
|
|
|
|
|
{
|
|
|
|
|
public static class GCHandleHelpers
|
|
|
|
|
{
|
2010-05-19 23:42:10 +00:00
|
|
|
|
[SecurityCritical]
|
2009-07-26 23:22:00 +00:00
|
|
|
|
public static void Pin(object value, Action<IntPtr> action)
|
|
|
|
|
{
|
2009-12-11 09:12:31 +00:00
|
|
|
|
if (action == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("action");
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-04 00:12:01 +00:00
|
|
|
|
var gcHandle = new GCHandle();
|
2009-07-26 23:22:00 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
gcHandle = GCHandle.Alloc(value, GCHandleType.Pinned);
|
|
|
|
|
action(gcHandle.AddrOfPinnedObject());
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (gcHandle.IsAllocated)
|
|
|
|
|
{
|
|
|
|
|
gcHandle.Free();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|