How to handle a blocked clipboard and other oddities

前端 未结 8 2013
闹比i
闹比i 2020-12-02 10:03

Over the course of the last couple of hours I have been tracking down a fairly specific bug with that occurs because another application has the clipboard open. Essentially

8条回答
  •  一向
    一向 (楼主)
    2020-12-02 10:17

    I ran into this error today. I decided to handle it by telling the user about the potentially misbehaving application. To do so, you can do something like this:

    [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 void btnCopy_Click(object sender, EventArgs e)
    {
        try
        {
            Clipboard.Clear();
            Clipboard.SetText(textBox1.Text);
        }
        catch (Exception ex)
        {
            string msg = ex.Message;
            msg += Environment.NewLine;
            msg += Environment.NewLine;
            msg += "The problem:";
            msg += Environment.NewLine;
            msg += getOpenClipboardWindowText();
            MessageBox.Show(msg);
        }
    }
    
    private string getOpenClipboardWindowText()
    {
        IntPtr hwnd = GetOpenClipboardWindow();
        StringBuilder sb = new StringBuilder(501);
        GetWindowText(hwnd.ToInt32(), sb, 500);
        return sb.ToString();
        // example:
        // skype_plugin_core_proxy_window: 02490E80
    }
    

    For me, the problem window title was "skype_plugin_core_proxy_window". I searched for info on that, and was surprised that it yielded only one hit, and that was in Russian. So I'm adding this answer, both to give another hit for that string, and to provide further help to bring potentially-misbehaving apps to light.

提交回复
热议问题