예제 #1
0
        /// <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 (!RdMarshal.IsComObject(comObj))
            {
                throw new ArgumentException("Expected a COM object");
            }

            var referencesPtr = RdMarshal.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);

            RdMarshal.Release(referencesPtr);
            return(retVal);
        }