When working with double pointers ( ** ), the best way is to simply marshal them as IntPtr .
public static extern int myFunction(IntPtr foo, IntPtr bar, IntPtr baz);
Then digging into a double pointer in managed code is performed.
IntPtr foo, bar baz; ... myFunction(foo, bar, baz); IntPtr oneDeep = (IntPtr)Marshal.PtrToStructure(foo, typeof(IntPtr)); int value = (int)Marshal.PtrToStructure(oneDeep, typeof(int));
The above code is obviously a bit ... ugly. I prefer to wrap it with nice extension methods on IntPtr .
public static class Extensions { public static T Deref<T>(this IntPtr ptr) { return (T)Marshal.PtrToStructure(ptr, typeof(T)); } }
Then the above can be rewritten as more readable
int value = ptr.Deref<IntPtr>().Deref<int>();
source share