How to find window handle from exe file's name - c++

Imagine I have Firefox and I open Firefox Start Page, then I should have a Window with the title: "Mozilla Firefox Start Page - Mozilla Firefox".
I can find window handle with the code below
HWND hwnd = ::FindWindow(0, _T("Mozilla Firefox Start Page - Mozilla Firefox"));
But what I want is find window handle from the window's exe file's name like this
HWND hwnd = FindWindowFromExe(_T("firefox.exe"));//How to make this function?
Does windows Api has a function like FindWindowFromExe()? If it doesn't, what is the best way to Find window from its exe?
Thanks for reading :)

There is no single API function to find a window by its owning process's file name. You will have to search for it manually.
You can use EnumWindows() to enumerate all top-level windows, or use FindWindow()/FindWindowEx() to find/enumerate specific types of windows.
For each window, you can either:
use GetWindowThreadProcessId() to get the process ID that owns the window, then
use OpenProcess() to open a HANDLE to that process, then
use GetModuleFileNameEx(), GetProcessImageFileName(), or QueryFullProcessImageName() to query the process for its full path and filename.
or
use GetWindowModuleFileName() to query the window for the full path and filename of the module that created it (assuming the intended window is created by an actual EXE and not a DLL used by an EXE).
Once you have the window's filename, you can then compare that to your target filename.

Related

Spy++ Win32 API Getting Window instance from Spy++ Information

I am using Spy++ to find windows, I am doing this as a test and realise the Handles change fequently. However, here is the information I get from Spy++. Can I use these handles to grab that window in C++
Here's how I get it from the name.
HWND main_window_handle = FindWindowA(NULL, WINDOW_NAME);
How can I get it using either the Window Handle or Instance Handle.
The window handle is the HWND and their values are not stable, it will probably change every time you run the program.
The instance handle (HINSTANCE) is also not stable and has little to do with finding a specific window in another application, it is the load address of the module (.exe or .dll) that created the window.
To find a window you will generally call FindWindow with a specific class name. If the class name of the window you are looking for is not really unique then you should probably use EnumWindows and try to look for other specific attributes and/or child windows to identify the top level window you are looking for.
It is also possible (and often the best approach) to use UI Automation to find and manipulate windows in 3rd-party applications.
Try using
HINSTANCE myInstance = (HINSTANCE)&__ImageBase;

Getting Window Title In C++

I am trying to get the current window's title and I am using this:
string GetActiveWindowTitle()
{
char wnd_title[256];
HWND hwnd = GetForegroundWindow();
GetWindowText(hwnd, wnd_title, sizeof(wnd_title));
return wnd_title;
}
This worked for me. Now I want to get window title of every newly opened window on runtime.
E.g if I open Google Chrome, it should then print the title of the Chrome window. After that if I open a notepad file it should print its title.
What could be the possible solution?
If you want to monitor windows which are appearing on the taskbar, you can use the RegisterShellHookWindow function.
If you want to monitor all windows, I believe SetWinEventHook(EVENT_OBJECT_SHOW, ...) is the way to go. See Raymond's post Using accessibility to monitoring windows as they come and go for an implementation example.

How to get window title from it's process path

I have a path of a process, I would like to get it's window title in C++.
Let's assume that this process has only one window.
So for example my process path is :
C:\Program Files\My Program\program.exe which is a running process's path, and I would like to get it's windows title.
Thanks.
Assuming there is only 1 window in the process, and assuming there is only one copy of the executable running at a time, then you can use EnumWindows() to enumerate top-level windows, calling GetWindowThreadProcessId(), OpenProcess() and GetModuleFileNameEx() on each window to get its owning process's path, and if you find a matching path then you can use GetWindowText() to get that window's title and stop the enumeration.

How to get full path of a window in Windows Explorer

When I have to open a folder in Windows Explorer (for example d:\myfolder\test), I use ::GetForegroundWindow() to get its Window Handle, and use ::GetWindowText to get the Title Text. But in the default windows the full path of the current window is not displayed (of course you can through tools-folder option-view-display full path in title bar to set the title bar to display full path), and the ::GetWindowText only gets test. If I want to get the full path d:\myfolder\test how can I get this by the windows handle got by ::GetForegroundWindow() ?
Any suggestions. Thanks.
By using the ShellWindows COM object. See this example on how the get the folder that an Explorer window is viewing.

Find a window using c++ and modifying controls

I would like to use c++ without mfc(and not clr) in order to modify textbox's and activate a button on a form outside of my project. I don't know where to start. I've done a lot of searching but can only find information for VB. A starting point would help.
Thanks.
I tried this and it doesn't seem to work.
HWND fWindow = FindWindow(NULL ,(LPCWSTR)"title");
and I also tried this
HWND fWindow = FindWindow(NULL ,LPCWSTR("title"));
I ALSO tried using LPTSTR instead of LPCWSTR, incase it was a unicode deal.
Maybe I don't understand this microsoft LPCWSTR and LPTSTR crap.
I also tried
HWND fWindow = FindWindow(NULL,TEXT("title"));
and that didn't work.
I guess the windows api must just be broken.
I tried the function on other programs...I'm using xp and I tried catching the calculator, and an explorer window, and something else. But I got nothing.
Heres some of the exact code I'm using to try and figure this out.
HWND face = NULL;
face = FindWindow(NULL,TEXT("My Computer"));
LPSTR title = TEXT("");
GetWindowText(face,title,250);
if(face != NULL)
{
MessageBox(NULL,title,TEXT("WOOP"),1);
}
face = nothing.
title = ""
Bear in mind, I'm not actually trying to hook explorer, I just want to figure out how to get it to work.
Use spy++ or winspector to see the actual "text" of the window.
(Strictly speaking, the caption of the window need not match it's window text. Especially true of "fancy" windows which paint their own caption.)
The following works fine for me (using Calc.exe to test).
HWND hwnd = NULL;
hwnd = FindWindow(NULL,_T("Calculator"));
TCHAR title[251];
if(hwnd != NULL)
{
GetWindowText(hwnd,title,250);
MessageBox(NULL,title,_T("WOOP"),MB_OK);
}
else
MessageBox(NULL,_T("No such window."),_T("OOPS"),MB_OK);
Edit: You should have used _TEXT instead of TEXT.
One way to do this is to use FindWindow to get a handle to the form. Then if you know the button and edit window Ids, you can use GetDlgItem to get their window handles. If you dont know the ids, you can use EnumChildWindows to examine all of the controls on the form.
Once you have the window handles for the controls, you can use SetWindowText to set the text on the edit control, and send a WM_COMMAND message to the form window with the button ID as the command value to make the form think that the button has been clicked.
There are a lot of ways to go about this once you have the correct window handles. There are security issues when you use the window handles of another process, but if the process isn't secured, then inter-process use of window handles just works. For a secured process, you won't be able to find out the window handles.
The windows API provides Methods for this. These should be independent of MFC and CLR, as they are plain win32. I had a project once accessing the Form fields of an Applictation from a loaded DLL (don't ask why).
you might want to look here (Codeproject)
or here (msdn)
At first, you need to obtain a handle to the process you want to access.
When have this, you can use GetDlgItem() (search msdn for that) to retrieve a handle to the desired textbox.
With this handle, you should be able to modify the control in question.
If your trying to get big (and do some more UI automation), you sould have a closer look at these:
Microsoft Active Accessibility
IAccessible2
Microsoft UI Automation
Windows Automation API (Win7)