views:

256

answers:

4

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.

A: 

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.

sum1stolemyname
A: 

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.

John Knoeller
I can't seem to make FindWindow....Find the window. I'll add to my post.
kelton52
A: 

If your trying to get big (and do some more UI automation), you sould have a closer look at these:

fmuecke
+2  A: 

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.

Vulcan Eager
I thought about that...but 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.
kelton52
oh my goodness that works...I don't understand what the difference is though.
kelton52
You should have used _TEXT instead of TEXT.
Vulcan Eager