无法使用GetGUIThreadInfo获取窗口标题

Unable to get Window Title using GetGUIThreadInfo

本文关键字:获取 窗口标题 GetGUIThreadInfo      更新时间:2023-10-16

我正在谷歌搜索,发现上面的函数在检索窗口标题文本时更可靠,但它不起作用。我没有得到任何结果;换句话说,没有捕获标题字符串。

GUITHREADINFO gui;
gui.cbSize = sizeof(gui);
char pStr[1024];
GetGUIThreadInfo pGetGUIThreadInfo;
HMODULE hinstUser = LoadLibrary((LPCTSTR) "user32.dll"); 
pGetGUIThreadInfo = (GetGUIThreadInfo)GetProcAddress(GetModuleHandle(TEXT("user32.dll")), "GetGUIThreadInfo");
if (pGetGUIThreadInfo(0 , &gui) == 0)
    return NULL;
GetWindowText(gui.hwndFocus, pStr, 1024);

但是上面的代码失败。使用下面的代码

是有效的
HWND hwnd = GetForegroundWindow();
GetWindowText(hwnd, pStr, 1024);

我认为如果你使用hwndActive而不是hwndFocus,它会达到你想要的效果。

也就是说,我看不出使用GetForegroundWindow()返回的窗口有什么问题。