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

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(); … Read more

Just what is an IntPtr exactly?

It’s a “native (platform-specific) size integer.” It’s internally represented as void* but exposed as an integer. You can use it whenever you need to store an unmanaged pointer and don’t want to use unsafe code. IntPtr.Zero is effectively NULL (a null pointer).