/// <summary>
 /// Creates a new instance of DestinationInfo at the specific memory address.
 /// </summary>
 /// <param name="baseAddr">Memory address where the DestinationInfo object should be created.</param>
 public DestinationInfo(IntPtr baseAddr)
 {
     lock (syncRoot)
     {
         guid        = new Guid(MarshalEx.ReadByteArray(baseAddr, 0, 16));
         description = MarshalEx.PtrToStringUni(baseAddr, 16, 256);
     }
 }
Example #2
0
        /// <summary>
        /// Retrieves the data that is currently on the system clipboard.
        /// </summary>
        /// <returns>An <see cref="IDataObject"/> that represents the data currently on the clipboard, or null if there is no data on the clipboard.</returns>
        public static IDataObject GetDataObject()
        {
            //data object to return
            DataObject dobj = new DataObject();

            if (!OpenClipboard(IntPtr.Zero))
            {
                throw new System.Runtime.InteropServices.ExternalException("Could not open Clipboard");
            }

            //get number of clipboard types available
            int cfcount = CountClipboardFormats();

            if (cfcount == 0)
            {
                //throw new System.Runtime.InteropServices.ExternalException("No data on the Clipboard");
                return(null);
            }
            else
            {
                ClipboardFormats format = 0;

                DataFormats.Format df;

                for (int i = 0; i < cfcount; i++)
                {
                    //get the next format on the clipboard
                    format = EnumClipboardFormats(format);

                    try
                    {
                        //get the dotnet data format
                        df = DataFormats.GetFormat((int)format);
                    }
                    catch
                    {
                        //if format is not recognised ignore it
                        break;
                    }

                    switch ((ClipboardFormats)df.Id)
                    {
                    case ClipboardFormats.Bitmap:
                        //device independant bitmap
                        IntPtr           hDib = GetClipboardData((ClipboardFormats)df.Id);
                        BitmapInfoHeader bih  = new BitmapInfoHeader();

                        //marshal thw bitmap information header
                        for (int iByte = 0; iByte < bih.Data.Length; iByte++)
                        {
                            bih.Data[iByte] = Marshal.ReadByte(hDib, iByte);
                        }

                        byte [] buf = MarshalEx.ReadByteArray(hDib, 0, (Math.Abs(bih.Width * bih.Height) * (bih.BitCount >> 3)) + bih.Size);

                        MemoryStream strm = new MemoryStream(buf.Length + 14);
                        BinaryWriter wr   = new BinaryWriter(strm);
                        wr.Write((short)0x4d42);                                  // BM
                        wr.Write((int)(buf.Length + 14));
                        wr.Write(0);
                        wr.Write((int)(0x36 + (BitConverter.ToInt32(buf, 0x20) * 4)));
                        wr.Write(buf);
                        wr.Flush();
                        strm.Seek((long)0, SeekOrigin.Begin);

                        dobj.SetData(df.Name, new System.Drawing.Bitmap(strm));
                        break;

                    case ClipboardFormats.UnicodeText:
                        //unicode text formats
                        dobj.SetData(df.Name, Marshal.PtrToStringUni(GetClipboardData((ClipboardFormats)df.Id)));
                        break;
                    }
                }
            }

            if (!CloseClipboard())
            {
                throw new System.Runtime.InteropServices.ExternalException("Could not close Clipboard");
            }

            return(dobj);
        }