我想将托管代码中的对象作为IntPtr
传递给WinApi函数。它会将此对象作为IntPtr
传递回托管代码中的回调函数。它不是一个结构,它是一个类的实例。
如何将object
转换为IntPtr
并返回?
因此,如果我想通过WinApi将列表传递给我的回调函数,我使用GCHandle
// object to IntPtr (before calling WinApi):
List<string> list1 = new List<string>();
GCHandle handle1 = GCHandle.Alloc(list1);
IntPtr parameter = (IntPtr) handle1;
// call WinAPi and pass the parameter here
// then free the handle when not needed:
handle1.Free();
// back to object (in callback function):
GCHandle handle2 = (GCHandle) parameter;
List<string> list2 = (handle2.Target as List<string>);
list2.Add("hello world");
编辑:如评论中所述,您需要在使用后释放句柄。我也用过铸造。使用像GCHandle.ToIntPtr(handle1)
这样的静态方法GCHandle.FromIntPtr(parameter)
和here可能是明智的。我还没有证实。
虽然接受的答案是正确的,但我想补充一点。
我喜欢为此创建扩展,所以它写道:list1.ToIntPtr()
。
public static class ObjectHandleExtensions
{
public static IntPtr ToIntPtr(this object target)
{
return GCHandle.Alloc(target).ToIntPtr();
}
public static GCHandle ToGcHandle(this object target)
{
return GCHandle.Alloc(target);
}
public static IntPtr ToIntPtr(this GCHandle target)
{
return GCHandle.ToIntPtr(target);
}
}
此外,根据你正在做多少,你可能很高兴在IDisposable
中包含你的列表。
public class GCHandleProvider : IDisposable
{
public GCHandleProvider(object target)
{
Handle = target.ToGcHandle();
}
public IntPtr Pointer => Handle.ToIntPtr();
public GCHandle Handle { get; }
private void ReleaseUnmanagedResources()
{
if (Handle.IsAllocated) Handle.Free();
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
~GCHandleProvider()
{
ReleaseUnmanagedResources();
}
}
然后你可能会像这样消耗它:
using (var handleProvider = new GCHandleProvider(myList))
{
var b = EnumChildWindows(hwndParent, CallBack, handleProvider.Pointer);
}