[C++] How to make a simple bot

On domingo, 12 de septiembre de 2010 0 comentarios

In this tutorial I will explain to you how to program a bot. I will not post enough of the source code for you to make a copy pasta bot.

First, you will need to include the windows.h header file.

Syntax « c » : [ Download ] [ Hide ] [ Expand ]
#include //This is what we need for the SendInput functions
int main()
{
     return 0;
}

The first function we can use to simulate keystrokes or mouse functions is SendInput().
Quote:
SendInput - Synthesizes keystrokes, mouse motions, and button clicks.
The SendInput function requires the use of the INPUT object.
Quote:
INPUT - Used by SendInput to store information for synthesizing input events such as keystrokes, mouse movement, and mouse clicks.
. So let us declare an INPUT object and have it setup for a mouse click.
Syntax « c » : [ Download ] [ Hide ] [ Expand ]
INPUT Input;
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
Input.mi.dx = X * (65535.0f / (GetSystemMetrics(SM_CXSCREEN) -1));
Input.mi.dy = Y * (65535.0f / (GetSystemMetrics(SM_CYSCREEN) -1));

If you just put this code in and compile + debug it, nothing will happen because we did not pass this through SendInput yet. Before I explain how to use SendInput, I will explain what this code above means. Since we set the input type to INPUT_MOUSE, we fill in Input.mi.dwFlags with what we want the mouse to do. In this case, we want to move the mouse. That is why we used MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE.
Quote:
MOUSEEVENTF_ABSOLUTE - Contains information about a simulated mouse event.
Next we filled in the X and Y position of where we want the mouse to land.

Now we can move on to SendInput.
Syntax « c » : [ Download ] [ Hide ]
 SendInput(true, &Input, sizeof(Input));

After this piece of code, you will notice the mouse does move. You should know where this goes.

Try to make the mouse click now. You should used MOUSEEVENTF_LEFTDOWN and MOUSEEVENTF_LEFTUP. You also need to call SendInput twice.
Syntax « c » : [ Download ] [ Hide ] [ Expand ]
INPUT Input;
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;     //Clicks the mouse down.
SendInput(true, &Input, sizeof(Input));
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;       //Releases the mouse clock.
SendInput(true, &Input, sizeof(Input));


Here is a keystroke example with the keyboard input.
Syntax « c » : [ Download ] [ Hide ]
INPUT Input;
Input.type = INPUT_KEYBOARD;
Input.ki.dwFlags = /* Figure it out yourself */
Input.ki.wVk = Virtual Key Codes;

Virtual Key Codes Table
The hint here is that it is very similar to a mouse click but, you use this KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP.
Syntax « c » : [ Download ] [ Hide ] [ Expand ]
Input.ki.wvK = 'A';
Input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
SendInput(true, &Input, sizeof(Input));
Input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(true, &Input, sizeof(Input));

Now you need to turn it on and off. The smart way to do this is create a thread and have it watch for the hotkeys and then create another thread to simulate those keystrokes. For this we need the CreateThread function.
Syntax « c » : [ Download ] [ Hide ] [ Expand ]
int main()
{
     DWORD dwHandleHotKeys;
     CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)HandleHotKeys, NULL, NULL, &dwHandleHotKeys);
     //On Exit
     ExitThread(dwHandleHotKeys);
}

void HandleHotKeys()
{
     //Create a loop to look for hotkeys.
}

Before I close off this tutorial, I should tell you that this is probably blocked by MANY anti-Tool applications. And for use the Sleep() function for delays between keystrokes.
Author: ヘ(^_^ヘ)(ノ^_^)ノ

0 comentarios:

Publicar un comentario