C # Calling the Windows API sample

Source: Internet
Author: User

One, callWindowsApi.
The Windows API methods for C # downgrade are as follows:
1. Introduction of namespaces: Using System.Runtime.InteropServices;
2, the reference needs to use the method, format: [DllImport ("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;

Second, find the target window
1), the handle is obtained according to the title of the window
IntPtr myintptr = FindWindow (null, "window name"); Null is the class name, can be obtained with Spy + +, or it can be empty
ShowWindow (Myintptr, Sw_restore); Restore a window
SetForegroundWindow (MYINTPTR); If there is no ShowWindow, this method cannot set the minimized window
2), traverse all windows to get handle
1 Define delegate method callback, Enumerate window API (EnumWindows), Get Window name API (GETWINDOWTEXTW) and Get 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, [MarshalAs (UNMANAGEDTYPE.LPWSTR)] StringBuilder lpstring, int nmaxcount);
[DllImport ("User32.dll")]private static extern int Getclassnamew (IntPtr hWnd, [MarshalAs (UNMANAGEDTYPE.LPWSTR)] StringBuilder lpstring, int nmaxcount);
2 Calling EnumWindows Traversal 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); Get the window name and save it in strname
String strName = sb. ToString ();

Getclassnamew (PW,SB,SB. capacity); Get 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; Returns false aborts EnumWindows traversal
}
Else
{
return true; Returns true to continue enumwindows traversal
}
}
3), open the window to get the handle
1 Define Settings active window API (SetActiveWindow), set foreground window API (SetForegroundWindow)
[DllImport ("user32.dll")]static extern IntPtr SetActiveWindow (IntPtr hWnd);
[DllImport ("User32.dll")] [Return:marshalas (Unmanagedtype.bool)]static extern Bool SetForegroundWindow (IntPtr hWnd);
2 Open Window
Process proc = Process.Start (@ "target program path");
SetActiveWindow (Proc. Mainwindowhandle);
SetForegroundWindow (Proc. Mainwindowhandle);

Iii. entering data into a specified window
1 sending data to a window using the Send Message API (SendMessage)
Inputstr (Myintptr, _gameid); Enter the game ID
SendMessage (Myintptr, Wm_syskeydown, 0X09, 0); Enter tab (0X09)
SendMessage (Myintptr, Wm_syskeyup, 0X09, 0);
Inputstr (Myintptr, _gamepass); Enter the game password
SendMessage (Myintptr, Wm_syskeydown, 0X0D, 0); Enter Enter (0x0d)
SendMessage (Myintptr, Wm_syskeyup, 0X0D, 0);

<summary>
Send a string
</summary>
<param name= "myintptr" > Window handles </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 using mouse and keyboard to simulate sending data to a window
SetWindowPos (PW, (INTPTR) ( -1), 0, 0, 0, 0, 0x0040 | 0x0001); Set Window Position
Setcursorpos (476, 177); Setting the mouse position
Mouse_event (0x0002, 0, 0, 0, 0); Simulate mouse down action
Mouse_event (0x0004, 0, 0, 0, 0); Simulate Mouse release action
Sendkeys.send (_gameid); Analog keyboard input Game ID
Sendkeys.send ("{tab}"); Analog Keyboard Input Tab
Sendkeys.send (_gamepass); Analog keyboard Input Game password
Sendkeys.send ("{ENTER}"); Analog keyboard Input Enter

Another: The Keybd_event method is mentioned above, the usage and the Mouse_event method are similar, the function and the Sendkeys.send same.

C # Calling the Windows API sample

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.