adding text to another programs text box c++ - 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.

Related

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.

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.

WXwidgets for GUI in C++:

I am totally beginner in creating GUIS and I am using wxwidgets to create GUI programs in CodeBlocks. I am following this tutorials here:
http://wiki.codeblocks.org/index.php?title=WxSmith_tutorials
I have figured out how to create dialog boxes and frames. Now, I have created in C++ a program that reads information from a .txt file and calls a method displayInfo() that prints this information using cout. What I would like to do is to print this information on a single window, by clicking a button, say: "Print Information".
The part that I am finding hard, is how to call my displayInfo() method from the main.cpp of the frame, and how to display that information on a window, instead of the terminal. I tried to import the header file of my class in the main.cpp of the frame, and called displayInfo(), but I do not think this is the right way to do it.
Any help would be appreciated.
wxWidgets has some predefined dialog boxes for display small quantities of text.
See wxMessageBox description
Otherwise you will have to use a DrawText method on the panel or window.
I reccomend using a text control and then redirecting cout to the text control
Like this:
#include <iostream>
wxTextCtrl *control = new wxTextCtrl(...);
wxStreamToTextRedirector redirect(control);
// all output to cout goes into the text control until the exit from current
// scope
For more discussion of neat variations on this trick, take a look at:
http://docs.wxwidgets.org/2.8/wx_wxtextctrl.html
Scroll down to the section titled: wxTextCtrl and C++ streams

Visual Studio C++ how to display a dynamic message (i.e., string) in my About box?

Should be trivial . . . when editing via the VS resource editor.... the tools/objects list only shows 'static text' and the create an event handler wizard has all fields and [next] button dimmed (disabled).
I have a lovely About box -- it all works -- but instead of static text fields to display --
I want/need to display several lines (strings) of current runtime status info.....
I just do know Visual Studio well enough (I'm using 2008). . .
If any one has a simple example -- that really is all I need.
Thanks in advance.
best regards,
Kevin Waite
If you put a static text box in your dialog you can set its text to anything you want at runtime. First you need to get the window handle of the text box:
HWND hwndText = GetDlgItem(hwndDialog, IDC_MYTEXT);
Then you can set the new text into it:
SetWindowText(hwndText, L"Hi mom, this is my first text box!");
Static text isn't meant to change, so Windows doesn't always do the right thing when you change it. You need to tell it to erase and repaint so that the new text is properly displayed.
InvalidateRect(hwndText, NULL, true);
How about adding an empty static text, and just setting its Text property?
I just made an empty Windows Forms application in Visual Studio C++ Express, and dragged a "Label" control onto the form. In the forms Load function, the text can be set like this:
this->label1->Text = "Hello World";
The same method can be used if you want larger texts. Just use a multiline TextBox instead.
If you want to display multiple lines of text, you can use the EditBox control and set the multiline property to True.
To pass the data to the about dialog, you will need to have to pass those strings to the dialog when the dialog is created (before the call to DoModal); and have the string added to the editbox in the aboutbox OnInitDialog.
If you need the text to be updated live while the about dialog is open you will probably have to add a thread that will fetch the strings from somewhere and the UI will be updated with those new strings.
Good luck.

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)