Determine which process is locking the clipboard

I was having a peculiar error where some process occasionally appeared to be using the clipboard when my application went to handle copy & paste operations. There are some retry work arounds, and I had an acceptable solution in place, but I wanted to locate which process it was if the error keeps occurring.

After some research and a little coding, I’ve wrapped my solution into an easy-to-use method (plus some declarations):

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetOpenClipboardWindow();

[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

private static Process GetProcessLockingClipboard()
{
    int processId;
    GetWindowThreadProcessId(GetOpenClipboardWindow(), out processId);

    return Process.GetProcessById(processId);
}

Enjoy!

This entry was posted in C# and tagged . Bookmark the permalink.
  • Delicious
  • Facebook
  • Reddit
  • StumbleUpon
  • Twitter
  • RSS Feed
  • Google
  • Digg

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>