[DllImport("user32.dll")]
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int nMaxCount);
int length = GetWindowTextLength(hWnd);
StringBuilder windowName = new StringBuilder(length + 1);
GetWindowText(hWnd, windowName, windowName.Capacity);
用DllImport声明GetWindowText时,千万别跟第2个参数加out。