c++: hooking game

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

c++: hooking game

Post by L0DER on Thu Jul 24, 2008 8:05 am
([msg=8139]see c++: hooking game[/msg])

I have a game called Mahjongg, basically it a game that have tiles and you have to match them.
So I want to try to make a bot that will match the tiles himself.
I need to hook the location of the tiles on the screen and make a loop for the matching thing.
The game for windows and probably doesn't use directX just the windows API but I'm not sure about it.
How do I hook it? is there build function in windows?

**another c/c++ related question: why simple application like this
Code: Select all
#include<iostream>
using namespace std;
int main(){
int c;
cout <<"For asdfsdfsdfasd asdf ?\n";
cin >> c;
c = c * 1000;
return 0;
}

Is wights 463kb
and
Code: Select all
#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

a lot less(20kb)?
Last edited by L0DER on Thu Jul 24, 2008 10:25 am, edited 1 time in total.
L0DER
New User
New User
 
Posts: 26
Joined: Tue Apr 22, 2008 12:58 am
Blog: View Blog (0)


Re: c++: hooking game

Post by nathandelane on Thu Jul 24, 2008 10:16 am
([msg=8153]see Re: c++: hooking game[/msg])

The easiest methods to hook into an application like a game (in Windows) include COM, OLE, and Windows API functions. However since it's a graphical matching game, you'll likely need to build a screen reader as well in order to match the graphics. Though these are the easiest methods for doing this, in many cases they are far from easy, and if you don't already have the API memorized, you're going to need to look up a lot of info and digest it, as well as play around until you find a solution. One tool I might suggest you try is called AutoHotKey. It is basically an automation tool, though it also hooks into Windows as a hot-key device. Read the documentation and use the object spy that comes with it. You can use it to write scripts. The source code is also available on the site for free so you could look into that as well.
Me, Nathandelane, Highly influential to Hackerdom, Premature Optimization=http://c2.com/cgi/wiki?PrematureOptimization
User avatar
nathandelane
Poster
Poster
 
Posts: 204
Joined: Thu Jun 26, 2008 11:26 am
Location: Utah
Blog: View Blog (0)


Re: c++: hooking game

Post by L0DER on Thu Jul 24, 2008 12:01 pm
([msg=8161]see Re: c++: hooking game[/msg])

thanks for the attention.
I took a look at the maps of the game and they are surprisingly easy it like big 2D array
Code: Select all
=========================================
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----*-*-*-*-------------*-*-*-*---------
-----------------------------------------
-----*-*-*-*-------------*-*-*-*---------
-----------------------------------------
-----*-*-*-*-------------*-*-*-*---------
-----------------------------------------
-----*-*-*-*-------------*-*-*-*---------
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----------------------------------------
=========================================

Every * represents a tile.

However since it's a graphical matching game, you'll likely need to build a screen reader as well in order to match the graphics.

I think you don't need to match by the graphics.
Now what about the memory the maps files set only the location of the tiles, the tiles themselves are random.
Shouldn't the exist somewhere in the memory?
Can you give me some information about those three (include COM, OLE, and Windows API functions)


**another c/c++ related question: why simple application like this

Code: Select all
#include<iostream>
using namespace std;
int main(){
int c;
cout <<"For asdfsdfsdfasd asdf ?\n";
cin >> c;
c = c * 1000;
return 0;
}


Is wights 463kb
and

Code: Select all
#include <windows.h>

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */

/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);

/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}


/* This function is called by the Windows function DispatchMessage() */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}


a lot less(20kb)?
L0DER
New User
New User
 
Posts: 26
Joined: Tue Apr 22, 2008 12:58 am
Blog: View Blog (0)


Re: c++: hooking game

Post by nathandelane on Thu Jul 24, 2008 12:58 pm
([msg=8167]see Re: c++: hooking game[/msg])

I can't answer you COM, OLE, Windows API question just yet, but I can answer your question about code. When you are using the iostream functions cin and cout (which are actually C++ objects), that code must be statically compiled, because iostream is a special library that is part of C++, not the operating system. With your Windows program, that code uses external DLL files for the Windows API calls to create the window, so naturally because it dynamically links to a part of the operating system, it is smaller in size, ultimately just a few API calls. Now you can compile with optimizations either for size or speed. With g++ and gcc (C only compiler) you can use the -Ox switch where x is an optional number 1 to 4, where you can optimize for speed or size, but they don't give you much. In general you will always see that object-oriented code is larger than structured code once compiled, but also you should always see that dynamically linked code is smaller than statically linked code.

With -O2 I was able to reduce your compiled code from 488,653 b to 488,000 b.
Me, Nathandelane, Highly influential to Hackerdom, Premature Optimization=http://c2.com/cgi/wiki?PrematureOptimization
User avatar
nathandelane
Poster
Poster
 
Posts: 204
Joined: Thu Jun 26, 2008 11:26 am
Location: Utah
Blog: View Blog (0)


Re: c++: hooking game

Post by L0DER on Thu Jul 24, 2008 1:41 pm
([msg=8175]see Re: c++: hooking game[/msg])

653 byte isn't much...
The iostream has in it more than cout and cin right, can I create new header and expel the unneeded once?
L0DER
New User
New User
 
Posts: 26
Joined: Tue Apr 22, 2008 12:58 am
Blog: View Blog (0)


Re: c++: hooking game

Post by codz30 on Fri Jul 25, 2008 3:55 am
([msg=8233]see Re: c++: hooking game[/msg])

The reason of the size difference is because when you #include <iostream> you link your .exe to the iostream library, making its bigger.
the <windows.h> is much smaller because of its adaptation to the windows OS.
btw, C is much better :)
codz30
New User
New User
 
Posts: 2
Joined: Fri Jul 25, 2008 3:43 am
Blog: View Blog (0)


Re: c++: hooking game

Post by nathandelane on Fri Jul 25, 2008 10:23 am
([msg=8245]see Re: c++: hooking game[/msg])

codz30 wrote:btw, C is much better :)


This is a matter of opinion, which should be construed as such. Some languages are better for individual purposes more so than the idea that one language is better overall. C does not provide some of the constructs that enable programmers to more quickly solve a problem. Also the creator of C++, Bjarne Stroustrup, stated that "C++ is a better C". Chapter and verse my friend. I've never heard any source to the idea that C is overall better. This kind of thing should be viewed as very subjective in any case, i.e. Java/Python/Ruby/etc. is better is not fact, unless it is qualified by in what ways and when. It's simply not helpful.
Me, Nathandelane, Highly influential to Hackerdom, Premature Optimization=http://c2.com/cgi/wiki?PrematureOptimization
User avatar
nathandelane
Poster
Poster
 
Posts: 204
Joined: Thu Jun 26, 2008 11:26 am
Location: Utah
Blog: View Blog (0)


Re: c++: hooking game

Post by L0DER on Fri Jul 25, 2008 1:53 pm
([msg=8268]see Re: c++: hooking game[/msg])

Do you think i can access the array and find there the symbol that represents the tile ?
Can I reduce Iostream's weight by deleting commands that aren't needed ?
L0DER
New User
New User
 
Posts: 26
Joined: Tue Apr 22, 2008 12:58 am
Blog: View Blog (0)


Re: c++: hooking game

Post by L0DER on Sun Jul 27, 2008 6:16 am
([msg=8393]see Re: c++: hooking game[/msg])

I tried art-money, and i was able to change the score.
In the game you have pearls with them you can get hint and undo and and mix, I tried to find it and change it, but I wasn't sure that it was the right one because when i changed it, it just return the previous value.
then I tried another game track-mania, this time I found the value of the km that i drove but i couldn't change it(it change to value, but a second later the previous value was reloaded ). what could be the problem maybe i found the pointer or something like that
L0DER
New User
New User
 
Posts: 26
Joined: Tue Apr 22, 2008 12:58 am
Blog: View Blog (0)


Re: c++: hooking game

Post by BhaaL on Sun Jul 27, 2008 11:04 am
([msg=8403]see Re: c++: hooking game[/msg])

You may want to look into ReadProcessMemory and WriteProcessMemory, once you found a good address.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron