简体   繁体   English

EnumDesktopWindows (C++) 大约需要 30 分钟才能在 Windows 10 上找到所需的打开窗口

[英]EnumDesktopWindows (C++) takes about 30 mins to find the desired open Window on Windows 10

This problem occurs only on Windows 10. Works fine on other versions such as Windows 7.此问题仅在 Windows 10 上发生。在其他版本(如 Windows 7)上工作正常。

On user action, I have following code to find out another open application window as:在用户操作上,我有以下代码来找出另一个打开的应用程序窗口:

void zcTarget::LocateSecondAppWindow( void )
{
    ghwndAppWindow = NULL;
    CString csQuickenTitleSearch = "MySecondApp";
    ::EnumDesktopWindows( hDesktop, MyCallback, (LPARAM)(LPCTSTR)csTitleSearch );
}

With callback functions as:使用回调函数:

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

This callback function mentioned above gets called for about 7 times, each time returning True.上面提到的这个回调函数被调用了大约 7 次,每次都返回 True。 At this stage it finds own app window through which EnumDesktopWindows was invoked.在此阶段,它会找到自己的应用程序窗口,通过该窗口调用 EnumDesktopWindows。

It returns True as expected, but then nothing happens for about 30 minutes.它按预期返回 True,但随后大约 30 分钟没有任何反应。 No debug points hit.没有调试点命中。 The original running application is unresponsive at this point.此时原始运行的应用程序没有响应。

How to resolve this problem?如何解决这个问题?

Found another path.找到了另一条路。 Instead of going by Window name, looking for Process helps.而不是通过窗口名称,寻找进程帮助。 Get process using process name, extract process id and get window handle.使用进程名称获取进程,提取进程 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM