Determine which process locks the clipboard

I have a peculiar error when some process sometimes uses the clipboard, when my application switches to copy and paste operations. There are several attempts to work around, and I have an acceptable solution in place, but I would like to find out what process this happens if the error occurs again.

+16
c #
Jul 05 '11 at 13:45
source share
4 answers

I wrapped my solution in an easy to use method (and 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 it!

+18
Jul 05 2018-11-11T00:
source share

Here's a similar solution, but this gives you a line that you can show the user:

 [System.Runtime.InteropServices.DllImport("user32.dll")] static extern IntPtr GetOpenClipboardWindow(); [System.Runtime.InteropServices.DllImport("user32.dll")] static extern int GetWindowText(int hwnd, StringBuilder text, int count); private string getOpenClipboardWindowText() { IntPtr hwnd = GetOpenClipboardWindow(); StringBuilder sb = new StringBuilder(501); GetWindowText(hwnd.ToInt32(), sb, 500); return sb.ToString(); } 
+3
Oct 12 2018-11-12T00:
source share

Based on Jeff Roy's answer, but shows how to get the length of the text, so it can be> 500. It also handles the case when the window is not found.

 [System.Runtime.InteropServices.DllImport("user32.dll")] static extern IntPtr GetOpenClipboardWindow(); [System.Runtime.InteropServices.DllImport("user32.dll")] static extern int GetWindowText(int hwnd, StringBuilder text, int count); [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern int GetWindowTextLength(int hwnd); private static string GetOpenClipboardWindowText() { var hwnd = GetOpenClipboardWindow(); if (hwnd == IntPtr.Zero) { return "Unknown"; } var int32Handle = hwnd.ToInt32(); var len = GetWindowTextLength(int32Handle); var sb = new StringBuilder(len); GetWindowText(int32Handle, sb, len); return sb.ToString(); } 
+3
Sep 25 '12 at 10:25
source share

To diagnose something like this, I would suggest starting with Process Explorer, http://technet.microsoft.com/en-us/sysinternals/bb896653

0
Jul 05 2018-11-11T00:
source share



All Articles