Writing to textbox in another process in Win32 (c++) - 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.

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;

How to find PID of window containing X

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.

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.

adding text to another programs text box c++

i have already managed to send text to a custom text box i created using c++, and to notepad, calc and other programs all with 1 window and 1 text box. however, i want to send text to another program that has more than one text box and is in tabs too. it is structured like so:
open program
choose from a selection of 2 tabs: a. stats b. config(which contains the text boxes)
fill in the 4 text boxes to desired values
i have tried winspy++ with no luck, here is simple code i have been working with.
#include <windows.h>
int main()
{
HWND hNote;
HWND hChild;
if (!(hNote=FindWindow("windowname",NULL)))
exit(1);
if (!(hChild=FindWindowEx(hNote,NULL,"EDIT",NULL)))
exit(2);
SendMessage(hChild,WM_SETTEXT,NULL,(LPARAM)"texttoadd");
return 0;
}
Can anyone help me how can resolve this issue ?
So the problem is to get a handle of the specific control. You may use for example following ways for finding control's handle:
Control can be distinguished by control id, then use GetDlgItem function to get the its handle. Control id can be found using tools like Spy++ or InqSoft Windows Scanner or other.
MSDN says that control can be found by coordinates of the point within parent window by ChildWindowFromPoint , ChildWindowFromPointEx or RealChildWindowFromPoint function.
Or all controls can be enumerated within parent window by EnumChildWindows and an appropriate one can be found using custom rules.

How to send text to an application?

I am trying to make an application, which reads the data from a serial port (on the serial port there is a barcode scanner plugged in), and then forwards it to an application. I can read data from serial port now, but i don't know, how to forward the read text, to an application, for example notepad. I tried to use SendMessage() API but it didn't succeed. Maybe i did something wrong.
Could someone help me, and maybe show some example?
Thanks,
kampi
Sounds like you're looking for keybd_event or the newer SendInput. It allows you to simulate keyboard input.
HWND hwnd = FindWindow(NULL, L"Untitled - Notepad");
SendMessage(hwnd, WM_SETTEXT, NULL, (LPARAM)L"Hello!");
This will set the Notepad's title bar text to Hello. Of course, you can elaborate a bit to find Notepad's textbox control, or to find your own control in an application, or to find the control that has focus in the active foreground window (see GetForegroundWindow), but the idea is that once you have a hwnd of the window/control you want to set text, the above code should work.
If you want to send it to the Notepad, then it would be easier to save the text into a temporary file and then open it with the Notepad. From a Windows application this could be done using CreateProcess.
On the other hand, if you in control of how the receiver application is working, you could use different approaches, such as: pipes, window messages, shared memory and some others. This is a good place to start.
If you mean another application, you should use one of IPC methods.
The simplest method should be named pipes.