Ejemplo n.º 1
0
        void InjectClipboardData(
            IClipboardHandle session,
            IClipboardData clipboardData)
        {
            var wrappers = memoryWrappers
                           .Where(x => x.CanWrap(clipboardData))
                           .ToArray();

            if (wrappers.Length > 0)
            {
                logger.Verbose("Injecting {bytes} bytes of {format} format into the clipboard.", clipboardData.RawData.Length, clipboardData.RawFormat);
            }

            foreach (var wrapper in wrappers)
            {
                var success = session.SetClipboardData(
                    clipboardData.RawFormat.Number,
                    wrapper.GetDataPointer(
                        clipboardData));
                if (success == IntPtr.Zero)
                {
                    throw new Exception("Could not set clipboard data format " + clipboardData.RawFormat + ".");
                }
            }
        }
        void InjectClipboardData(
            IClipboardHandle session,
            IClipboardData clipboardData)
        {
            using (var memoryHandle = memoryHandleFactory.AllocateInMemory(clipboardData.RawData))
            {
                var globalPointer = AllocateInMemory(clipboardData);

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

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

                generalNativeApi.GlobalUnlock(target);

                if (session.SetClipboardData(clipboardData.RawFormat, globalPointer) !=
                    IntPtr.Zero)
                {
                    return;
                }

                generalNativeApi.GlobalFree(globalPointer);
                throw new Exception("Could not set clipboard data.");
            }
        }
        void InjectClipboardData(
            IClipboardHandle session,
            IClipboardData clipboardData)
        {
            using (var memoryHandle = memoryHandleFactory.AllocateInMemory(clipboardData.RawData))
            {
                var globalPointer = AllocateInMemory(clipboardData);

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

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

                generalNativeApi.GlobalUnlock(target);

                if (session.SetClipboardData(clipboardData.RawFormat, globalPointer) !=
                    IntPtr.Zero)
                {
                    return;
                }

                generalNativeApi.GlobalFree(globalPointer);
                throw new Exception("Could not set clipboard data.");
            }
        }
 void InjectPackageContents(
     IClipboardHandle session, IClipboardDataPackage package)
 {
     foreach (var clipboardData in package.Contents)
     {
         InjectClipboardData(session, clipboardData);
     }
 }
 void InjectPackageContents(
     IClipboardHandle session,
     IClipboardDataPackage package)
 {
     foreach (var clipboardData in package.Contents)
     {
         InjectClipboardData(session, clipboardData);
     }
 }
        void InjectClipboardData(
            IClipboardHandle session,
            IClipboardData clipboardData)
        {
            var wrappers = memoryWrappers
                           .Where(x => x.CanWrap(clipboardData))
                           .ToArray();

            if (wrappers.Length > 0)
            {
                logger.Verbose("Injecting {bytes} bytes of {format} format into the clipboard.", clipboardData.RawData.Length, clipboardData.RawFormat);
            }
            else
            {
                logger.Verbose("No suitable memory wrapper found for format {format}.", clipboardData.RawFormat);
            }

            foreach (var wrapper in wrappers)
            {
                var success = session.SetClipboardData(
                    clipboardData.RawFormat.Number,
                    wrapper.GetDataPointer(
                        clipboardData));
                if (success == IntPtr.Zero)
                {
                    throw new Exception(
                              "Could not set clipboard data format " + clipboardData.RawFormat + " from " + clipboardData.Package.Source.ProcessName + ".",
                              new Win32Exception(Marshal.GetLastWin32Error()));
                }

                var formats = session.GetClipboardFormats();
                if (formats.All(x => x.Number != clipboardData.RawFormat.Number))
                {
                    logger.Warning(
                        "The format {format} was not found in the clipboard after attempting injecting it to the clipboard.",
                        clipboardData.RawFormat);
                }
            }
        }