Esempio n. 1
0
 public void Uninstall()
 {
     if (!ClipboardApi.RemoveClipboardFormatListener(windowHandle))
     {
         throw new InvalidOperationException("Could not uninstall a clipboard hook for the main window.");
     }
 }
Esempio n. 2
0
 public void Install(IntPtr windowHandle)
 {
     this.windowHandle = windowHandle;
     if (!ClipboardApi.AddClipboardFormatListener(windowHandle))
     {
         throw GenerateInstallFailureException();
     }
 }
Esempio n. 3
0
        static Exception GenerateInstallFailureException()
        {
            var errorCode = Marshal.GetLastWin32Error();

            var existingOwner = ClipboardApi.GetClipboardOwner();
            var ownerTitle    = WindowApi.GetWindowTitle(existingOwner);

            return(new InvalidOperationException($"Could not install a clipboard hook for the main window. The window '{ownerTitle}' currently owns the clipboard. The last error code was {errorCode}."));
        }
Esempio n. 4
0
        /// <summary>
        /// Destroy all mouse event
        /// </summary>
        private void DestroyKeyboardMouseEvent()
        {
            if (_nextClipboardViewer != null)
            {
                // Set clipboard event to default
                ClipboardApi.ChangeClipboardChain(this.Handle, _nextClipboardViewer);
            }

            _jKeyboardMouseEvents.MouseDoubleClick  -= MouseTranslateEvent;
            _jKeyboardMouseEvents.MouseDragFinished -= MouseTranslateEvent;
            _jKeyboardMouseEvents.Dispose();
        }
Esempio n. 5
0
        public void InjectData(IClipboardDataPackage package)
        {
            clipboardCopyInterceptor.SkipNext();

            using (clipboardHandleFactory.StartNewSession())
            {
                ClipboardApi.EmptyClipboard();
                InjectPackageContents(package);
            }

            logger.Information("Clipboard package has been injected to the clipboard.", 1);
        }
Esempio n. 6
0
        IEnumerable <string> GetFilesCopiedFromRawData(byte[] data)
        {
            var files = new List <string>();

            using (var memoryHandle = memoryHandleFactory.AllocateInMemory(data))
            {
                var count = ClipboardApi.DragQueryFile(memoryHandle.Pointer, 0xFFFFFFFF, null, 0);
                FetchFilesFromMemory(files, memoryHandle, count);
            }

            return(files);
        }
Esempio n. 7
0
        void HandleClipboardUpdateWindowMessage()
        {
            var clipboardItemIdentifier = ClipboardApi.GetClipboardSequenceNumber();

            logger.Information($"Clipboard update message received with sequence #{clipboardItemIdentifier}.", 1);

            if (clipboardItemIdentifier != lastClipboardItemIdentifier)
            {
                lastClipboardItemIdentifier = clipboardItemIdentifier;

                TriggerDataCopiedEvent();
            }
        }
Esempio n. 8
0
        static void FetchFilesFromMemory(List <string> files, IMemoryHandle memoryHandle, int count)
        {
            for (var i = 0u; i < count; i++)
            {
                var length          = ClipboardApi.DragQueryFile(memoryHandle.Pointer, i, null, 0);
                var filenameBuilder = new StringBuilder(length);

                length = ClipboardApi.DragQueryFile(memoryHandle.Pointer, i, filenameBuilder, length + 1);

                var fileName = filenameBuilder.ToString();
                files.Add(fileName);
            }
        }
Esempio n. 9
0
        private void InitFormEvent()
        {
            _jKeyboardMouseEvents = Hook.GlobalEvents();

            // Key event
            _jKeyboardMouseEvents.KeyDown += TranslateKeyDown;

            // Mouse event
            _jKeyboardMouseEvents.MouseDragFinished += MouseTranslateEvent;
            _jKeyboardMouseEvents.MouseDoubleClick  += MouseTranslateEvent;

            // ClipboardEvent
            _nextClipboardViewer = (IntPtr)ClipboardApi.SetClipboardViewer((int)this.Handle);
        }
Esempio n. 10
0
 public IClipboardDataControlPackage Create()
 {
     using (clipboardSessionFactory.StartNewSession())
     {
         var formats = ClipboardApi.GetClipboardFormats();
         if (IsAnyFormatSupported(formats))
         {
             return(ConstructPackage(formats));
         }
         else
         {
             return(null);
         }
     }
 }
