unsafe uint Win32Com.IDataObject.QueryGetData(FORMATETC *format)
        {
            if (_wrapped is Win32Com.IDataObject ole)
            {
                return(ole.QueryGetData(format));
            }

            if (format->dwAspect != DVASPECT.DVASPECT_CONTENT)
            {
                return(DV_E_DVASPECT);
            }
            if (!format->tymed.HasAllFlags(TYMED.TYMED_HGLOBAL))
            {
                return(DV_E_TYMED);
            }

            var dataFormat = ClipboardFormats.GetFormat(format->cfFormat);

            if (string.IsNullOrEmpty(dataFormat) || !_wrapped.Contains(dataFormat))
            {
                return(DV_E_FORMATETC);
            }

            return(0);
        }
Esempio n. 2
0
        void IOleDataObject.GetDataHere(ref FORMATETC format, ref STGMEDIUM medium)
        {
            if (_wrapped is IOleDataObject ole)
            {
                ole.GetDataHere(ref format, ref medium);
                return;
            }

            if (medium.tymed != TYMED.TYMED_HGLOBAL || !format.tymed.HasFlag(TYMED.TYMED_HGLOBAL))
            {
                Marshal.ThrowExceptionForHR(DV_E_TYMED);
            }

            if (format.dwAspect != DVASPECT.DVASPECT_CONTENT)
            {
                Marshal.ThrowExceptionForHR(DV_E_DVASPECT);
            }

            string fmt = ClipboardFormats.GetFormat(format.cfFormat);

            if (string.IsNullOrEmpty(fmt) || !_wrapped.Contains(fmt))
            {
                Marshal.ThrowExceptionForHR(DV_E_FORMATETC);
            }

            if (medium.unionmember == IntPtr.Zero)
            {
                Marshal.ThrowExceptionForHR(STG_E_MEDIUMFULL);
            }

            int result = WriteDataToHGlobal(fmt, ref medium.unionmember);

            Marshal.ThrowExceptionForHR(result);
        }
        unsafe uint Win32Com.IDataObject.GetDataHere(FORMATETC *format, Interop.STGMEDIUM *medium)
        {
            if (_wrapped is Win32Com.IDataObject ole)
            {
                return(ole.GetDataHere(format, medium));
            }

            if (medium->tymed != TYMED.TYMED_HGLOBAL || !format->tymed.HasAllFlags(TYMED.TYMED_HGLOBAL))
            {
                return(DV_E_TYMED);
            }

            if (format->dwAspect != DVASPECT.DVASPECT_CONTENT)
            {
                return(DV_E_DVASPECT);
            }

            string fmt = ClipboardFormats.GetFormat(format->cfFormat);

            if (string.IsNullOrEmpty(fmt) || !_wrapped.Contains(fmt))
            {
                return(DV_E_FORMATETC);
            }

            if (medium->unionmember == IntPtr.Zero)
            {
                return(STG_E_MEDIUMFULL);
            }

            return(WriteDataToHGlobal(fmt, ref medium->unionmember));
        }
Esempio n. 4
0
        void IOleDataObject.GetData(ref FORMATETC format, out STGMEDIUM medium)
        {
            if (_wrapped is IOleDataObject ole)
            {
                ole.GetData(ref format, out medium);
                return;
            }
            if (!format.tymed.HasAllFlags(TYMED.TYMED_HGLOBAL))
            {
                Marshal.ThrowExceptionForHR(DV_E_TYMED);
            }

            if (format.dwAspect != DVASPECT.DVASPECT_CONTENT)
            {
                Marshal.ThrowExceptionForHR(DV_E_DVASPECT);
            }

            string fmt = ClipboardFormats.GetFormat(format.cfFormat);

            if (string.IsNullOrEmpty(fmt) || !_wrapped.Contains(fmt))
            {
                Marshal.ThrowExceptionForHR(DV_E_FORMATETC);
            }

            medium       = default(STGMEDIUM);
            medium.tymed = TYMED.TYMED_HGLOBAL;
            int result = WriteDataToHGlobal(fmt, ref medium.unionmember);

            Marshal.ThrowExceptionForHR(result);
        }
