Exemplo n.º 1
0
        /// <summary>
        /// Writes the ConnectionInfo data to unmanaged memory.
        /// </summary>
        /// <returns>A pointer to the unmanaged memory block storing the ConnectionInfo data</returns>
        public IntPtr ToPtr()
        {
            IntPtr p = MarshalEx.AllocHLocal((uint)Marshal.SizeOf(typeof(ConnectionInfo)));

            MarshalEx.WriteInt32(p, 0, this.cbSize);
            MarshalEx.WriteInt32(p, 4, this.dwParams);
            MarshalEx.WriteInt32(p, 8, this.dwFlags);
            MarshalEx.WriteInt32(p, 12, this.dwPriority);
            MarshalEx.WriteBool(p, 16, this.bExclusive);
            MarshalEx.WriteBool(p, 20, this.bDisabled);
            MarshalEx.WriteByteArray(p, 24, this.guidDestNet.ToByteArray());
            MarshalEx.WriteIntPtr(p, 40, this.hWnd);
            MarshalEx.WriteInt32(p, 44, this.uMsg);
            MarshalEx.WriteInt32(p, 48, this.lParam);
            MarshalEx.WriteInt32(p, 52, this.ulMaxCost);
            MarshalEx.WriteInt32(p, 56, this.ulMinRcvBw);
            MarshalEx.WriteInt32(p, 60, this.ulMaxConnLatency);

            handle = p;
            return(p);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Places nonpersistent data on the system clipboard.
        /// </summary>
        /// <param name="data">The data to place on the clipboard.</param>
        /// <exception cref="System.ArgumentNullException">The value of data is null.</exception>
        public static void SetDataObject(IDataObject data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("The value of data is null");
            }
            else
            {
                if (!OpenClipboard(IntPtr.Zero))
                {
                    throw new System.Runtime.InteropServices.ExternalException("Could not open Clipboard");
                }

                if (!EmptyClipboard())
                {
                    throw new System.Runtime.InteropServices.ExternalException("Unable to empty Clipboard");
                }

                //from here on we are only supporting unicode text, but it would be possible to support other formats
                if (data.GetDataPresent(DataFormats.UnicodeText))
                {
                    string unicodedata;
                    IntPtr hClipboard;

                    try
                    {
                        //extract unicode string from supplied data
                        unicodedata = data.GetData(DataFormats.UnicodeText).ToString();
                    }
                    catch
                    {
                        throw new FormatException("Clipboard data not in a recognised format");
                    }

                    //marshall the string
                    hClipboard = MarshalEx.StringToHGlobalUni(unicodedata);

                    //pass data to clipboard
                    if (SetClipboardData(ClipboardFormats.UnicodeText, hClipboard) == IntPtr.Zero)
                    {
                        throw new System.Runtime.InteropServices.ExternalException("Could not put data on Clipboard");
                    }
                }
                else if (data.GetDataPresent(DataFormats.Bitmap))
                {
                    System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)data.GetData(DataFormats.Bitmap);

                    int bs = bmp.Width * 3 * bmp.Height;
                    //allocate unmanaged memory
                    IntPtr hClipboard = MarshalEx.AllocHGlobal(bs + 41);
                    byte[] buffer     = new byte[40];

                    MemoryStream strm = new MemoryStream(buffer);
                    BinaryWriter wr   = new BinaryWriter(strm);
                    wr.Write(40);
                    wr.Write(bmp.Width);
                    wr.Write(bmp.Height);
                    wr.Write((short)1);
                    wr.Write((short)0x18);
                    // remaining bytes will be 0
                    wr.Close();

                    MarshalEx.WriteByteArray(hClipboard, 0, buffer);
                    int pos = 40;
                    for (int r = bmp.Height - 1; r >= 0; r--)
                    {
                        for (int c = 0; c < bmp.Width; c++)
                        {
                            int color = bmp.GetPixel(c, r).ToArgb();

                            byte red   = (byte)((color & 0x00ff0000) >> 16);
                            byte green = (byte)((color & 0x0000ff00) >> 8);
                            byte blue  = (byte)(color & 0x000000ff);

                            MarshalEx.WriteByte(hClipboard, pos++, blue);
                            MarshalEx.WriteByte(hClipboard, pos++, green);
                            MarshalEx.WriteByte(hClipboard, pos++, red);
                        }
                    }


                    //pass data to clipboard
                    if (SetClipboardData(ClipboardFormats.Bitmap, hClipboard) == IntPtr.Zero)
                    {
                        throw new System.Runtime.InteropServices.ExternalException("Could not put data on Clipboard");
                    }
                }

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