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.");
            }
        }
Beispiel #2
0
        IReadOnlyCollection <string> GetFilesCopiedFromRawData(byte[] data)
        {
            var files = new List <string>();

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

            return(files);
        }
        public IntPtr GetDataPointer(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);

                return(globalPointer);
            }
        }