ARTICLE AD BOX
In the following example code I open a new Firefox window, wait some time for the window to appear, and then I want to get its HWND.
#include <exception> #include <stdio.h> #include "Windows.h" static HWND GetFirefoxWindow(DWORD createdFirefoxPID) { wchar_t buffer[64]; swprintf_s(buffer, L"PID of the created Firefox: %lu.\n", createdFirefoxPID); OutputDebugStringW(buffer); return NULL; } // Signature taken from // C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um\WinBase.h int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nShowCmd) { STARTUPINFOW si = { sizeof(si) }; PROCESS_INFORMATION pi = {}; wchar_t commandLine[2048] = LR"("C:\Program Files\Mozilla Firefox\firefox.exe")" LR"( -new-window https://example.com)"; if (!CreateProcessW(NULL, commandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { return 1; } CloseHandle(pi.hProcess); CloseHandle(pi.hThread); Sleep(5000); const HWND hwnd = GetFirefoxWindow(pi.dwProcessId); return ERROR_SUCCESS; }How to get the HWND?
The problem is, the process with PID = pi.dwProcessId dies immediately after starting it, so I cannot list all its windows.
I don't want to list all Firefox HWND-s before and after CreateProcessW() and compare them. I want a more robust solution.
