C # Call Windows api c # create a simple game plug-in (using Warcraft III as an example)

Source: Internet
Author: User
Tags dota
C # simple game plug-in (taking Warcraft III as an example)

There are many plug-in production tutorials on the internet, mostly for large-scale online games, including packet capture, disassembly, and C ++ knowledge synthesis. The same is true. Common plug-ins are written in VC ++ and have never been written in C # or other. NET languages.

As a loyal fan of Microsoft. NET technology, this is a pity. However, it doesn't matter. Here we will teach you two Tricks: free of tuition. :)

In fact, as a game plug-in, there are three main functions: Simulating keyboard operations, simulating mouse operations, and modifying memory data. It is difficult to modify memory data, but it is easy to simulate the mouse and keyboard. Many plug-ins of popular games can only be achieved through the simulation of the mouse and keyboard, such as: dance troupe, QQ speed, continuous viewing, various types of web games, as well as various large online games in the automatic fight, automatic medication and so on.

Warcraft III, the title of the frozen throne of Warcraft, also known as World of Warcraft (war3), swept the world in the last year 67 s. In the last two years, war3 has set off a new upsurge in Dota in China.

This article describes how to use C # To create game plug-ins by taking explicit blood and key-changing plug-ins in Dota games as an example.

The final interface is as follows:

