public static string GetWindowTitle(IntPtr hWnd)
{
long titleLength = 0;
SendMessageTimeout(
hWnd,
WM_GETTEXTLENGTH,
IntPtr.Zero,
IntPtr.Zero,
SMTO_ABORTIFHUNG,
SEND_MESSAGE_TIMEOUT,
out titleLength
);
if (titleLength <= 0)
return "";
StringBuilder titleBuilder = new StringBuilder((int)titleLength + 1);
SendMessageTimeout(
hWnd,
WM_GETTEXT,
(IntPtr)titleBuilder.Capacity,
titleBuilder,
SMTO_ABORTIFHUNG,
SEND_MESSAGE_TIMEOUT,
IntPtr.Zero
);
return titleBuilder.ToString();
}