How come FindWindow finds a window that EnumChildWindows doesn't? - c++

I'm looking for a window that its class name is "CLIPBRDWNDCLASS" (it can be found in office apps and other applications).
If I use FindWindow or FindWindowEx I find the first HWND that has this class, but I want all the windows with that class, so I decided to use recursive EnumChildWindows to enumerate all windows and find the window I want:
//-------------------------------------------------------------------------------
BOOL CALLBACK enum_wnd_proc(HWND h, LPARAM lp)
{
char cls[1024] = {0};
::GetClassNameA(h, cls, 1024);
if(std::string(cls) == "CLIPBRDWNDCLASS")
{
// match!
}
::EnumChildWindows(h, enum_wnd_proc, NULL);
return TRUE;
}
//-------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
::EnumWindows(enum_wnd_proc, NULL);
return 0;
}
//-------------------------------------------------------------------------------
The this is that this window does not return by the EnumWindows, only by FindWindow.
Does anyone can tell why it doesn't work ???

The reason EnumWindows doesn't work is that the window you are looking for is a message only window.
FindWindowEx can find them in two cases:
If both hwndParent and hwndChildAfter are NULL.
If you specify 'HWND_MESSAGE' as your parent window.
This code will find all the relevant windows for you (a modified version of a solution from here):
HWND hWindow = FindWindowExA(HWND_MESSAGE, NULL, "CLIPBRDWNDCLASS", NULL);
while (hWindow )
{
// Do something here with window...
// Find next window
hWindow = FindWindowExA(HWND_MESSAGE, hWindow , "CLIPBRDWNDCLASS", NULL);
}
Also note that unlike what's written in the above link, GetParent() for message only windows does not return HWND_MESSAGE (at least not for my tests).

My old easy way to ENUMERATE all message-only windows:
EnumChildWindows(GetAncestor(FindWindowEx(HWND_MESSAGE,0,0,0),GA_PARENT),addr EnumChildProc,0)
// GetAncestor(FindWindowEx(HWND_MESSAGE,0,0,0),GA_PARENT) = "GetMessageWindow" (class "Message")
// GetAncestor(FindWindowEx(HWND_DESKTOP,0,0,0),GA_PARENT) = GetDesktopWindow (class "#32769")

Related

How to get Main Window Handle on Page in WinUI 3 Using C++

I am working on a WinUI 3 demo using C++.
I want to get a Main or Native Window Handler to open a Picker within a Page.
The code block which I am using works fine on Window but it does not work on Page.
auto windowNative{ this->try_as<::IWindowNative>()};
winrt::check_bool(windowNative);
HWND hWnd{ 0 };
windowNative->get_WindowHandle(&hWnd);
Help me to get MainWindow Handler in Page1.xaml.cpp
Only Window implements IWindowNative, so you need to pass the window reference around, or if you're sure there's only one Window in your process, you can use a code like this:
HWND GetProcessFirstWindowHandle(DWORD pid = 0)
{
struct ProcessWindow { DWORD pid; HWND hWnd; } pw = {};
pw.pid = pid ? pid : GetCurrentProcessId();
EnumWindows([](auto hWnd, auto lParam)
{
DWORD pid;
GetWindowThreadProcessId(hWnd, &pid);
if (pid != ((ProcessWindow*)lParam)->pid)
return TRUE;
((ProcessWindow*)lParam)->hWnd = hWnd;
return FALSE;
}, (LPARAM)&pw);
return pw.hWnd;
}
And for example, call it simply like this:
void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
{
auto hwnd = GetProcessFirstWindowHandle();
}
You can also add some check on class name, like what's done in this answer (it's C# but the code is already using interop to access native Windows APIs) : Retrive Window Handle in Class Library WinUI3

Why did I fail to find window use parent specified in Spy++?

Imagine there is a web page with a <input type="file" />. I open this page in Firefox, and click on the file button. Then a dialog will popup.
I want to set the file name edit of the popup dialog programmatically in C++:
First I use Spy++ to check window class, the Firefox window and popup dialog properties in Spy++ look like:
Firefox window:
Handle: 001E013E
Caption: Table with objects - Mozilla Firefox
Class: MozillaWindowClass
Popup dialog:
Handle: 004508BE
Caption: File Upload
Class: #32770 (Dialog)
Spy++ also shows browser window is the parent of popup dialog.
My code looks like:
#include <Windows.h>
#include <stdio.h>
int main()
{
HWND hBrowser = FindWindow(L"MozillaWindowClass", NULL);
printf("Browser hwnd=%X\n", hBrowser);
HWND hDialog = FindWindowEx(hBrowser, NULL, L"#32770 (Dialog)", NULL);
printf("Dialog hwnd=%X\n", hDialog);
system("pause");
}
But the value of hBrowser does not equal the value in Spy++ dialog, and value of hDialog is NULL. I have only one Firefox window opened, with only one tab.
Then I tried to change my code to:
// 0x001E013E is the handle in Spy++
HWND hDialog = FindWindowEx((HWND)0x001E013E, NULL, L"#32770 (Dialog)", NULL);
hDialog still outputs as NULL.
Questions:
Why the handle in Spy++ and the one I get in program is not the same?
Does the "parent window" in Spy++ have the same meaning with the parent parameter in FindWindowEx?
Note: I can't use window title to do the find, due to localization issue (Firefox may be installed in languages other than English).
MozillaWindowClass is the owner of the open dialog, it is not a parent you can use with FindWindowEx. There can also be more than one MozillaWindowClass window so it is better to look for the dialog first:
BOOL CALLBACK FindMozillaOpenFilenameDialogEnumProc(HWND hWnd, LPARAM param)
{
HWND*pData = (HWND*) param;
if (GetClassLongPtr(hWnd, GCW_ATOM) == 32770) // Found a dialog?
{
HWND hOwner = GetWindow(hWnd, GW_OWNER);
if (hOwner)
{
WCHAR buf[100];
GetClassName(hOwner, buf, 100);
if (0 == lstrcmp(buf, TEXT("MozillaWindowClass"))) // The dialog is owned by a Mozilla window?
{
HWND hCombo = GetDlgItem(hWnd, 0x047c); // cmb13
GetClassName(hCombo, buf, 100);
buf[8] = '\0'; // Some Windows versions use ComboBox and some use ComboBoxEx32, we only care if it is some type of combobox
if (0 == lstrcmp(buf, TEXT("ComboBox"))) // The dialog contains a ComboBox with the expected ID?
{
*pData = hWnd;
return false;
}
}
}
}
return true;
}
int main()
{
HWND hDialog = NULL;
EnumWindows(FindMozillaOpenFilenameDialogEnumProc, (LPARAM) &hDialog);
printf("Dialog hwnd=%X\n", hDialog);
if (hDialog)
{
HWND hCombo = GetDlgItem(hDialog, 0x047c);
SendMessage(hCombo, WM_SETTEXT, 0, (LPARAM) TEXT("c:\\foo\\bar.exe")); // Could also use CDM_SETCONTROLTEXT?
}
return 0;
}
This code relies on undocumented and internal names and window relationships, it could break at any time.
Keep in mind that "MozillaWindowClass" is a internal Mozilla name and could change at any time. The documented cmb13 id of the filename control is only documented for GetOpenFileName and GetSaveFileName but not for IFileDialog based dialogs. You really should use UI Automation when you interact with the open dialog in another application!

EnumChildWindows never calls its callback