Esempio n. 5
0
            private FORMATETC ConvertToFormatEtc(string aFormatName)
            {
                FORMATETC result = default(FORMATETC);

                result.cfFormat = ClipboardFormats.GetFormat(aFormatName);
                result.dwAspect = DVASPECT.DVASPECT_CONTENT;
                result.ptd      = IntPtr.Zero;
                result.lindex   = -1;
                result.tymed    = TYMED.TYMED_HGLOBAL;
                return(result);
            }
        private unsafe object GetDataFromOleHGLOBAL(string format, DVASPECT aspect)
        {
            var formatEtc = new Interop.FORMATETC();

            formatEtc.cfFormat = ClipboardFormats.GetFormat(format);
            formatEtc.dwAspect = aspect;
            formatEtc.lindex   = -1;
            formatEtc.tymed    = TYMED.TYMED_HGLOBAL;
            if (_wrapped.QueryGetData(&formatEtc) == 0)
            {
                Interop.STGMEDIUM medium = default;
                _ = _wrapped.GetData(&formatEtc, &medium);
                try
                {
                    if (medium.unionmember != IntPtr.Zero && medium.tymed == TYMED.TYMED_HGLOBAL)
                    {
                        if (format == DataFormats.Text)
                        {
                            return(ReadStringFromHGlobal(medium.unionmember));
                        }
                        if (format == DataFormats.FileNames)
                        {
                            return(ReadFileNamesFromHGlobal(medium.unionmember));
                        }

                        byte[] data = ReadBytesFromHGlobal(medium.unionmember);

                        if (IsSerializedObject(data))
                        {
                            using (var ms = new MemoryStream(data))
                            {
                                ms.Position = DataObject.SerializedObjectGUID.Length;
                                BinaryFormatter binaryFormatter = new BinaryFormatter();
                                return(binaryFormatter.Deserialize(ms));
                            }
                        }
                        return(data);
                    }
                }
                finally
                {
                    UnmanagedMethods.ReleaseStgMedium(ref medium);
                }
            }
            return(null);
        }
        private unsafe IEnumerable <string> GetDataFormatsCore()
        {
            var formatsList = new List <string>();
            var enumFormat  = _wrapped.EnumFormatEtc((int)DATADIR.DATADIR_GET);

            if (enumFormat != null)
            {
                enumFormat.Reset();

                var formats = ArrayPool <Interop.FORMATETC> .Shared.Rent(1);

                try
                {
                    uint fetched = 0;
                    do
                    {
                        fixed(Interop.FORMATETC *formatsPtr = formats)
                        {
                            // Read one item at a time.
                            // When "celt" parameter is 1, "pceltFetched" is ignored.
                            var res = enumFormat.Next(1, formatsPtr, &fetched);

                            if (res != 0)
                            {
                                break;
                            }
                        }
                        if (fetched > 0)
                        {
                            if (formats[0].ptd != IntPtr.Zero)
                            {
                                Marshal.FreeCoTaskMem(formats[0].ptd);
                            }

                            formatsList.Add(ClipboardFormats.GetFormat(formats[0].cfFormat));
                        }
                    }while (fetched > 0);
                }
                finally
                {
                    ArrayPool <Interop.FORMATETC> .Shared.Return(formats);
                }
            }
            return(formatsList);
        }
Esempio n. 8
0
        int IOleDataObject.QueryGetData(ref FORMATETC format)
        {
            if (_wrapped is IOleDataObject ole)
            {
                return(ole.QueryGetData(ref format));
            }
            if (format.dwAspect != DVASPECT.DVASPECT_CONTENT)
            {
                return(DV_E_DVASPECT);
            }
            if (!format.tymed.HasFlag(TYMED.TYMED_HGLOBAL))
            {
                return(DV_E_TYMED);
            }

            string dataFormat = ClipboardFormats.GetFormat(format.cfFormat);

            if (!string.IsNullOrEmpty(dataFormat) && _wrapped.Contains(dataFormat))
            {
                return(unchecked ((int)UnmanagedMethods.HRESULT.S_OK));
            }
            return(DV_E_FORMATETC);
        }
Esempio n. 9
0
        private IEnumerable <string> GetDataFormatsCore()
        {
            var enumFormat = _wrapped.EnumFormatEtc(DATADIR.DATADIR_GET);

            if (enumFormat != null)
            {
                enumFormat.Reset();
                FORMATETC[] formats = new FORMATETC[1];
                int[]       fetched = { 1 };
                while (fetched[0] > 0)
                {
                    fetched[0] = 0;
                    if (enumFormat.Next(1, formats, fetched) == 0 && fetched[0] > 0)
                    {
                        if (formats[0].ptd != IntPtr.Zero)
                        {
                            Marshal.FreeCoTaskMem(formats[0].ptd);
                        }

                        yield return(ClipboardFormats.GetFormat(formats[0].cfFormat));
                    }
                }
            }
        }
Esempio n. 10
0
        private IEnumerable <string> GetDataFormatsCore()
        {
            var enumFormat = _wrapped.EnumFormatEtc(DATADIR.DATADIR_GET);

            if (enumFormat != null)
            {
                enumFormat.Reset();

                var formats = ArrayPool <FORMATETC> .Shared.Rent(1);

                var fetched = ArrayPool <int> .Shared.Rent(1);

                try
                {
                    do
                    {
                        fetched[0] = 0;
                        if (enumFormat.Next(1, formats, fetched) == 0 && fetched[0] > 0)
                        {
                            if (formats[0].ptd != IntPtr.Zero)
                            {
                                Marshal.FreeCoTaskMem(formats[0].ptd);
                            }

                            yield return(ClipboardFormats.GetFormat(formats[0].cfFormat));
                        }
                    }while (fetched[0] > 0);
                }
                finally
                {
                    ArrayPool <FORMATETC> .Shared.Return(formats);

                    ArrayPool <int> .Shared.Return(fetched);
                }
            }
        }