public static UInt32 SetCustomContext <InteropHandle>( GetContextFunc <InteropHandle> getContextFunc, Func <InteropHandle, IntPtr, UInt32> setContextFunc, InteropHandle handle, Object customContext) { // Get the current context pointer and release on completion IntPtr contextPtrOld; UInt32 err = getContextFunc( handle, out contextPtrOld); if (PartyError.SUCCEEDED(err)) { var contextPtr = IntPtr.Zero; if (customContext != null) { contextPtr = GCHandle.ToIntPtr(GCHandle.Alloc(customContext)); } err = setContextFunc( handle, contextPtr); if (PartyError.SUCCEEDED(err)) { if (contextPtrOld != IntPtr.Zero) { GCHandle contextGcHandle = GCHandle.FromIntPtr(contextPtrOld); contextGcHandle.Free(); } } else { if (contextPtr != IntPtr.Zero) { GCHandle contextGcHandle = GCHandle.FromIntPtr(contextPtr); contextGcHandle.Free(); } } } return(err); }
public static UInt32 GetCustomContext <InteropHandle>( GetContextFunc <InteropHandle> getContextFunc, InteropHandle handle, out Object customContext) { customContext = null; IntPtr contextPtr; UInt32 err = getContextFunc(handle, out contextPtr); if (PartyError.SUCCEEDED(err)) { if (contextPtr != IntPtr.Zero) { GCHandle contextGcHandle = GCHandle.FromIntPtr(contextPtr); customContext = (Object)contextGcHandle.Target; } } return(err); }