Example #1
0
        /// <summary>Gets the file content for the specified FileDescriptor index.</summary>
        /// <param name="index">The index of the file content to retrieve.</param>
        public static MemoryStream GetFileContent(this System.Windows.Forms.IDataObject data, int index)
        {
            // As this is indexed, "FileContent" is most likely null, so the COM IDataObject needs to be used
            var comData = (System.Runtime.InteropServices.ComTypes.IDataObject)data;

            var formatetc = new FORMATETC()
            {
                cfFormat = (short)DataFormats.GetFormat("FileContents").Id,
                dwAspect = DVASPECT.DVASPECT_CONTENT,
                lindex   = index,
                ptd      = IntPtr.Zero,
                tymed    = TYMED.TYMED_ISTREAM | TYMED.TYMED_HGLOBAL
            };

            var medium = new STGMEDIUM();

            comData.GetData(ref formatetc, out medium);

            switch (medium.tymed)
            {
            case TYMED.TYMED_HGLOBAL:
                return(data.GetFileContentFromHGlobal(medium));

            case TYMED.TYMED_ISTREAM:
                return(data.GetFileContentFromIStream(medium));

            default:
                throw new InvalidOperationException($"Cannot get FileContent for {medium.tymed} TYMED.");
            }
        }