C # simple game plug-in (taking Warcraft III as an example)

Source: Internet
Author: User
Tags dota

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 plug-ins.ProgramSimulate the keyboard principle.

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 the 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 externIntptrFindwindow (StringLpclassname,StringLpwindowname );// Set the process window to the beginning
[Dllimport("User32.dll")]Public static extern boolSetforegroundwindow (IntptrHwnd );// Simulate Keyboard Events
 
[Dllimport("User32.dll")]Public static extern voidKeybd_event (ByteBvk,ByteBscan,Int32Dwflags,Int32Dwextrainfo );

// Release the constant of the buttonPrivate const intKeyeventf_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.

Just two linesCode.

 
// DelegatePublic Delegate intHookproc(IntNcode,IntptrWparam,IntptrLparam );
 
 

Public voidHook_start (){// Install the keyboard hookIf(Hhook = 0) {keyboardhookprocedure =NewHookproc(Keyboardhookproc); hhook = setwindowshookex (wh_keyboard_ll, keyboardhookprocedure, 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 = to process the Keyboard Message function.NewHookproc(Keyboardhookproc) 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 intKeyboardhookproc (IntNcode,IntptrWparam,IntptrLparam ){// Monitor user keyboard input
 
KeyboardhookstructInput = (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 ProgramReturnCallnexthookex (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:

  // get the Warcraft program handle   intptr  wchandle = findwindow ( null , " Warcraft III ");  // If the hook is valid   If  (wchandle! =  intptr . zero) { // set the game window to the beginning  setforegroundwindow (wchandle); 
ByteVk_num1 = 219;// [Key code on the keyboard. Click [to display the user unit life value.ByteVk_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 );// 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 ){ // Obtain 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 beginning 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.

 

Attachment: Download of plug-in finished products (running in. NET 2.0 or above environments)

References: proficient in. Net interoperability. I would like to thank the authors Huang Jizhou and Cui xiaoyuan for their gifts. I finally learned how to use them ~ :)

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.