Beispiel #1
0
        public InputshareDataObject(List <ClipboardVirtualFileData.FileAttributes> files)
        {
            objectType           = ClipboardDataType.File;
            fileDescriptorStream = GetFileDescriptor(files);
            foreach (var file in files)
            {
                //ISLogger.Write("Creating remote file stream for " + file.FileName);
                ManagedRemoteIStream str = new ManagedRemoteIStream(file);
                streams.Add(str);
            }

            operationFiles = files;

            byte[] a = BitConverter.GetBytes((int)DragDropEffects.Copy);

            IntPtr p = Marshal.AllocHGlobal(a.Length);

            Marshal.Copy(a, 0, p, a.Length);

            SetData(NativeMethods.CFSTR_FILEDESCRIPTORW, null);
            SetData(NativeMethods.CFSTR_FILECONTENTS, null);
            SetData(NativeMethods.CFSTR_PERFORMEDDROPEFFECT, null);
            SetData(NativeMethods.CFSTR_PREFERREDDROPEFFECT, IntPtr.Zero);
            SetData("InputshareFileData", "Inputshare object");
        }
Beispiel #2
0
        /// <summary>
        ///     Retrieves data from the clipboard in a specified format. The clipboard must have been opened previously.
        /// </summary>
        /// <remarks>
        ///     <p>
        ///         A connection must be made using <see cref="Open" /> method in the same thread.
        ///     </p>
        /// </remarks>
        /// <exception cref="ArgumentException">Throws if clipboard is closed.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     <paramref name="dataType" /> should be defined in
        ///     <seealso cref="ClipboardDataType" /> enum.
        /// </exception>
        /// <exception cref="ClipboardWindowsApiException">
        ///     <p>If the <c>GlobalSize</c> function of windows api for the size of the clipboard handle fails.</p>
        ///     <p>If the <c>GlobalLock</c> function of windows api for the size of the clipboard handle fails.</p>
        /// </exception>
        public byte[] GetData(ClipboardDataType dataType)
        {
            ThrowIfNotOpen();
            ThrowIfNotInRange(dataType);
            var format = (uint)dataType;

            var dataPtr = NativeMethods.GetClipboardData(format);

            if (dataPtr == IntPtr.Zero)
            {
                return(null);
            }

            var sizePtr = NativeMethods.GlobalSize(dataPtr);

            if (sizePtr == UIntPtr.Zero)
            {
                throw new ClipboardWindowsApiException(NativeMethods.GetLastError());
            }

            var lockedMemoryPtr = NativeMethods.GlobalLock(dataPtr);

            if (lockedMemoryPtr == IntPtr.Zero)
            {
                throw new ClipboardWindowsApiException(NativeMethods.GetLastError());
            }

            var buffer = new byte[sizePtr.ToUInt64()];

            Marshal.Copy(lockedMemoryPtr, buffer, 0, buffer.Length);

            NativeMethods.GlobalUnlock(dataPtr);

            return(buffer);
        }
 public ClipboardOperation(Guid operationId, ClipboardDataType dataType, ClipboardDataBase data, ISServerSocket host)
 {
     OperationId = operationId;
     DataType    = dataType;
     Data        = data;
     Host        = host;
 }
Beispiel #4
0
        /// <inheritdoc />
        /// <remarks>
        ///     <p>A connection must be made using <see cref="Open" /> method in the same thread.</p>
        ///     <p>Data might not be set before <see cref="Close" /> or <see cref="Dispose" /> methods are called.</p>
        /// </remarks>
        /// <exception cref="ArgumentNullException"><paramref name="data" /> is <see langword="null" />.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     <paramref name="dataType" /> should be defined in
        ///     <seealso cref="ClipboardDataType" /> enum.
        /// </exception>
        /// <exception cref="ArgumentException">Throws if clipboard is closed.</exception>
        public IClipboardOperationResult SetData(ClipboardDataType dataType, byte[] data)
        {
            ThrowIfNotOpen();
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            ThrowIfNotInRange(dataType);

            var result = TryCreateNativeBytes(data, out var nativeBytes);

            if (result != ClipboardOperationResultCode.Success)
            {
                return(new ClipboardOperationResult(result));
            }

            // Set clipboard data
            if (NativeMethods.SetClipboardData((uint)dataType, nativeBytes) == IntPtr.Zero)
            {
                NativeMethods.GlobalFree(nativeBytes);
                return(new ClipboardOperationResult(ClipboardOperationResultCode.ErrorSetClipboardData));
            }
            // ReSharper disable once RedundantAssignment
#pragma warning disable IDE0059        // Unnecessary assignment of a value
            nativeBytes = IntPtr.Zero; // SetClipboardData takes ownership of dataPtr upon success
#pragma warning restore IDE0059        // Unnecessary assignment of a value
            return(ClipboardOperationResult.SuccessResult);
        }
Beispiel #5
0
 /// <exception cref="ArgumentOutOfRangeException">
 ///     <paramref name="dataType" /> should be defined in
 ///     <seealso cref="ClipboardDataType" /> enum.
 /// </exception>
 private static void ThrowIfNotInRange(ClipboardDataType dataType)
 {
     if (!Enum.IsDefined(typeof(ClipboardDataType), dataType))
     {
         throw new ArgumentOutOfRangeException(nameof(dataType),
                                               $"Value must be defined in the {nameof(ClipboardDataType)} enum.");
     }
 }
Beispiel #6
0
        /// <summary>
        ///     Determines whether the content of the clipboard is <paramref name="dataType" />.
        /// </summary>
        /// <param name="dataType">Clipboard data format.</param>
        /// <returns><c>true</c> if content of the clipboard data is <paramref name="dataType" />, otherwise; <c>false</c>.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     <paramref name="dataType" /> should be defined in
        ///     <seealso cref="ClipboardDataType" /> enum.
        /// </exception>
        /// <seealso cref="ClipboardDataType" />
        public bool IsContentTypeOf(ClipboardDataType dataType)
        {
            ThrowIfNotInRange(dataType);
            var format = (uint)dataType;
            var result = NativeMethods.IsClipboardFormatAvailable(format);

            return(result);
        }
Beispiel #7
0
        /// <summary>
        ///     Places data on the clipboard in a specified clipboard format.
        /// </summary>
        /// <remarks>
        ///     <p>
        ///         A connection must be made using <see cref="Open" /> method in the same thread.
        ///     </p>
        ///     <p>
        ///         Data might not be set before <see cref="Close" /> or <see cref="Dispose" /> methods are called.
        ///     </p>
        /// </remarks>
        /// <exception cref="ArgumentNullException"><paramref name="data" /> is <see langword="null" />.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     <paramref name="dataType" /> should be defined in
        ///     <seealso cref="ClipboardDataType" /> enum.
        /// </exception>
        /// <exception cref="ArgumentException">Throws if clipboard is closed.</exception>
        public IClipboardOperationResult SetData(ClipboardDataType dataType, byte[] data)
        {
            ThrowIfNotOpen();
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            ThrowIfNotInRange(dataType);
            //prepare variable to retrieve clipboard data
            var sizePtr = new UIntPtr((uint)data.Length);

            var dataPtr = NativeMethods.GlobalAlloc(NativeMethods.GHND, sizePtr);

            if (dataPtr == IntPtr.Zero)
            {
                Debug.Assert(false);
                return(new ClipboardOperationResult(ClipboardOperationResultCode.ErrorGlobalAlloc));
            }

            Debug.Assert(NativeMethods.GlobalSize(dataPtr).ToUInt64() >= (ulong)data.Length); // Might be larger

            var lockedMemoryPtr = NativeMethods.GlobalLock(dataPtr);

            if (lockedMemoryPtr == IntPtr.Zero)
            {
                Debug.Assert(false);
                NativeMethods.GlobalFree(dataPtr);
                return(new ClipboardOperationResult(ClipboardOperationResultCode.ErrorGlobalLock));
            }

            Marshal.Copy(data, 0, lockedMemoryPtr, data.Length);
            NativeMethods.GlobalUnlock(dataPtr); // May return false on success
            //retrieve clipboard data
            var uFormat = (uint)dataType;

            if (NativeMethods.SetClipboardData(uFormat, dataPtr) == IntPtr.Zero)
            {
                Debug.Assert(false);
                NativeMethods.GlobalFree(dataPtr);
                return(new ClipboardOperationResult(ClipboardOperationResultCode.ErrorSetClipboardData));
            }
            // SetClipboardData takes ownership of dataPtr upon success.
            dataPtr = IntPtr.Zero;
            return(ClipboardOperationResult.SuccessResult);
        }
