EnumDesktopWindows(C++)大约需要30分钟才能在Windows 10上找到所需的打开窗口
EnumDesktopWindows (C++) takes about 30 mins to find the desired open Window on Windows 10
此问题仅在Windows 10上发生。在其他版本(如Windows 7)上工作正常。
在用户操作上,我有以下代码来找出另一个打开的应用程序窗口,如下所示:
void zcTarget::LocateSecondAppWindow( void )
{
ghwndAppWindow = NULL;
CString csQuickenTitleSearch = "MySecondApp";
::EnumDesktopWindows( hDesktop, MyCallback, (LPARAM)(LPCTSTR)csTitleSearch );
}
回调函数为:
BOOL CALLBACK MyCallback( HWND hwnd, LPARAM lParam)
{
if ( ::GetWindowTextLength( hwnd ) == 0 )
{
return TRUE;
}
CString strText;
GetWindowText( hwnd, strText.GetBuffer( 256 ), 256 );
strText.ReleaseBuffer();
if ( strText.Find( (LPCTSTR)lParam ) == 0 )
{
// We found the desired app HWND, so save it off, and return FALSE to
// tell EnumDesktopWindows to stopping iterating desktop HWNDs.
ghwndAppWindow = hwnd;
return FALSE;
}
return TRUE;
} // This is the line after which call is not returned for about 30 mins
上面提到的这个回调函数被调用大约 7 次,每次返回 True。在此阶段,它会找到自己的应用程序窗口,通过该窗口调用 EnumDesktopWindows。
它按预期返回 True,但随后大约 30 分钟内没有任何反应。未命中调试点。此时,原始正在运行的应用程序无响应。
如何解决这个问题?
找到了另一条路。而不是按窗口名称,查找进程帮助。使用进程名称获取进程,提取进程 ID 并获取窗口句柄。
void zcTarget::LocateSecondAppWindow( void )
{
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
if (_stricmp(entry.szExeFile, "myApp.exe") == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
EnumData ed = { GetProcessId( hProcess ) };
if ( !EnumWindows( EnumProc, (LPARAM)&ed ) &&
( GetLastError() == ERROR_SUCCESS ) ) {
ghwndQuickenWindow = ed.hWnd;
}
CloseHandle(hProcess);
break;
}
}
}
CloseHandle(snapshot);
}
BOOL CALLBACK EnumProc( HWND hWnd, LPARAM lParam ) {
// Retrieve storage location for communication data
zcmTaxLinkProTarget::EnumData& ed = *(zcmTaxLinkProTarget::EnumData*)lParam;
DWORD dwProcessId = 0x0;
// Query process ID for hWnd
GetWindowThreadProcessId( hWnd, &dwProcessId );
// Apply filter - if you want to implement additional restrictions,
// this is the place to do so.
if ( ed.dwProcessId == dwProcessId ) {
// Found a window matching the process ID
ed.hWnd = hWnd;
// Report success
SetLastError( ERROR_SUCCESS );
// Stop enumeration
return FALSE;
}
// Continue enumeration
return TRUE;
}
相关文章:
- 自 Windows 10 20H1 以来,具有单独线程的多个窗口停止工作
- Windows Service 可以枚举桌面顶级窗口吗?
- Windows 脚本主机在关闭创建的窗口时终止
- 无法使我的第一个Windows OpenGL窗口抬起并运行
- 在控制台窗口关闭(在Windows上)上优雅地关闭命令行应用程序
- 在Windows 10上弹出代码:: Bloks的窗口
- 操纵Windows Explorer窗口时,UDP数据包会掉落
- QT/Windows,可重大的无框窗口
- 编写 Windows 桌面应用程序时,我应该在哪里跟踪我的窗口句柄?
- 什么是窗口最大化/最小化/恢复的WM Windows消息
- 使用64位 自定义窗口形状(ETC平滑的角落不是矩形)在Windows中使用C 创建OpenGL窗口
- 如何调整Windows 10窗口并忽略透明边框
- Windows SetFocus,目标窗口不会捕获键盘输入
- 从Windows注册表中保存的工作区检索窗口放置
- 是否有一种在windows上分发C/C++开发包(无源代码)的首选规范方式
- C++ Windows CreateChildProcess - 隐藏/不显示子进程的控制台窗口
- 在Windows中平滑调整窗口大小(使用Direct2D 1.1)
- Windows窗体作为非托管应用程序的子窗口
- 更换Windows上的系统菜单弹出窗口
- 将快速文本C++ Windows 32 窗口