a few function I often use when making auto-it like bots

Share Your Own Code Samples With the World

Moderators: g3nuin3, SpeedWing, WhiteHat, mezzo

a few function I often use when making auto-it like bots

Postby mezzo » Sat May 17, 2008 12:33 am

The moment you start making some 'automation' routines, you'll notice that you often reuse a set of routines.. these are some I use often.

Code: Select all
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// General purpose routines
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
struct point {
   int X;
   int Y;
};
/////////////////////////////////////////////////////////////////////////////////////////////
// Bring the window to the foreground
/////////////////////////////////////////////////////////////////////////////////////////////
int Foreground_window( char * windowname) {
    HWND hWnd = FindWindow( windowname );
    if ( !hWnd ) {
      PrintF( "Can't find window!" );
      return 0; }
    ShowWindowAsync( hWnd, SW_RESTORE );
    //ShowWindowAsync( hWnd, SW_MAXIMIZE );

    while ( GetForegroundWindow() != hWnd ) {
      SetForegroundWindow( hWnd );
      Sleep( 10 );}
   return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Check to see if a process is running
/////////////////////////////////////////////////////////////////////////////////////////////
bool check_is_running( char * binaryname) {

if ( StrICmp( GetCurProcessName(), binaryname ) != 0 ) {
        PrintF( "You must be attached!" );
        return 0;  }
PrintF( "You are now attached" );
return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Perform a left click
/////////////////////////////////////////////////////////////////////////////////////////////
void left_click() {
   MouseEvent( MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, 0, 0, 0 );
   Sleep(1);
   MouseEvent( MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, 0, 0, 0 );
   Sleep(1);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Move the mouse to a designated point
/////////////////////////////////////////////////////////////////////////////////////////////
void move_mouse(int pClickX, int pClickY) {
   
int iScreenW = GetPrimScreenWidth();
int iScreenH = GetPrimScreenHeight();

pClickX = pClickX * 65535.0f / iScreenW;
pClickY = pClickY * 65535.0f / iScreenH;

MouseEvent( MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, pClickX, pClickY, 0 );
Sleep(1);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// TYPE the command... simulate keystrokes
/////////////////////////////////////////////////////////////////////////////////////////////
void type_command (char * pChar)
{
   int counter;
   short letterke ;
   int iLength = StrLen( pChar );

   for ( counter = 0; counter < iLength ; counter++ ) {
      
      letterke = VkKeyScan( pChar[counter] );
      KeyboardEvent( letterke , 0 );
      KeyboardEvent( letterke  ,  KEYEVENTF_KEYUP );}
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Press ENTER
/////////////////////////////////////////////////////////////////////////////////////////////
void press_enter()
{
   KeyboardEvent( VK_RETURN , 0 );
   Sleep(250);
   KeyboardEvent( VK_RETURN  ,  KEYEVENTF_KEYUP );
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Wait a random time, parameters in seconds..
/////////////////////////////////////////////////////////////////////////////////////////////
void random_wait_sec(int minwait, int maxwait) {
   DWORD randomtime;

   DWORD tempvalue = maxwait - minwait;
   randomtime = Random( tempvalue );
   randomtime = randomtime + minwait;
   Sleep(randomtime * 1000);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Wait a random time, parameters in seconds..
/////////////////////////////////////////////////////////////////////////////////////////////
void random_wait_msec(int minwait, int maxwait) {
   DWORD randomtime;

   DWORD tempvalue = maxwait - minwait;
   randomtime = Random( tempvalue );
   randomtime = randomtime + minwait;
   Sleep(randomtime);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// generate random
/////////////////////////////////////////////////////////////////////////////////////////////
DWORD generate_random(int min, int max) {
   DWORD randomnumber;

   DWORD tempvalue = max - min;
   if ( tempvalue < 1 ) {
      PrintF("[ERROR] You messed up the parameters to generate_random!");
      return 0;
      }
   randomnumber = Random( tempvalue );
   randomnumber = randomnumber + min;
   return randomnumber;
   //Sleep(randomtime);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// randomize click
/////////////////////////////////////////////////////////////////////////////////////////////
void randomize_click(int lowX, int highX, int lowY, int highY, int maxsleepmseconds) {
int coordinateX = generate_random( lowX,highX );
int coordinateY = generate_random( lowY,highY );
if ( debugflag ) PrintF("[DEBUG] Moving Mouse to %u:%u.. ", coordinateX, coordinateY);
move_mouse( coordinateX, coordinateY );
random_wait_msec( 0, maxsleepmseconds );
left_click();
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Where do I click ??
/////////////////////////////////////////////////////////////////////////////////////////////
VOID wheredoiclick() {
long clickedX;
long clickedY;
POINT clickedhere;
PrintF("You have 5 seconds to position the mouse before I tell you the X/Y location.");
Sleep(5000);
GetCursorPos(&clickedhere);
PrintF("You clicked X: %u and Y: %u", clickedhere.x, clickedhere.y);
PrintF("move_mouse(%u,%u);", clickedhere.x, clickedhere.y);

// Some colour routines:
// Important note: change GWwindowname to whatever contains the window name, declare with ' char * windowname = "Minesweeper"; '
Foreground_window( GWwindowname );
HWND hTST = FindWindow( GWwindowname );
    if ( !hTST ) { return; }

HDC something = GetWindowDC( hTST);
int colors = GetPixel( something, clickedhere.x, clickedhere.y);
int redvalue = GetRValue(colors);
int greenvalue = GetGValue(colors);
int bluevalue = GetBValue(colors);
PrintF("Colour index of that pixel is %u.", colors);
PrintF("RedValue: %u GreenValue: %u BlueValue: %u", redvalue, greenvalue, bluevalue);
ReleaseDC( hTST, something);
}
- No thanks, I already have a penguin -
User avatar
mezzo
El Mariachi
 
Posts: 739
Joined: Mon Apr 30, 2007 10:27 pm
Location: Antwerp

Re: a few function I often use when making auto-it like bots

Postby chocokid » Fri Sep 12, 2008 10:42 am

mezzo wrote:The moment you start making some 'automation' routines, you'll notice that you often reuse a set of routines.. these are some I use often.

Code: Select all
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// General purpose routines
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
struct point {
   int X;
   int Y;
};
/////////////////////////////////////////////////////////////////////////////////////////////
// Bring the window to the foreground
/////////////////////////////////////////////////////////////////////////////////////////////
int Foreground_window( char * windowname) {
    HWND hWnd = FindWindow( windowname );
    if ( !hWnd ) {
      PrintF( "Can't find window!" );
      return 0; }
    ShowWindowAsync( hWnd, SW_RESTORE );
    //ShowWindowAsync( hWnd, SW_MAXIMIZE );

    while ( GetForegroundWindow() != hWnd ) {
      SetForegroundWindow( hWnd );
      Sleep( 10 );}
   return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Check to see if a process is running
/////////////////////////////////////////////////////////////////////////////////////////////
bool check_is_running( char * binaryname) {

if ( StrICmp( GetCurProcessName(), binaryname ) != 0 ) {
        PrintF( "You must be attached!" );
        return 0;  }
PrintF( "You are now attached" );
return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Perform a left click
/////////////////////////////////////////////////////////////////////////////////////////////
void left_click() {
   MouseEvent( MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, 0, 0, 0 );
   Sleep(1);
   MouseEvent( MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, 0, 0, 0 );
   Sleep(1);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Move the mouse to a designated point
/////////////////////////////////////////////////////////////////////////////////////////////
void move_mouse(int pClickX, int pClickY) {
   
int iScreenW = GetPrimScreenWidth();
int iScreenH = GetPrimScreenHeight();

pClickX = pClickX * 65535.0f / iScreenW;
pClickY = pClickY * 65535.0f / iScreenH;

MouseEvent( MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, pClickX, pClickY, 0 );
Sleep(1);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// TYPE the command... simulate keystrokes
/////////////////////////////////////////////////////////////////////////////////////////////
void type_command (char * pChar)
{
   int counter;
   short letterke ;
   int iLength = StrLen( pChar );

   for ( counter = 0; counter < iLength ; counter++ ) {
      
      letterke = VkKeyScan( pChar[counter] );
      KeyboardEvent( letterke , 0 );
      KeyboardEvent( letterke  ,  KEYEVENTF_KEYUP );}
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Press ENTER
/////////////////////////////////////////////////////////////////////////////////////////////
void press_enter()
{
   KeyboardEvent( VK_RETURN , 0 );
   Sleep(250);
   KeyboardEvent( VK_RETURN  ,  KEYEVENTF_KEYUP );
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Wait a random time, parameters in seconds..
/////////////////////////////////////////////////////////////////////////////////////////////
void random_wait_sec(int minwait, int maxwait) {
   DWORD randomtime;

   DWORD tempvalue = maxwait - minwait;
   randomtime = Random( tempvalue );
   randomtime = randomtime + minwait;
   Sleep(randomtime * 1000);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Wait a random time, parameters in seconds..
/////////////////////////////////////////////////////////////////////////////////////////////
void random_wait_msec(int minwait, int maxwait) {
   DWORD randomtime;

   DWORD tempvalue = maxwait - minwait;
   randomtime = Random( tempvalue );
   randomtime = randomtime + minwait;
   Sleep(randomtime);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// generate random
/////////////////////////////////////////////////////////////////////////////////////////////
DWORD generate_random(int min, int max) {
   DWORD randomnumber;

   DWORD tempvalue = max - min;
   if ( tempvalue < 1 ) {
      PrintF("[ERROR] You messed up the parameters to generate_random!");
      return 0;
      }
   randomnumber = Random( tempvalue );
   randomnumber = randomnumber + min;
   return randomnumber;
   //Sleep(randomtime);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// randomize click
/////////////////////////////////////////////////////////////////////////////////////////////
void randomize_click(int lowX, int highX, int lowY, int highY, int maxsleepmseconds) {
int coordinateX = generate_random( lowX,highX );
int coordinateY = generate_random( lowY,highY );
if ( debugflag ) PrintF("[DEBUG] Moving Mouse to %u:%u.. ", coordinateX, coordinateY);
move_mouse( coordinateX, coordinateY );
random_wait_msec( 0, maxsleepmseconds );
left_click();
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Where do I click ??
/////////////////////////////////////////////////////////////////////////////////////////////
VOID wheredoiclick() {
long clickedX;
long clickedY;
POINT clickedhere;
PrintF("You have 5 seconds to position the mouse before I tell you the X/Y location.");
Sleep(5000);
GetCursorPos(&clickedhere);
PrintF("You clicked X: %u and Y: %u", clickedhere.x, clickedhere.y);
PrintF("move_mouse(%u,%u);", clickedhere.x, clickedhere.y);

// Some colour routines:
// Important note: change GWwindowname to whatever contains the window name, declare with ' char * windowname = "Minesweeper"; '
Foreground_window( GWwindowname );
HWND hTST = FindWindow( GWwindowname );
    if ( !hTST ) { return; }

HDC something = GetWindowDC( hTST);
int colors = GetPixel( something, clickedhere.x, clickedhere.y);
int redvalue = GetRValue(colors);
int greenvalue = GetGValue(colors);
int bluevalue = GetBValue(colors);
PrintF("Colour index of that pixel is %u.", colors);
PrintF("RedValue: %u GreenValue: %u BlueValue: %u", redvalue, greenvalue, bluevalue);
ReleaseDC( hTST, something);
}

how can i use it dude??
User avatar
chocokid
Hackleberry Fin
 
Posts: 20
Joined: Fri Sep 12, 2008 10:23 am
Location: 7th-HeLL


Return to Code Submissions

Who is online

Users browsing this forum: No registered users and 0 guests