Beispiel #8
0
        /// <inheritdoc />
        /// <remarks>
        ///     <p>
        ///         A connection must be made using <see cref="Open" /> method in the same thread.
        ///     </p>
        /// </remarks>
        /// <exception cref="ArgumentException">Throws if clipboard is closed.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     <paramref name="dataType" /> should be defined in
        ///     <seealso cref="ClipboardDataType" /> enum.
        /// </exception>
        /// <exception cref="ClipboardWindowsApiException">
        ///     <p>If the <c>GlobalSize</c> function of windows api for the size of the clipboard handle fails.</p>
        ///     <p>If the <c>GlobalLock</c> function of windows api for the size of the clipboard handle fails.</p>
        /// </exception>
        public byte[] GetData(ClipboardDataType dataType)
        {
            ThrowIfNotOpen();
            ThrowIfNotInRange(dataType);

            var dataPtr = NativeMethods.GetClipboardData((uint)dataType);

            if (dataPtr == IntPtr.Zero)
            {
                return(new byte[0]);
            }

            var buffer = GetManagedBuffer(dataPtr);

            Marshal.Copy(dataPtr, buffer, 0, buffer.Length);

            return(buffer);
        }
Beispiel #9
0
        public static ClipboardDataBase FromBytes(byte[] data)
        {
            ClipboardDataType type = (ClipboardDataType)data[0];

            switch (type)
            {
            case ClipboardDataType.File:
                return(new ClipboardVirtualFileData(data));

            case ClipboardDataType.Image:
                return(new ClipboardImageData(data));

            case ClipboardDataType.Text:
                return(new ClipboardTextData(data));

            default:
                throw new ArgumentException("Invalid clipboard data");
            }
        }
Beispiel #10
0
        public InputshareDataObject(List <ClipboardVirtualFileData.FileAttributes> files)
        {
            objectType           = ClipboardDataType.File;
            fileDescriptorStream = GetFileDescriptor(files);
            foreach (var file in files)
            {
                //ISLogger.Write("Creating remote file stream for " + file.FileName);
                ManagedRemoteIStream str = new ManagedRemoteIStream(file);
                streams.Add(str);
            }

            operationFiles = files;



            SetData(NativeMethods.CFSTR_FILEDESCRIPTORW, null);
            SetData(NativeMethods.CFSTR_FILECONTENTS, null);
            SetData(NativeMethods.CFSTR_PERFORMEDDROPEFFECT, null);
            SetData("InputshareFileData", "Inputshare object");
        }
Beispiel #11
0
 /// <inheritdoc />
 public IClipboardOperationResult SetData(ClipboardDataType dataType, byte[] data) =>
 _session.SetData(dataType, data);
Beispiel #12
0
 /// <inheritdoc />
 public byte[] GetData(ClipboardDataType dataType)
 => _session.GetData(dataType);
Beispiel #13
0
 /// <inheritdoc />
 public bool IsContentTypeOf(ClipboardDataType dataType) => _session.IsContentTypeOf(dataType);
Beispiel #14
0
 public InputshareDataObject(Image image)
 {
     objectType = ClipboardDataType.Image;
     SetImage(image);
 }
Beispiel #15
0
 public InputshareDataObject(ClipboardTextData text)
 {
     objectType = ClipboardDataType.Text;
     SetData(DataFormats.Text, text.Text);
 }
Beispiel #16
0
 public ClipboardData(ClipboardDataType type, object value)
 {
     Type  = type;
     Value = value;
 }