Determine which process is locking the clipboard

前端 未结 4 565
一生所求
一生所求 2020-12-29 04:58

I have a peculiar error where some process occasionally appears to be using the clipboard when my application goes to handle copy & paste operations. There are some ret

4条回答
  •  误落风尘
    2020-12-29 05:50

    Based on Jeff Roe's answer, but shows how to get the text length, so could be > 500. Also handles case where 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();
    }
    

提交回复
热议问题