Esempio n. 1
0
        public int QueryGetData(ref FORMATETC format)
        {
            try
            {
                log.DebugFormat("IDataObject.QueryGetData called -- cfFormat {0} dwAspect {1} lindex {2} ptd {3} tymed {4}", format.cfFormat, format.dwAspect, format.lindex, format.ptd, format.tymed);
                log.DebugFormat("Format name: {0}", System.Windows.Forms.DataFormats.GetFormat((ushort)format.cfFormat).Name);
                if (format.cfFormat == NativeMethods.CF_HDROP)
                {
                    //Validate index
                    if (format.lindex != -1)
                    {
                        return(NativeMethods.DV_E_LINDEX);
                    }
                    //Validate medium type
                    if (!format.tymed.HasFlag(TYMED.TYMED_HGLOBAL))
                    {
                        return(NativeMethods.DV_E_TYMED);
                    }
                    //Validate DV aspect
                    if (format.dwAspect != DVASPECT.DVASPECT_CONTENT)
                    {
                        return(NativeMethods.DV_E_DVASPECT);
                    }

                    log.DebugFormat("IDataObject.QueryGetData result: {0}", NativeMethods.S_OK);
                    return(NativeMethods.S_OK);
                }
                else if (format.cfFormat == NativeMethods.CF_TEXT || format.cfFormat == NativeMethods.CF_UNICODETEXT || format.cfFormat == (ushort)DataObjectHelper.GetClipboardFormat("Csv"))
                {
                    //Do not return text formats -- some applications try to get text before files
                    return(NativeMethods.DV_E_FORMATETC);
                }
                else
                {
                    int result = innerData.QueryGetData(format);
                    log.DebugFormat("Result: {0}", result);
                    return(result);
                }
            }
            catch (Exception ex)
            {
                log.Error("Exception in IDataObject.QueryGetData", ex);
                return(NativeMethods.E_UNEXPECTED);
            }
        }
Esempio n. 2
0
        public int EnumFormatEtc(DATADIR direction, out IEnumFORMATETC ppenumFormatEtc)
        {
            IEnumFORMATETC origEnum = null;

            try
            {
                log.DebugFormat("IDataObject.EnumFormatEtc called -- direction {0}", direction);
                switch (direction)
                {
                case DATADIR.DATADIR_GET:
                    //Get original enumerator
                    int result = innerData.EnumFormatEtc(direction, out origEnum);
                    if (result != NativeMethods.S_OK)
                    {
                        ppenumFormatEtc = null;
                        return(result);
                    }

                    //Enumerate original formats
                    List <FORMATETC> formats = new List <FORMATETC>();
                    FORMATETC[]      buffer  = new FORMATETC[] { new FORMATETC() };
                    while (origEnum.Next(1, buffer, null) == NativeMethods.S_OK)
                    {
                        //Convert format from short to unsigned short
                        ushort cfFormat = (ushort)buffer[0].cfFormat;

                        //Do not return text formats -- some applications try to get text before files
                        if (cfFormat != NativeMethods.CF_TEXT && cfFormat != NativeMethods.CF_UNICODETEXT && cfFormat != (ushort)DataObjectHelper.GetClipboardFormat("Csv"))
                        {
                            formats.Add(buffer[0]);
                        }
                    }

                    //Add CF_HDROP format
                    FORMATETC format = new FORMATETC();
                    format.cfFormat = NativeMethods.CF_HDROP;
                    format.dwAspect = DVASPECT.DVASPECT_CONTENT;
                    format.lindex   = -1;
                    format.ptd      = IntPtr.Zero;
                    format.tymed    = TYMED.TYMED_HGLOBAL;
                    formats.Add(format);

                    //Return new enumerator for available formats
                    ppenumFormatEtc = new FormatEtcEnumerator(formats.ToArray());
                    return(NativeMethods.S_OK);

                case DATADIR.DATADIR_SET:
                    //Return original enumerator
                    return(innerData.EnumFormatEtc(direction, out ppenumFormatEtc));

                default:
                    //Invalid direction
                    ppenumFormatEtc = null;
                    return(NativeMethods.E_INVALIDARG);
                }
            }
            catch (Exception ex)
            {
                log.Error("Exception in IDataObject.EnumFormatEtc", ex);
                ppenumFormatEtc = null;
                return(NativeMethods.E_UNEXPECTED);
            }
            finally
            {
                //Release all unmanaged objects
                if (origEnum != null)
                {
                    Marshal.ReleaseComObject(origEnum);
                }
            }
        }
Esempio n. 3
0
        public int GetData(ref FORMATETC format, out STGMEDIUM medium)
        {
            try
            {
                //Get data into passed medium
                log.DebugFormat("IDataObject.GetData called -- cfFormat {0} dwAspect {1} lindex {2} ptd {3} tymed {4}", format.cfFormat, format.dwAspect, format.lindex, format.ptd, format.tymed);
                log.DebugFormat("Format name: {0}", System.Windows.Forms.DataFormats.GetFormat((ushort)format.cfFormat).Name);

                if (format.cfFormat == NativeMethods.CF_HDROP)
                {
                    medium = new STGMEDIUM();

                    //Validate index
                    if (format.lindex != -1)
                    {
                        return(NativeMethods.DV_E_LINDEX);
                    }
                    //Validate medium type
                    if (!format.tymed.HasFlag(TYMED.TYMED_HGLOBAL))
                    {
                        return(NativeMethods.DV_E_TYMED);
                    }
                    //Validate DV aspect
                    if (format.dwAspect != DVASPECT.DVASPECT_CONTENT)
                    {
                        return(NativeMethods.DV_E_DVASPECT);
                    }

                    //Extract files if not already extracted
                    if (tempFilenames == null)
                    {
                        ExtractFiles();
                    }

                    //Get list of dropped files
                    log.Debug("Setting drop files");
                    DataObjectHelper.SetDropFiles(ref medium, tempFilenames);
                    FilesDropped = true;
                    return(NativeMethods.S_OK);
                }
                else if (format.cfFormat == NativeMethods.CF_TEXT || format.cfFormat == NativeMethods.CF_UNICODETEXT || format.cfFormat == (ushort)DataObjectHelper.GetClipboardFormat("Csv"))
                {
                    //Do not return text formats -- some applications try to get text before files
                    medium = new STGMEDIUM();
                    return(NativeMethods.DV_E_FORMATETC);
                }
                else
                {
                    int result = innerData.GetData(format, out medium);
                    log.DebugFormat("Result: {0}", result);
                    return(result);
                }
            }
            catch (Exception ex)
            {
                log.Error("Exception in IDataObject.GetData", ex);
                medium = new STGMEDIUM();
                return(NativeMethods.E_UNEXPECTED);
            }
        }