Original Design:
For the sake of security, the company wants to implement an automatic login module because it does not let too many people know the password.
Design Philosophy:
Mainly by calling Windows
In the API, find the target window and process, and then automatically enter the username and password saved in the database in the input box, and log on.
Design steps:
1. Call windows
API.
C # use the following method to call a Windows API:
1. Introduce namespace: Using
System. runtime. interopservices;
2. reference the method to be used. Format: [dllimp
ORT ("DLL file")] method declaration;
[Dllimport ("user32.dll")] private
Static extern bool showwindow (intptr hwnd, int
Ncmdshow );
[Dllimport ("user32.dll")] Private Static extern bool
Setforegroundwindow (intptr
Hwnd );
[Dllimport ("user32.dll")] Private Static extern intptr
Findwindow (string lpclassname, string
Lpwindowname );
[Dllimport ("user32.dll")] Private Static extern int
Sendmessage (intptr hwnd, int MSG, int wparam, int
Lparam );
[Dllimport ("user32.dll")] Private Static extern bool
Setcursorpos (int x, int y );
[Dllimport ("user32.dll")] private
Static extern void mouse_event (INT dwflags, int dx, int dy, int dwdata, int
Dwextrainfo );
[Dllimport ("user32.dll")] Private Static extern void
Keybd_event (byte bvk, byte bscan, uint dwflags, uint
Dwextrainfo );
[Dllimport ("user32.dll")] Private Static extern bool
Setwindowpos (intptr hwnd, intptr hwndlnsertafter, int X, int y, int CX, int cy,
Uint flags );
// Showwindow Parameters
Private const int sw_shownormal =
1;
Private const int sw_restore = 9;
Private const int sw_shownoactivate
= 4;
// Sendmessage Parameters
Private const int wm_keydown = 0x100;
Private
Const int wm_keyup = 0x101;
Private const int wm_syschar =
0x106;
Private const int wm_syskeyup = 0x105;
Private const int
Wm_syskeydown = 0x104;
Private const int wm_char =
0x102;
2. Find the target window
1) obtain the Handle Based on the window title
Intptr myintptr =
Findwindow (null, "window name"); // null is the class name, which can be obtained using spy ++ or empty
Showwindow (myintptr,
Sw_restore); // restore the window
Setforegroundwindow (myintptr );
// If no showwindow exists, this method cannot set a minimized window.
2) traverse all windows to get the handle
1
Define the delegate method callback, enumerate the window API (enumwindows), get the window name API (getwindowtextw), and get the window class name API (getclassnamew)
Public
Delegate bool callback (INT hwnd, int
Lparam );
[Dllimport ("USER32")] public static extern int
Enumwindows (callback X, int y );
[Dllimport ("user32.dll")] private
Static extern int getwindowtextw (intptr hwnd,
[Financialas (unmanagedtype. lpwstr)] stringbuilder lpstring, int
Nmaxcount );
[Dllimport ("user32.dll")] Private Static extern int
Getclassnamew (intptr hwnd, [financialas (unmanagedtype. lpwstr)] stringbuilder
Lpstring, int nmaxcount );
2. Call enumwindows to traverse the window
Callback mycallback = new
Callback (recall );
Enumwindows (mycallback, 0 );
3. Callback method recall
Public
Bool recall (INT hwnd, int lparam)
{
Stringbuilder sb = new
Stringbuilder (256 );
Intptr PW = new
Intptr (hwnd );
Getwindowtextw (PW, Sb, SB. capacity );
// Obtain the window name and save it in strname
String strname =
SB. tostring ();
Getclassnamew (PW, Sb, SB. capacity );
// Obtain the window class name and save it in strclass
String strclass = sb. tostring ();
If
(Strname. indexof ("window name keyword")> = 0 & strclass. indexof ("Class Name keyword")> =
0)
{
Return false;
// Return false to stop enumwindows Traversal
}
Else
{
Return true;
// Return true to continue enumwindows Traversal
}
}
3) open the window to get the handle
1
Define setactivewindow and setforegroundwindow)
[Dllimport ("user32.dll")] Static
Extern intptr setactivewindow (intptr
Hwnd );
[Dllimport ("user32.dll")] [Return:
Externalas (unmanagedtype. bool)] Static extern bool setforegroundwindow (intptr
Hwnd );
2 open a window
Process proc =
Process. Start (@ "Target Program path ");
Setactivewindow (Proc. main1_whandle );
Setforegroundwindow (Proc. main1_whandle );
3. input data to the specified window
1
Send data to the window using the sendmessage API
Inputstr (myintptr, _ gameid );
// Enter the game ID
Sendmessage (myintptr, wm_syskeydown, 0x09, 0 );
// Enter the tab (0x09)
Sendmessage (myintptr, wm_syskeyup, 0x09,
0 );
Inputstr (myintptr, _ gamepass); // enter the game Password
Sendmessage (myintptr,
Wm_syskeydown, 0x0d, 0); // enter (0x0d)
Sendmessage (myintptr, wm_syskeyup,
0x0d, 0 );
/// <Summary>
/// Send a string
///
</Summary>
/// <Param name = "myintptr"> window handle </param>
///
<Param name = "input"> string </param>
Public void inputstr (intptr
Myintptr, string input)
{
Byte [] CH =
(Asciiencoding. ASCII. getbytes (input ));
For (INT I = 0; I <Ch. length;
I ++)
{
Sendmessage (PW, wm_char, CH, 0 );
}
}
2
Use the mouse and keyboard to simulate sending data to the window
Setwindowpos (PW, (intptr) (-1), 0, 0, 0, 0, 0x0040 |
0x0001); // set the window position
Setcursorpos (476,177); // sets the mouse position
Mouse_event (0x0002,
0, 0, 0, 0); // simulate the mouse operation
Mouse_event (0x0004, 0, 0, 0, 0 );
// Simulate the mouse Release Operation
Sendkeys. Send (_ gameid );
// Enter the game ID on the simulated keyboard
Sendkeys. Send ("{tab }");
// Enter a tab on the simulated keyboard
Sendkeys. Send (_ gamepass );
// Enter the game password on the simulated keyboard
Sendkeys. Send ("{enter }");
// Enter on the simulated keyboard
In addition, the keybd_event method is also mentioned above. Its usage is similar to that of the mouse_event method, and its function is the same as that of sendkeys. Send.