public ProgressUpdateWrapper(IProgressUpdateInterface wrappedInterface) { if (wrappedInterface != null) { _wrappedInterfacePtr = Marshal.GetIUnknownForObjectInContext(wrappedInterface); } }
static void Run() { // Create an int object int obj = 1; Console.WriteLine("Calling Marshal.GetIUnknownForObjectInContext..."); // Get the IUnKnown pointer for the Integer object IntPtr pointer = Marshal.GetIUnknownForObjectInContext(obj); Console.WriteLine("Calling Marshal.Release..."); // Always call Marshal.Release to decrement the reference count. Marshal.Release(pointer); }
/// <summary> /// Takes a COM object, and reads the unmanaged memory given by its pointer, allowing us to read internal fields /// </summary> /// <typeparam name="T">the type of structure to return</typeparam> /// <param name="comObj">the COM object</param> /// <returns>the requested structure T</returns> public static T ReadComObjectStructure <T>(object comObj) { // Reads a COM object as a structure to copy its internal fields if (!Marshal.IsComObject(comObj)) { throw new ArgumentException("Expected a COM object"); } var referencesPtr = Marshal.GetIUnknownForObjectInContext(comObj); if (referencesPtr == IntPtr.Zero) { throw new InvalidOperationException("Cannot access the TypeLib API from this thread. TypeLib API must be accessed from the main thread."); } var retVal = ReadStructureSafe <T>(referencesPtr); Marshal.Release(referencesPtr); return(retVal); }
public void DumpGraphInfo(string fileName, IGraphBuilder graph) { if (string.IsNullOrEmpty(fileName) || graph == null) { return; } string filterOutputString = string.Empty; IEnumFilters enumFilters; int hr = graph.EnumFilters(out enumFilters); DsError.ThrowExceptionForHR(hr); IntPtr fetched = IntPtr.Zero; IBaseFilter[] filters = { null }; int r = 0; while (r == 0) { try { r = enumFilters.Next(filters.Length, filters, fetched); DsError.ThrowExceptionForHR(r); if (filters == null || filters.Length == 0 || filters[0] == null) { continue; } FilterInfo filterInfo; filters[0].QueryFilterInfo(out filterInfo); filterOutputString += string.Format("{0:X8}", Marshal.GetIUnknownForObjectInContext(filters[0]).ToInt32()) + " "; filterOutputString += filterInfo.achName + Environment.NewLine; /* We will want to enum all the pins on the source filter */ IEnumPins pinEnum; hr = filters[0].EnumPins(out pinEnum); DsError.ThrowExceptionForHR(hr); IntPtr fetched2 = IntPtr.Zero; IPin[] pins = { null }; while (pinEnum.Next(pins.Length, pins, fetched2) == 0) { PinInfo pinInfo; pins[0].QueryPinInfo(out pinInfo); var prefix = "[In ] "; if (pinInfo.dir == PinDirection.Output) { prefix = "[Out] "; } filterOutputString += string.Format("{0:X8}", Marshal.GetIUnknownForObjectInContext(pins[0]).ToInt32()) + " "; filterOutputString += prefix + pinInfo.name + Environment.NewLine; Marshal.ReleaseComObject(pins[0]); } Marshal.ReleaseComObject(pinEnum); } catch { r = 0; continue; } } Marshal.ReleaseComObject(enumFilters); var file2 = System.IO.File.CreateText(fileName); file2.AutoFlush = true; file2.Write(filterOutputString); file2.Close(); }