After reading quite a bit on the clipboard being blocked when trying to use it, I tried using OpenClipboard()
directly, to capture the clipboard and be able to use it from my window.
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
[DllImport("user32.dll", SetLastError=true)]
private static extern bool OpenClipboard(IntPtr hWndNewOwner);
[DllImport("user32.dll", SetLastError=true)]
private static extern bool CloseClipboard();
private int idx = 0;
private void refresh_Tick(object sender, EventArgs e) {
switch (idx++) {
case 0:
OpenClipboard(Handle);
break;
default:
Clipboard.SetText(" ");
break;
}
}
}
When using SetText
, I will get the infamous error:
A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Windows.Forms.dll
Additional information: Requested Clipboard operation did not succeed.
So, the questions:
Does
OpenClipboard()
actually work?What is the argument for
OpenClipboard()
for? The rest of the API does not need a handle to any window, so why doesOpenClipboard()
? In other words, the Clipboard is supposed to be shared between PROCESSES, not WINDOWS - but I don't see a way to lock it for my current process.I can call
OpenClipboard(IntPtr.Zero)
, which MSDN says:If this parameter is NULL, the open clipboard is associated with the current task.
What is 'task' supposed to mean?