/// <summary> /// Given a <see cref="System.IntPtr"/> from a generated Vtable, convert to the target type. /// </summary> /// <typeparam name="T">Desired type.</typeparam> /// <param name="dispatchPtr">Pointer supplied to Vtable function entry.</param> /// <returns>Instance of type associated with dispatched function call.</returns> public static unsafe T GetInstance <T>(ComInterfaceDispatch *dispatchPtr) where T : class { // See the dispatch section in the runtime for details on the masking below. const long DispatchThisPtrMask = ~0xfL; var comInstance = *(ComInterfaceInstance **)(((long)dispatchPtr) & DispatchThisPtrMask); return(Unsafe.As <T>(GCHandle.InternalGet(comInstance->GcHandle))); }