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.
Related
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.
With the following code part, I can get the handle of the open file dialog box that is opened. That works fine.
HWND handleForWindow = FindWindow(NULL, "Open");
Now, I need to type the filepath + filename into the textbox area in the same dialog box. For this, I need to get the handle of the TextBox. I have been using Spy++ and it worked great so far. However, it doesn't get the handle of the TextBox. Spy++ says that the class is "Edit" and has no caption.
I tried the following and some other possibilities with the caption and the class name but no luck.
HWND hwndTextBox = FindWindowEx(handleForWindow, 0, "Edit", "Caption");
Actually the TextBox is a part of ComboBox. If I put there "ComboBox", it gets the handle nicely. But in that case if I send a text message to the box, it doesn't put any text there.
SendMessage(hwndTextBox, WM_SETTEXT, 0, (LPARAM)"AnyText");
Since this doesn't work, I had an idea of getting the current active element in the window because the TextBox comes active all the time whenever it is opened. But I couldn't find any Api call for this. Could anyone help me about that?
Thanks.
You can send a CB_GETCOMBOBOXINFO message to a ComboBox to retrieve the HWND of its edit box.
If you are trying to interact with an Open File dialog from the Common Dialog library, you can send CDM_GETFOLDERPATH and CDM_GETFILEPATH messages to the dialog itself to get its current path and filename, and a CDM_SETCONTROLTEXT message to change the filename displayed in the dialog's cmb13 or edt1 field (see Explorer-Style Control Identifiers).
I've been having trouble finding a way to check if the user of the program is visiting a specific website. If I wanted to open a pop up box telling me which website I was currently on, what would be the best way to do it?
I've been thinking about various ways but I can never come to a definite conclusion. Thought about checking which browser is open by browsing the processes or checking the window, but how would I find it out? Even if I didn't find out exact website address but just the name, would be fine.
For example, right now the window open says 'Check Which Website is Visited - Stack Overflow - Mozilla Firefox', is there a way to get that from a programming standpoint? Like somehow check and read what windows are currently open.
Thanks for any help.
To get the title of the active window use this code:
HWND hwnd = GetForegroundWindow();
CString title;
LPTSTR str = title.GetBufferSetLength(GetWindowTextLength(hwnd));
GetWindowText(hwnd, str, title.GetLength() + 1);
if (title.IsEmpty())title = _T("User Desktop");
...
The active window will be under "title".
To get the active running application use this code:
DWORD pid;
GetWindowThreadProcessId(GetForegroundWindow(), &pid);
HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
if (hProcess != NULL)
{
TCHAR path[MAX_PATH] = { 0 };
TCHAR filename[MAX_PATH] = { 0 };
GetProcessImageFileName(hProcess, path, MAX_PATH);
_wsplitpath(path, NULL, NULL, filename, NULL);
CloseHandle(hProcess);
...
filename will hold the active application
Getting the exact URL of the current site from a web browser is going to be specific to each browser. In most cases the easiest way is going to be creating a browser plugin or extension that your application can communicate with to retrieve the desired information. Whether that is a viable option or not is going to be specific to what you are trying to do and whether the user is willing to install the plugin. A lot of applications such as Yahoo Messenger used to do this to integrate messenger functionality into the browser but I don't know if they still do.
Even retrieving just the title is going to be problematic as each browser is different. In some cases you can use GetWindowText to retrieve the caption bar title. Unfortunately browsers such as Internet Explorer and Chrome use tabs or custom embedded controls to present that information to users so using GetWindowText is of limited use in this case.
In order to use GetWindowText to retrieve the text in the caption bar you will have to obtain the handle to the specific web browser window though. You can do this with EnumWindows and use information like process ID with that owns the window or the window class (not a C++ class) to determine if it's a browser window. You will also need to deal with the possibility that there could be more than own browser window open. In this case you could prompt the user with a list of titles and have them select one, it just depends on what you are actually trying to accomplish.
I'm writing a keylogger type application, and I have pretty much everything done, my problem is getting the window title, I don't want to write the window title every time they press a key, I can get the window title, like I know how to, but how would I only write it to the log when its a new window?
You could maintain an internal list of all windows (with window handles as identifiers to keep memory usage low), and when a new one appears, then you can extract it.
The Win32 API (which I assume you're using) has the function:
EnumChildWindows
which can be used for this task. Call
GetDesktopWindow
to supply as the input window handle, and you'll get every window created under this desktop session.
I'm not sure if this would still work via RDP (and Windows Vista+ have the secure desktop as well as the normal clients), but it depends how vital this functionality is for you.
Store the previous window title and if the current window title is different than the previous window title, print the new window title and set the previous window title to the current window title.
This fails if there's multiple windows with the same name, but it may work for your needs.
Anytime a keypress is detected, call GetForegroundWindow to get the handle to the currently focused window. Store this value as a variable in your program and when it changes, call GetWindowText to get the title of the window and log this title.
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)