public static string GetWindowText(IntPtr windowHandle)
{
// Check if the handle is valid
HandleManipulator.ValidateAsArgument(windowHandle, "windowHandle");
// Get the size of the window's title
var capacity = NativeMethods.GetWindowTextLength(windowHandle);
// If the window doesn't contain any title
if (capacity == 0)
return string.Empty;
// Get the text of the window's title bar text
var stringBuilder = new StringBuilder(capacity + 1);
if (NativeMethods.GetWindowText(windowHandle, stringBuilder, stringBuilder.Capacity) == 0)
throw new Win32Exception("Couldn't get the text of the window's title bar or the window has no title.");
return stringBuilder.ToString();
}