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."); } } }