簡體   English   中英

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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM