How to find PID of window containing X - c++

I can use FindWindow, but the name of the window changes every time I open it. So my question is, how can I either:
A) Find the PID of a window that contains 'x'
B) Find the PID a window with the name of the exe file?
Sorry if the question is obvious, new to C++. Thanks in advance!

Try to use EnumWindows to get all windows handles
Check state of windows handle by IsWindowVisible. Because some invisible windows can hang the call to GetWindowText
Get the title of each windows by GetWindowText. Then check title contains 'X' which characters or string you want.
Get pid of it by GetWindowThreadProcessId
About B/, you can get executable file name by GetWindowModuleFileName via its windows handle.

Related

C++ program to bring a process to foreground if already running otherwise create a new process

I am trying to learn windows programming. I would like to launch an executable program.exe (say) from c++ code. I am able to achieve this by using CreateProcess() method in windows. However, my problem is if the process is already created and running in the background then the windows for program.exe should come to foreground otherwise a new process should be created and brought to the foreground. Any help will be appreciated.
Look at Win32 API functions such as the following:
to discover if a given process is running, you can use:
CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS) with Process32First() and Process32Next(). See Taking a Snapshot and Viewing Processes.
or
EnumProcesses with OpenProcess() and GetModuleBaseName()/GetProcessImageFileName()/QueryFullProcessImageName(). See Enumerating All Processes.
to find an existing window, you can use:
FindWindow() or FindWindowEx(), if you know the window's class name or title text ahead of time.
or
EnumWindows() with GetClassName() and/or GetWindowText(), if the window's class name or title text are dynamic but follow a pattern you can look for.
to restore a window if it is minimized, you can use IsIconic() with SetWindowPos(SW_RESTORE).
to bring a window into the foreground, you can use BringWindowToTop() and/or SetForegroundWindow().
Is that program.exe written by you? This functionality is better handled there: on start you check if there is already an instance running and if there is - activate it.
Otherwise - what do you do if there are multiple instances of program.exe already running?

How to find window handle from exe file's name

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.

Writing to textbox in another process in Win32 (c++)

Suppose you need to write a process or service (process1) that will read or write to textbox in another application or process (process 2). How is it done?
Is the name of the textbox of process 2 is written in some sort of a registry so i can get it from some sort of a system call?
Thanks.
The text box is identified by an ID number that you can find using Spy++. Use FindWindow and EnumChildWindows to find the HWND of the target text box. With the HWND you can SendMessage WM_GETTEXT or WM_SETTEXT. Note: This won't work if the security level of the two processes differ.

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 ask to restart firefox?

I have created a c++ program that install also a firefox extension. So, to get this extension works, he need to restart firefox.
So, how can I ask to restart firefox while the user is using it ?
This kind of looks like a duplicate to this question: winapi - How can I get a process handle by its name in C++. Essentially, what you'd be looking to do is "find" the process "firefox.exe" (in place of "target.exe") and if finding it is successful, you put up a warning dialogue box to close firefox and re-open. If not, you just continue with the install or whatever. Hope this helped!
Find the main window of Firefox using its caption. I guess Firefox appends the title of the current page to the caption of the main window, so FindWindow is not enough. Try to enumerate top-level windows using EnumWindows, and find the one containing "Mozilla Firefox" in the caption. If you have the window handle, send a WM_CLOSE message to it (use PostMessage which waits for the target window to process the message), and wait for it to disappear. You may get a popup, find and close it the same way. If the main Firefox window is still open after a few seconds (try to find it repeatedly), you may call TerminateProcess (you will need GetWindowThreadProcessId and OpenProcess for this). When the window is closed, restart Firefox by calling CreateProcess. (There are lots of examples for all these API functions on the Web.)