A working example is
public static String GetText(IntPtr hWnd) {
Int32 length = GetWindowTextLength(hWnd);
StringBuilder sb = new StringBuilder(length + 1);
GetWindowText(hWnd, sb, sb.Capacity);
return sb.ToString();
}
The StringBuilder capacity is set for the text length+1 to allow for a null terminator and very importantly the GetWindowText method is given the
correct size. Do not add your own padding! The interop marshaller is being clever and treating the StringBuilder as an array of characters and sb.Capacity is the length of that array.
Alan.