I am using Win32 API functions to register the current program in the Clipboard chain. Below function retrieves the window handle of the current owner of the clipboard. The problem is
GetCaptionOfWindow always return null.
[DllImport("user32.dll")]
public static extern IntPtr GetClipboardOwner();
Pinvoke signature
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetWindowTextLength(IntPtr hWnd);
What I have tried:
IntPtr ClipboardOwnerHandler = GetClipboardOwner();
string windowTitle = GetCaptionOfWindow(ClipboardOwnerHandler );
private string GetCaptionOfWindow(IntPtr hwnd)
{
string caption = "";
StringBuilder windowText = null;
try
{
int max_length = WindowsApiManager.GetWindowTextLength(hwnd);
windowText = new StringBuilder("", max_length + 5);
WindowsApiManager.GetWindowText(hwnd, windowText, max_length + 2);
if (!String.IsNullOrEmpty(windowText.ToString()) && !String.IsNullOrWhiteSpace(windowText.ToString()))
caption = windowText.ToString();
}
catch (Exception ex)
{
caption = ex.Message;
}
finally
{
windowText = null;
}
return caption;
}