Esempio n. 11
0
        public byte[] UnwrapStructure(uint format)
        {
            //TODO: it is very sad that we invoke System.Drawing here to get the job done. probably not very optimal.

            var bitmapVersionFivePointer = ClipboardApi.GetClipboardData(ClipboardApi.CF_DIBV5);
            var bitmapVersionFiveHeader  = (BITMAPV5HEADER)Marshal.PtrToStructure(bitmapVersionFivePointer, typeof(BITMAPV5HEADER));

            if (bitmapVersionFiveHeader.bV5Compression == BI_RGB)
            {
                var bitmapVersionOneBytes  = ClipboardApi.GetClipboardDataBytes(ClipboardApi.CF_DIB);
                var bitmapVersionOneHeader = GeneralApi.ByteArrayToStructure <BITMAPINFOHEADER>(bitmapVersionOneBytes);

                return(HandleBitmapVersionOne(bitmapVersionOneBytes, bitmapVersionOneHeader));
            }
            else
            {
                return(HandleBitmapVersionFive(bitmapVersionFivePointer, bitmapVersionFiveHeader));
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Empty the Clipboard and Restore to system clipboard data contained in a collection of ClipData objects
        /// </summary>
        /// <param name="clipData">The collection of ClipData containing data stored from clipboard</param>
        /// <returns></returns>
        public static bool SetClipboard(ReadOnlyCollection <ClipboardData> clipData)
        {
            //Open clipboard to allow its manipultaion
            if (!ClipboardApi.OpenClipboard(IntPtr.Zero))
            {
                return(false);
            }

            //Clear the clipboard
            EmptyClipboard();

            //Get an Enumerator to iterate into each ClipData contained into the collection
            IEnumerator <ClipboardData> cData = clipData.GetEnumerator();

            while (cData.MoveNext())
            {
                ClipboardData cd = cData.Current;

                //Get the pointer for inserting the buffer data into the clipboard
                IntPtr alloc = MemoryApi.GlobalAlloc(MemoryApi.GMEM_MOVEABLE | MemoryApi.GMEM_DDESHARE, cd.Size);
                IntPtr gLock = MemoryApi.GlobalLock(alloc);

                //Clopy the buffer of the ClipData into the clipboard
                if ((int)cd.Size > 0)
                {
                    Marshal.Copy(cd.Buffer, 0, gLock, cd.Buffer.GetLength(0));
                }
                else
                {
                }
                //Release pointers
                MemoryApi.GlobalUnlock(alloc);
                ClipboardApi.SetClipboardData(cd.Format, alloc);
            }
            ;
            //Close the clipboard to realese unused resources
            ClipboardApi.CloseClipboard();
            return(true);
        }
Esempio n. 13
0
        void InjectClipboardData(IClipboardData clipboardData)
        {
            using (var memoryHandle = memoryHandleFactory.AllocateInMemory(clipboardData.RawData))
            {
                var globalPointer = AllocateInMemory(clipboardData);

                var target = GeneralApi.GlobalLock(globalPointer);
                if (target == IntPtr.Zero)
                {
                    throw new InvalidOperationException("Could not allocate memory.");
                }

                GeneralApi.CopyMemory(target, memoryHandle.Pointer, (uint)clipboardData.RawData.Length);

                GeneralApi.GlobalUnlock(target);

                if (ClipboardApi.SetClipboardData(clipboardData.RawFormat, globalPointer) == IntPtr.Zero)
                {
                    GeneralApi.GlobalFree(globalPointer);
                    throw new Exception("Could not set clipboard data.");
                }
            }
        }
Esempio n. 14
0
 public void Dispose()
 {
     ClipboardApi.CloseClipboard();
 }
Esempio n. 15
0
 public byte[] UnwrapStructure(uint format)
 {
     return(ClipboardApi.GetClipboardDataBytes(format));
 }
Esempio n. 16
0
 public static bool EmptyClipboard()
 {
     return(ClipboardApi.EmptyClipboard());
 }
Esempio n. 17
0
        /// <summary>
        /// Convert to a DataClip collection all data present in the clipboard
        /// </summary>
        /// <returns></returns>
        public static ReadOnlyCollection <ClipboardData> GetClipboard()
        {
            //Init a list of ClipData, which will contain each Clipboard Data
            List <ClipboardData> clipData = new List <ClipboardData>();

            //Open Clipboard to allow us to read from it
            if (!ClipboardApi.OpenClipboard(IntPtr.Zero))
            {
                return(new ReadOnlyCollection <ClipboardData>(clipData));
            }

            //Loop for each clipboard data type
            uint format = 0;

            while ((format = ClipboardApi.EnumClipboardFormats(format)) != 0)
            {
                //Check if clipboard data type is recognized, and get its name
                string formatName = "0";
                if (format > 14)
                {
                    StringBuilder res = new StringBuilder();
                    if (ClipboardApi.GetClipboardFormatName(format, res, 100) > 0)
                    {
                        formatName = res.ToString();
                    }
                }
                //Get the pointer for the current Clipboard Data
                IntPtr pos = ClipboardApi.GetClipboardData(format);
                //Goto next if it's unreachable
                if (pos == IntPtr.Zero)
                {
                    continue;
                }
                //Get the clipboard buffer data properties
                UIntPtr lenght = MemoryApi.GlobalSize(pos);
                IntPtr  gLock  = MemoryApi.GlobalLock(pos);
                byte[]  buffer;
                if ((int)lenght > 0)
                {
                    //Init a buffer which will contain the clipboard data
                    buffer = new byte[(int)lenght];
                    int l = Convert.ToInt32(lenght.ToString());
                    //Copy data from clipboard to our byte[] buffer
                    Marshal.Copy(gLock, buffer, 0, l);
                }
                else
                {
                    buffer = new byte[0];
                }
                //Create a ClipData object that represtens current clipboard data
                var cd = new ClipboardData(format, formatName, buffer);
                cd.FormatName = formatName;
                //Add current Clipboard Data to the list


                clipData.Add(cd);
            }
            //Close the clipboard and realese unused resources
            ClipboardApi.CloseClipboard();
            //Returns the list of Clipboard Datas as a ReadOnlyCollection of ClipData
            return(new ReadOnlyCollection <ClipboardData>(clipData));
        }
Esempio n. 18
0
 public ClipboardHandle(
     IMainWindowHandleContainer mainWindow)
 {
     ClipboardApi.OpenClipboard(mainWindow.Handle);
 }