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

Rob*_*ert 3 c# pinvoke winapi

我有这样的代码:

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);
Run Code Online (Sandbox Code Playgroud)

在Windows中我有49个指针.Windows [0]有我在Spy ++中可以看到的文本,但方法GetWindowText以outText矩形而不是此文本返回.我得到{慬潹瑵潃瑮潲ㅬ}(在视觉和记事本中,这个中国标志显示为矩形.我的编码有问题吗?

谢谢

Dav*_*nan 13

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

解决方案是确保您调用Unicode版本GetWindowText.通过Charset=Charset.Unicode在您的指定中执行此操作DllImport.

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