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
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();
}