Among the many shared software, there is a kind of things that may interest everyone-get the password in the editing box. In fact, its implementation mechanism is very simple, and there is no essential difference from obtaining the text in any window. However, obtaining the password in the edit box of another thread is a little troublesome. The key is how to obtain the handle of the edit box. The following two methods are provided for your reference.
Method 1: Obtain the mouse pointer
The key to this method is to obtain the handle of the window pointed to by the current mouse pointer. To obtain the position of the current mouse pointer, use the system function: getcursorpos (& Point). For details, see the function about cursor in msdn. The implementation steps are as follows:
1. Create a timer (interval of 2 seconds) in the dialog box and perform the following operations in the ontimer function:
2. Call the system function: getcursorpos (& Point) in the ontimer (uint nidevent) function );
3. Call cwnd: windowfrompoint (point) to obtain the target window pointer W (type: cwnd *);
5. Call w-> getsafehwnd () to obtain the handle hwnd of the window;
6. Call: getclassname () to obtain the Class Name of the window and determine whether the class name is "edit ". Call: getwindowlong (hwnd, gwl_style) to determine whether the window style is es_password, so as to determine whether it is a password;
7. Call
: Sendmessage (
(Hwnd) hwnd, // handle to destination window
Wm_gettext, // message to send
(Wparam) 55, // number of characters to copy
(Lparam) sname // text Buffer
);
Obtain the text (password) in the target window (edit box ).
Note: you cannot use: getwindowtext () to obtain the text in the edit box of another thread. sendmessage () must be used (). On the contrary, sendmessage () or: getwindowtext () can be used to obtain titles of other types of windows.
The following example automatically obtains the text (or password) in the window pointed to by the pointer as the mouse pointer moves, and has been debugged.
Void cgetpassdlg: ontimer (uint nidevent)
{
Cpoint point;
: Getcursorpos (& Point );
Cwnd * w = cwnd: windowfrompoint (point );
Char sname [60];
Hwnd = NULL;
If (W! = NULL) hwnd = W-> getsafehwnd ();
If (hwnd! = NULL)
{
: Getclassname (hwnd, sname, 55 );
Cstring STR (sname );
M_strprompt = "This is not a password! (:-<";
If (STR = "edit" &: getwindowlong (hwnd, gwl_style) & es_password)
M_strprompt = "Haha, the password is found! (:-)";
: Sendmessage (
(Hwnd) hwnd, // handle to destination window
Wm_gettext, // message to send
(Wparam) 55, // number of characters to copy
(Lparam) sname // text Buffer
);
M_static_pass = sname;
}
Updatedata (false );
Cdialog: ontimer (nidevent );
}
Method 2: Obtain the input focus
The key to this method is to obtain the handle of the window with keyboard input focus:
1. Create a timer (interval of 2 seconds) in the dialog box and perform the following operations in the ontimer function:
2. in Windows 95, you can use: getforegroundwindow () to obtain the current window handle of the user;
3. Call: getwindowthreadprocessid (hwnd, null) to obtain the ID of the thread to which the window belongs;
4. Call: getcurrentthreadid () to obtain the ID of the current thread;
4. Call: attachthreadinput () to associate a thread with another thread. According to my feeling, in this function call, the order of two thread identifiers in the parameter table does not seem to matter;
5. Call: getfocus () to obtain the window handle with the keyboard input focus;
6. Call: getclassname () to obtain the Class Name of the window and determine whether the class name is "edit ". Call: getwindowlong (hwnd, gwl_style) to determine whether the window style is es_password, so as to determine whether it is a password;
7. If the window class name is "edit", call
: Sendmessage (
(Hwnd) hwnd, // handle to destination window
Wm_gettext, // message to send
(Wparam) 55, // number of characters to copy
(Lparam) sname // text Buffer
);
8. Call: attachthreadinput () to separate the thread from another thread.
If Windows 98 and Windows NT 4.0 sp3 are used, use: getguithreadinfo () to obtain information about the current active window in the system. For example: thread status, activity window handle, window handle with keyboard input focus, window handle that captures mouse input, window handle with activity menu, window handle that currently displays characters, etc..
Note: When using this function, it must contain winable. h file, and "# include <winable. h> "command should not be placed in stdafx .. H should be directly placed in the corresponding C file.
The sample code is long and is omitted here. See the appendix below.
Summary
The program implemented in the above method is short and concise, with a size of more than 20 KB. If you have any questions, please send me a letter. I think the first one is better because it is simple. The second method may be more useful in other programs. If you have any good ideas, you may wish to share them with others.
Appendix:
The following is the sample code of the ontimer function in the second method (it has been debugged ):
Void cgetpassdlg: ontimer (uint nidevent)
{
Char sname [60] = {0 };
// When used for Windows 98 or later environment:
# Ifdef in_win98_system
Guithreadinfo Info;
Info. cbsize = sizeof (Info );
Bool BL =: getguithreadinfo (0, & info );
If (BL & info. hwndfocus! = NULL)
{
: Getclassname (info. hwndfocus, sname, 55 );
Cstring STR (sname );
M_strprompt = "This is not a password! (:-<";
If (STR = "edit" &: getwindowlong (info. hwndfocus, gwl_style) & es_password)
M_strprompt = "Haha, the password is found! :-)";
: Sendmessage (
(Hwnd) info. hwndfocus, // handle to destination window
Wm_gettext, // message to send
(Wparam) 55, // number of characters to copy
(Lparam) sname // text Buffer
);
M_static_pass = sname;
}
# Else
// When used for Windows 95 Environment:
DWORD curid, WID;
Hwnd =: getforegroundwindow ();
If (hwnd! = NULL)
{
Curid =: getcurrentthreadid ();
WID =: getwindowthreadprocessid (hwnd, null );
: Attachthreadinput (
WID, // thread to attach
Curid, // thread to attach
True // attach or detach
);
Hwnd =: getfocus ();
If (hwnd! = NULL): getclassname (hwnd, sname, 55 );
Cstring STR (sname );
M_strprompt = "This is not a password! :-<";
If (STR = "edit" &: getwindowlong (hwnd, gwl_style) & es_password)
M_strprompt = "Haha, the password is found! :-)";
: Sendmessage (
(Hwnd) hwnd, // handle to destination window
Wm_gettext, // message to send
(Wparam) 55, // number of characters to copy
(Lparam) sname // text Buffer
);
M_static_pass = sname;
: Attachthreadinput (
WID, // thread to attach
Curid, // thread to attach
False // attach or detach
);
}
# Endif
Updatedata (false );
Cdialog: ontimer (nidevent );
}