C# - How To Convert Object To IntPtr And Back?

C#WinapiCallbackMarshallingIntptr

C# Problem Overview


I want to pass an object from managed code to a WinApi function as IntPtr. It will pass this object back to my callback function in managed code as IntPtr. It's not a structure, it's an instance of a class.

How do I convert object to IntPtr and back ?

C# Solutions


Solution 1 - C#

So if I want to pass a list to my callback function through WinApi I use 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");

Thx to David Heffernan

Edit: As noted in the comments, you need to free the handle after use. Also I used casting. It might be wise to use the static methods GCHandle.ToIntPtr(handle1) and GCHandle.FromIntPtr(parameter) like here. I haven't verified that.

Solution 2 - C#

While the accepted answer is correct, I wanted to add a little bit to it.

I have grown fond of creating extensions for this so it reads: 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);
    }
}

Also, depending on how much of this you're doing, it might be nice to contain your list in an 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();
    }
}

And then you might consume it like this:

using (var handleProvider = new GCHandleProvider(myList))
{
    var b = EnumChildWindows(hwndParent, CallBack, handleProvider.Pointer);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBitterblueView Question on Stackoverflow
Solution 1 - C#BitterblueView Answer on Stackoverflow
Solution 2 - C#Josh GustView Answer on Stackoverflow