In this example, two functions are available: explicit blood and changing the Q key to the 7 key of the keypad. Those who play war3 know that these two functions are very important to war3 (especially Dota.

First, let's briefly introduce how plug-ins simulate the keyboard.

Plug-ins and game programs are two different processes. Plug-ins use APIs provided by windows to find the game program process and set keyboard hooks (what is a hook? You don't know, but Baidu Knows .) After the hook is set, we monitor the keys of the users in the game process and process them as needed to complete some simulated keyboard operations.

After learning about this process, we can start to sort out our ideas. The following four steps are required to complete the plug-in:

1. Declare functions and constants in Windows APIs

// Keyboard hook Structure Function [structlayout (layoutkind. sequential)] public class keyboardhookstruct {public int vkcode; Public int scancode; Public int flags; Public int time; Public int dwextrainfo ;} # region dllimport // set hook [dllimport ("user32.dll", charset = charset. auto, callingconvention = callingconvention. stdcall)] public static extern int setwindowshookex (INT idhook, hookproc lpfn, intptr hinstance, int threadid); [dllimport ("user32.dll", charset = charset. auto, callingconvention = callingconvention. stdcall)] // remove the hook public static extern bool unhookwindowshookex (INT idhook); [dllimport ("user32.dll", charset = charset. auto, callingconvention = callingconvention. stdcall)] // call the next hook public static extern int callnexthookex (INT idhook, int ncode, intptr wparam, intptr lparam); // obtain the module handle [dllimport ("kernel32.dll ", charset = charset. auto, callingconvention = callingconvention. stdcall)] Private Static extern intptr getmodulehandle (string lpmodulename); // find the target process window
[Dllimport ("user32.dll")] public static extern intptr findwindow (string lpclassname, string lpwindowname); // set the process window to the beginning
[Dllimport ("user32.dll")] public static extern bool setforegroundwindow (intptr hwnd); // simulate a keyboard event
        [DllImport("User32.dll")]        public static extern void keybd_event(Byte bVk, Byte bScan, Int32 dwFlags, Int32 dwExtraInfo);

// Release the key constant private const int keyeventf_keyup = 2;
In this example, fewer functions are used, all of which are in the system's user32.dll, including setting and canceling hooks, calling the next hook, importing processes, and simulating keyboards. Import them in sequence.
The naming rules of these functions are reasonable, and the functions can be known almost only according to the function name.
If you are not familiar with some of the functions, search for msdn.

Ii. Use Windows API to set hooks

With the above Windows API function declaration, the next step is to set the hook.

Only a few lines of code, but it contains quite a wealth of content.

// Delegate Public Delegate int hookproc (INT ncode, intptr wparam, intptr lparam );
 

Public void hook_start () {// install the keyboard hook if (hhook = 0) {handler = new hookproc (keyboardhookproc); hhook = setwindowshookex (wh_keyboard_ll, handler, getmodulehandle (process. getcurrentprocess (). mainmodule. modulename), 0 );}}
 
First, we will introduce setwindowshookex, the star function for setting hooks. Its parameters are described as follows.

Setwindowshookex (
Idhook: integer; {hook type}
Lpfn: tfnhookproc; {function pointer}
Hmod: hinst; {handle of the module (exe, DLL) that contains the hook function; generally hinstance; it can be 0 if it is the current thread}
Dwthreadid: DWORD {associated thread; Use getcurrentthreadid to obtain the current thread; 0 indicates system level hook}
): Hhook; {return the hook handle; 0 indicates failure}

 

Note the lpfn parameter. The above explanation is "function pointer ". In C #, pointers cannot be used directly, let alone function pointers. We can use the delegate in C # To implement the function pointer function.

In the above Code, we define a delegate keyboardhookprocedure = new hookproc (keyboardhookproc) that processes the Keyboard Message function, and pass it as a parameter to setwindowshookex. Keyboardhookproc is the specific function to be delegated.

 

3. Monitor user operations

 After setting the hook, we can write the code that monitors user operations and simulates the keyboard in the delegate function.

Public static int keyboardhookproc (INT ncode, intptr wparam, intptr lparam) {// monitors user keyboard input
Keyboardhookstruct input = (keyboardhookstruct) Marshal. ptrtostructure (lparam, typeof (keyboardhookstruct); // intercept the Home Key
If (input. vkcode = (INT) keys. Home) {// write other operation logic here
} // Continue to execute the next hook program return callnexthookex (hhook, ncode, wparam, lparam );}

 

4. Simulate keyboard operations as needed

Explicit blood function:We all know that there are three blood display shortcuts provided by war3. The ALT key displays the life of all units. [The Key displays the life of a friend unit,] The key displays the life of a local unit. What plug-ins need to do is to simulate that they keep pressing a key. Since the Alt key and many other keys constitute a combination, we cannot simulate long-pressing ALT, otherwise it will affect normal games. Our solution should be to simulate a long press of the [key and] key. The Code is as follows:

// Obtain the handle intptr wchandle = findwindow (null, "Warcraft III") of the Warcraft program; // If the hook is valid if (wchandle! = Intptr. Zero) {// set the game window to the top setforegroundwindow (wchandle );
Byte vk_num1 = 219; // [key code on the keyboard. Click [to display the user unit life value. Byte vk_num2 = 221; // code of the] key on the keyboard. Press] to display the life of the enemy unit. Keybd_event (vk_num1, 0, 0, 0); // long press [keybd_event (vk_num2, 0, 0, 0); // long press]

               }

Key change:The shortcut keys on the keypad (numpad) are inconvenient to press, so many players prefer to change the keys on the keypad to the letter and keyboard on the left. Anyone playing Dota knows that there is no hero's skill to use the shortcut key "Q" (the summoner has a ball that is "Q" (not a skill )). Therefore, changing the 7 key on the keypad to Q will not cause any conflict. The method is also very simple: if the user is monitored to press the "Q" key, it is like the game process sends the "7" key on the keypad. The Code is as follows:

// If the user presses the Q key if (input. vkcode = (INT) keys. q) {// get the handle of the Warcraft program intptr wchandle = findwindow (null, "Warcraft III"); // If the hook is valid if (wchandle! = Intptr. zero) {// set the game window to the top setforegroundwindow (wchandle); byte vk_q = (byte) keys. numpad7; keybd_event (vk_q, 0, 0, 0); // press keypad 7 keybd_event (vk_q, 0, keyeventf_keyup, 0); // release keypad 7} return 1 ;}

Now we have finished introducing the plug-in of the simulated keyboard. The simulated mouse is very similar. Please try again. This article is only intended for discussion. You are welcome to discuss the blog of streaming Trojan Horse.

Reprinted address:
Http://www.cnblogs.com/azure/archive/2009/07/03/1515967.html

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.