I'm trying to manipulate a specific Internet Explorer 11 window. Using WinSpy++ I find that
The top level window's class is an IEFrame with the title of the document as the text (as returned by GetWindowText)
The actual web view class is called "Internet Explorer_Server" and is a child of the former.
I wrote a simple test case for finding the web view of IE11 opened on "https://encrypted.google.com/" in three different ways:
HWND FindIE_A()
{
// Use FindWindow, works!
HWND hWndTop = ::FindWindowA( NULL, "Google - Internet Explorer" );
// Find the web view window, the callback (FindIEServer) is NEVER called!
HWND hWnd = NULL;
::EnumChildWindows( hWndTop, &FindIEServer, (LPARAM)&hWnd );
return hWnd;
}
HWND FindIE_B()
{
// Use EnumChildWindows with NULL as parent, works!
HWND hWndTop = NULL;
::EnumChildWindows( NULL, &FindIEMain, (LPARAM)&hWndTop );
// Find the web view window, the callback (FindIEServer) is NEVER called!
HWND hWnd = NULL;
::EnumChildWindows( hWndTop, &FindIEServer, (LPARAM)&hWnd );
return hWnd;
}
HWND FindIE_C()
{
// Simple EnumWindows, works!
HWND hWndTop = NULL;
::EnumWindows( &FindIEMain, (LPARAM)&hWndTop );
// Find the web view window, the callback (FindIEServer) is NEVER called!
HWND hWnd = NULL;
::EnumChildWindows( hWndTop, &FindIEServer, (LPARAM)&hWnd );
return hWnd;
}
The callbacks that are very simple; get a property from the window and compare against a hard-coded value:
BOOL CALLBACK FindIEServer( HWND hWnd, LPARAM lParam )
{
char className[64];
::GetClassNameA( hWnd, className, sizeof(className) );
if ( !strcmp( className, "Internet Explorer_Server" ) )
{
*(HWND*)lParam = hWnd;
return FALSE;
}
return TRUE;
}
BOOL CALLBACK FindIEMain( HWND hWnd, LPARAM lParam )
{
char text[128];
::GetWindowTextA( hWnd, text, sizeof(text) );
if ( !strcmp( text, "Google - Internet Explorer" ) )
{
*(HWND*)lParam = hWnd;
return FALSE;
}
return TRUE;
}
EnumChildWindows failed (by not calling the callback AT ALL!) every time when provided with a parent window. Why?
The problem is that when I look for the window title, I was assuming there was only one window with that title. However Internet Explorer does some shenanigans and creates multiple windows with the same title however only one of them has the class IEFrame.
It just so happens that the first window found was the wrong one, it didn't have any children (and thus EnumChildWindows doesn't have anything to iterate over). Just adding an extra check for title + class works.
However as #wakjah suggested, it is better integrate IE (or any other browser) directly into your code. With google I found lots of documentation on how to do this with both IE and Chrome.

Win32/Qt - Can one Enumerate all the toplevel windows belonging to the calling process?

I want to detect all the top-level windows in order to send messages to it's Descendants.
How can I do that?
The following code seems to not be detecting Qt top level window, I don't know why.
static BOOL CALLBACK EnumWindowsProc(_In_ HWND hwnd, _In_ LPARAM lParam) {
WORD far wndProcessID;
WORD currentProcessID = GetCurrentProcessId();
std::vector<HWND> *topWindowList = (std::vector<HWND> *)lParam;
if (topWindowList != NULL &&
GetWindowThreadProcessId(hwnd, NULL) == currentProcessID) {
printf("Found a top level window");
fflush(stdout);
topWindowList->push_back(hwnd);
}
return TRUE;
}
void enumAllDesktopChildWindow() {
std::vector<HWND> topWindowList;
EnumChildWindows(GetDesktopWindow(), EnumWindowsProc, LPARAM(&topWindowList));
}
First, the GetWindowThreadProcessId API returns a Thread ID (TID) not a Process ID (PID)
Second, if you want to enumerate all top-level Windows, you should use EnumWindows, not EnumChildWindows. If you want to use EnumChildWindows, pass NULL as the first parameter.

C++ Win 32 API: Problem With Passing window SW_HIDE

I am attempting to hide a 3rd part window on bootup of our computers. I am using the following code.
#include<windows.h>
#include <stdio.h>
int main() {
char windowName[500];
HWND window = FindWindow("WindowClassAsReportedByWindowSpy++", NULL);
//GetWindowText(window, windowName, 63);
ShowWindow(firefox,SW_HIDE);
getchar();
return 0;
}
The only problem is the window will not hide. Any ideas on why this isn't working /how I could accomplish this differently.
Most likely your program calls FindWindow before the target window is created, and so doesn't find it.
You'll need to sleep and retry the find.
You probably want to do sanity checks to make sure FindWindow is not returning NULL. Even better, call FindWindow in a loop until it doesn't return NULL.
#include <windows.h>
#include <stdio.h>
static const wchar_t g_cszFirefoxClass[] = L"firefox";
int __cdecl wmain(__in int argc, __in_ecount_z_opt(argc) wchar_t* _wargv[], __in_z_opt __wenviron[])
{
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(_wargv);
UNREFERENCED_PARAMETER(__wenviron);
HWND hWnd;
do {
hWnd = FindWindow(g_cszFirefoxClass, NULL);
Sleep(100);
} while (hWnd == NULL);
wprintf(L"[-] Firefox found! [HWND = 0x%X]\n", hWnd);
if (ShowWindow(hWnd, SW_HIDE))
{
wprintf(L"[-] Successfully hid Firefox window!\n");
return EXIT_SUCCESS;
}
else
{
fwprintf(stderr, L"[x] Failed to hide Firefox window..\n");
return EXIT_FAILURE;
}
}