5

I was wondering if there is anyway to lock and unlock the clipboard from C#. Basically, I'd be writing something into it and I do not want anyone else to write to it before I pick up my stuff.

How can I do this?

Breeze
  • 2,010
  • 2
  • 32
  • 43
kambamsu
  • 561
  • 2
  • 9
  • 21
  • 3
    That would be a bad idea, IMHO, and probably impossible. Perhaps if you gave more detail as to what you actually want to achieve, someone will have a better solution – pdr Jun 02 '10 at 10:15
  • 2
    As far as I know you cannot lock the Clipboard. It is meant to be a shared resource. – A G Jun 02 '10 at 10:20
  • 2
    It wouldn't be a bad idea, it would be terrible! It would make it really easy for the user to lose data and he wouldn't even know why, he'd be copying stuff and most of the time it would work, but sometimes not. – Hans Olsson Jun 02 '10 at 10:26
  • 3
    How to lock the clipboard is useful to know, particularly when you are testing that your code deals gracefully with other rogue applications that have locked the clipboard. – JDHnz Mar 01 '13 at 00:35

4 Answers4

5

The OpenClipboard() API function, followed by EmptyClipboard() locks the clipboard until CloseClipboard is called. You should probably pass a window handle of a window in the target process, but zero works.

Here's a C# console app that worked for me when I wanted to test that my application gracefully handled a rogue application locking the clipboard.

class Program
{
    [DllImport("user32.dll", EntryPoint = "OpenClipboard", SetLastError = true)]
    private static extern bool OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool CloseClipboard();

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool EmptyClipboard();

    static void Main(string[] args)
    {
        OpenClipboard(new IntPtr(0));
        EmptyClipboard();
        Console.WriteLine("Clipboard locked. Press enter to unlock and exit.");
        Console.ReadLine();
        CloseClipboard();
    }
}

These declarations came from pinvoke.net.

Robin Bennett
  • 3,192
  • 1
  • 8
  • 18
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
3

Do NOT do this. The clipboard is there for the convenience of the USER, not the PROGRAMMER! You will cause errors and crashes in other programs that are trying to (properly) use the clipboard, including programs that are monitoring for updates.

“Programs should not transfer data into our out of the clipboard without an explicit instruction from the user.”
— Charles Petzold, Programming Windows 3.1, Microsoft Press, 1992

Chris Thornton
  • 15,620
  • 5
  • 37
  • 62
1

As @shambulator says, this isn't what the clipboard is designed to do. Perhaps you should try a different tack. Integration with legacy systems can be tackled inmany ways (most of them bad, alas!).

The system to which you only can send keystrokes: is it a Windows application? Can you dig into its window structure to explicitly set texts inside text boxes by their HWNDs? Does the system have any kind of file I/O you could use? Perhaps you could dig into its database?

Pontus Gagge
  • 17,166
  • 1
  • 38
  • 51
0

Nope, the clipboard doesn't work that way. What if the user wanted to copy something in another app, but couldn't because yours had locked it somehow?

If you're trying to have two processes communicate, look into alternatives designed for inter-process communication, like remoting, named pipes, sockets or shared memory. Remoting is probably the first place to look for applications written in C#.

anton.burger
  • 5,637
  • 32
  • 48