C# Getting window's title by handle in Hebrew return question marks

邮差的信 提交于 2019-12-23 17:21:40

问题


I am using this: to get window's title by its handle:

[DllImport("user32.dll")] private static extern int GetWindowText(int hWnd, StringBuilder title, int size);

StringBuilder title = new StringBuilder(256);
GetWindowText(hWnd, title, 256);

If the title has hebrew chars, it replaces them by question marks.
I guess the problem is related to econding or something... how can I solve it?


回答1:


Use this:

[DllImport("user32.dll", CharSet = CharSet.Auto)]



回答2:


Your question contains a little error, that might not occurs very often. You assume, that the title has a max length of 256 characters, which might be enough for most cases. But as this post shows, the length could be at 100K characters, maybe more. So I would use another helper function: GetWindowTextLength

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

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetWindowTextLength(IntPtr hWnd);

public static string GetWindowTitle(IntPtr hWnd)
{
    var length = GetWindowTextLength(hWnd);
    var title = new StringBuilder(length);
    GetWindowText(hWnd, title, length);
    return title.ToString();
}


来源:https://stackoverflow.com/questions/19144658/c-sharp-getting-windows-title-by-handle-in-hebrew-return-question-marks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!