Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


C/C++ Auto Click using PostMessageA + 5 Tutorial

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
4ng3licDew
Cheater
Reputation: 0

Joined: 14 Feb 2008
Posts: 28

PostPosted: Sat Feb 23, 2008 10:35 am    Post subject: C/C++ Auto Click using PostMessageA + 5 Tutorial Reply with quote

Hi everyone,

First I would like to thanks everyone who had contributed at Cheat Engine Forum. As my way of saying thank you I am going to show you how to write your own auto click program.

I have created an AutoClick program for MapleStory Global, using Microsoft Visual Studio C++ 6.0 and win32.

This program generates 'control' key down events, 'z' key down events, 't' key down events, and left mouse double click events.

To better understand what I am about to discuss here you will need:
1. to know C/C++
2. to have a basic understanding of how to create a Win32 program. For a tutorial on Win32 Fundamental please google "win32 tutorial" (Sorry I can't post a link yet).


Here is what I did:

1. Create my own PostMessageA function, using a well known technique PostMessage + 5 that has been discussed here at Cheat Engine Forum. You will need to load user32 DLL at start up. Then get a function pointer to PostMessageA and add 5 bytes to this function pointer.

Code:

HINSTANCE hInst; // Instance of user32 DLL
DWORD DLLFunc; // PostMessageA + 5
.
.
.
// PostMessageA + 5 bypass
__declspec(naked) BOOL WINAPI myPostMessageA(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
   __asm
   {
      mov  edi, edi
      push ebp
      mov  ebp, esp
      jmp dword ptr ds:[DLLFunc]
   }
}
.
.
.
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
            WPARAM wParam, LPARAM lParam)
.
.
.
         case WM_CREATE:

            m_hInst = LoadLibrary("user32.dll");
            DLLFunc = NULL;
            if (m_hInst != NULL) {
               DLLFunc = (DWORD)GetProcAddress(m_hInst, "PostMessageA") + 5;
            }
.
.
.



2. To unload user32 DLL when AutoClick terminates.

Code:

.
.
.
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
            WPARAM wParam, LPARAM lParam)
.
.
.
         case WM_DESTROY:

            if (m_hInst != NULL) {
               // Un-Load DLL
               ::FreeLibrary(m_hInst);
               m_hInst = NULL;
            }
.
.
.


3. To generate a 'Control' key down event or a mouse double click event.

Code:

HWND m_cHandle; // Windows handle to MapleStory
.
.
.
void SendKey(UINT vk_key) {
   UINT scancode;
   LPARAM lparam;

   // Get window handle on MapleStory
   m_cHandle = ::FindWindow("MapleStoryClass", NULL);

   if ((m_cHandle != NULL) && (DLLFunc != NULL)) {

      // map virtual key code to scan code
      scancode = MapVirtualKey(VK_CONTROL, 0);

      // The scancode value is in the low 16 bits
      // need to shift it to the left 16 bits.
      // + 1 is the repeat count
      lparam = (scancode << 16) + 1;

      myPostMessageA(cHandle, WM_KEYDOWN, NULL, lparam);
      myPostMessageA(cHandle, WM_KEYUP, NULL, lparam);

      // This call will only generate key press in textboxes
      //myPostMessageA(cHandle, WM_KEYDOWN, vk_key, NULL);
      //myPostMessageA(cHandle, WM_KEYUP, vk_key, NULL);

      // This call will generate key press in both textboxes
      // and the graphic screen.
      //myPostMessageA(cHandle, WM_KEYDOWN, vk_key, lparam);
      //myPostMessageA(cHandle, WM_KEYUP, vk_key, NULL);
   }
}

// Send left mouse button double click
void SendDoubleClick(void) {

   if (cHandle == NULL) {
      // Get window handle on MapleStory
      cHandle = FindWindow("MapleStoryClass", NULL);
   }

   if ((cHandle != NULL) && (DLLFunc != NULL)) {
      
      // Get mouse cursor position
      GetCursorPos(&Pos);

      myPostMessageA(cHandle, WM_LBUTTONDOWN, NULL, (LPARAM)&Pos);
      myPostMessageA(cHandle, WM_LBUTTONUP, NULL, (LPARAM)&Pos);
      myPostMessageA(cHandle, WM_LBUTTONDBLCLK, NULL, (LPARAM)&Pos);
      myPostMessageA(cHandle, WM_LBUTTONUP, NULL, (LPARAM)&Pos);
   }      
}               
.
.
.
// generate a 'Control' key down event
SendKey(VK_CONTROL);

// generate left mouse double click event
SendDoubleClick();
.
.
.


4. To Add hot key Control + F9 to toggle auto click on or off. You need to register the hot key at start up, and unregister the hot key when the program terminates. Then add an event handler for when the hot key is pressed. This event handler will toggle the auto click on or off.

Code:

UINT m_nIDHotKey1; // Hot key identifier
.
.
.
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
            WPARAM wParam, LPARAM lParam)
.
.
.
         case WM_CREATE:

            // Register "Ctrl + F9" as my hot key
            nIDHotKey1 = GlobalAddAtom("AutoClick1");
            RegisterHotKey(hWnd, nIDHotKey1, MOD_CONTROL, VK_F9);
.
.
.
         case WM_DESTROY:

            // Un-Register my hot key
            UnregisterHotKey(hWnd, nIDHotKey1);
.
.
.
         case WM_HOTKEY:

            // (Ctrl + F9) Auto Click 1
            if (wParam == nIDHotKey1) {

               // Toggle timer on or off here
            
               return 0;
            }
.
.
.


5. To create timer and timer callback.

Code:

#define ID_TIMER1 2
int nTimer1on; // Flag indicating timer1 is on/off
.
.
.
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
            WPARAM wParam, LPARAM lParam)
   int interval;
.
.
.
         case WM_CREATE:

            // Set timer flags to off
            nTimer1on = 0;
.
.
.
         case WM_HOTKEY:

            // (Ctrl + F9) Auto Click 1
            if (wParam == nIDHotKey1) {

               if (nTimer1on == 0) {
                 
                  nTimer1on = 1;

                  interval = 350; // set auto click at every 350 milli seconds interval

                  // Create timer
                  SetTimer(hWnd, ID_TIMER1, interval, NULL);
               
               } else {

                  nTimer1on = 0;

                  // Destroy timer
                  KillTimer(hWnd, ID_TIMER1);
               }
               
               return 0;
            }
.
.
.
         case WM_TIMER:
      
            if(wParam == ID_TIMER1) {

               // Send key down event
               SendKey(VK_CONTROL);

               return 0;
            }
.
.
.



I hope this will help you in your future coding endeavor

I have attached the source code in this reply for all you programmers out there. Enjoy!



The Extension 'zip' was deactivated by an board admin, therefore this Attachment is not displayed.


The Extension 'zip' was deactivated by an board admin, therefore this Attachment is not displayed.


autoclick.jpg
 Description:
Snap shot of AutoClick GUI.
 Filesize:  64.33 KB
 Viewed:  14243 Time(s)

autoclick.jpg


Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Sat Feb 23, 2008 10:43 am    Post subject: Reply with quote

Quote:
HINSTANCE hInst;
.
.
.
m_hInst = LoadLibrary("user32.dll");


Coding error.

Messy and Unefficiant..

Although it does explain a couple things, most of this has been posted many times before.
Im sure someone will find a use for it though.

_________________
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites