c#中的GetWindowText返回矩形而不是文本

c#中的GetWindowText返回矩形而不是文本,c#,winapi,pinvoke,C#,Winapi,Pinvoke,我有这样的代码: IntPtr hwndGetValue = new IntPtr(67904); List<IntPtr> windows = GetChildWindows(hwndGetValue); int textLength = GetWindowTextLength(windows[0]); StringBuilder outText = new StringBuilder(textLength + 1); int a = GetWindowText(windows[

我有这样的代码:

IntPtr hwndGetValue = new IntPtr(67904);
List<IntPtr> windows = GetChildWindows(hwndGetValue);

int textLength = GetWindowTextLength(windows[0]);
StringBuilder outText = new StringBuilder(textLength + 1);
int a = GetWindowText(windows[0], outText, outText.Capacity);
IntPtr hwndGetValue=新的IntPtr(67904);
列表窗口=GetChildWindows(hwndGetValue);
int textLength=GetWindowTextLength(windows[0]);
StringBuilder outText=新的StringBuilder(textLength+1);
int a=GetWindowText(windows[0],outText,outText.Capacity);
在windows中,我有49个指针。Windows[0]有我可以在Spy++中看到的文本,但方法GetWindowText返回的是outText矩形,而不是此文本。我明白了{慬潹瑵潃瑮潲ㅬ} (在可视和记事本中,此中文标志显示为矩形。我的编码有问题吗


谢谢

这些症状表明您正在调用ANSI版本的
GetWindowText
,但将返回值解释为Unicode

解决方案是确保调用Unicode版本的
GetWindowText
。方法是在
DllImport
中指定
Charset=Charset.Unicode

[DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString,
    int nMaxCount);

哇,谢谢,这个问题快把我逼疯了。把字符集从“CharSet.Auto”改为“CharSet.Unicode”就成功了!