Determine which process is locking the clipboard

前端 未结 4 554
一生所求
一生所求 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:51

    I've wrapped my solution into 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!

提交回复
热议问题