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.
Related
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?
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.
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.
From the process address space of another app how would you get a handle to each window that it is using/displaying?
I know you can get a snapshot of all the current threads running within a process http://msdn.microsoft.com/en-us/library/ms686701(v=vs.85).aspx but I am wondering if there is a way, using the THREAD ID to then get a hwnd value that you can the test using IsWindow(hwnd), or logically if you can get a hwnd don't you know you already have a value window? But I am wondering if this would work or if indeed it is a sensible approach?
Thanks.
You can use EnumThreadWindows to enumerate all nonchild windows associated with a thread, and then use EnumChildWindows to get all their child windows.
Alternatively, you can use EnumWindows to get all top-level windows on the desktop, and use GetWindowThreadProcessId to filter to only those associated with the process.
Note that this information is very dynamic. Windows come and go all the time. Top level windows will often be the most persistent, but even these can disapear right after you 'find' them, or new ones apear right after you look for them.
i got to do a task that is to find out process /exe/application running in the background.
ie:the process is running but do not have any UI/ Window visible although its an windows GUI application . i thot of reading EXEheader. The header contains a field called 'Subsystem'and application is to run under and the type of interface it requires.
but it returns Windows GUI and it is so. but i want teo detect if that application haves any window or not. also this application is not a service as if it is a service i can easily read the info.
i will be glad if any of you genious put some light on the pronblem stated.
Warm Greetings..
Sarfu
If I understand your question correctly, you want to know if a running application has any visible windows.
To do this, you can call EnumWindows to get all top-level windows. For each window, call GetWindowThreadProcessId to get the process ID and GetWindowLong(hwnd, GWL_STYLE) to get the window style. Test the style for WS_VISIBLE to see if the window is visible. Run through all the windows and see if your process owns a visible one. If you don't have the process ID, you can get them all with EnumProcesses.
The "subssytem" GUI doesn't tell you that the application has a window. In fact, the opposite is closer to the truth. A console application gets a console window. A GUI app is responsible for creating its own windows, if and when it needs them. A GUI process that hasn't called CreateWindow() won't have any windows.
Apparently, you do know the executable you're looking for. In that case, call EnumProcesses() to find all processes, and for each process call EnumProcessModules(). On Windows, "modules" are DLLs and EXEs. Each process will have exactly one EXE module. So, if the one EXE module of any process is the executable you're looking for, then your application is running.