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 


Create a button in an existing window?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General Gamehacking
View previous topic :: View next topic  
Author Message
The0neThe0nly
Expert Cheater
Reputation: 0

Joined: 08 Dec 2009
Posts: 119
Location: In a box

PostPosted: Thu Dec 22, 2011 2:01 pm    Post subject: Create a button in an existing window? Reply with quote

The game I want to cheat has a window that I want to add a button to. I've looked through the process's resources and know the ID that that button should have.

So how could I do this?

All I need to do is make a button onto the window that has the same ID as another MenuItem.

I have the ID, so I've already done that part.

Now all I need is to make a button while the window is open.

I can't edit the process because it detects it (unless I edit it while the process is running, which I haven't found a way to do that). So what can I do?
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8585
Location: 127.0.0.1

PostPosted: Fri Dec 23, 2011 7:09 pm    Post subject: Reply with quote

If you are coding this yourself, you can use FindWindow to locate the window of the process you are wanting to work with, then use CreateWindowEx to create the button on the window.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
The0neThe0nly
Expert Cheater
Reputation: 0

Joined: 08 Dec 2009
Posts: 119
Location: In a box

PostPosted: Sun Feb 12, 2012 4:38 pm    Post subject: Reply with quote

Ok, I've done this, now how do I include the ID's in CreateWindowEx?
Back to top
View user's profile Send private message
Corruptor
Advanced Cheater
Reputation: 3

Joined: 10 Aug 2011
Posts: 82

PostPosted: Mon Feb 13, 2012 2:51 pm    Post subject: Reply with quote

I dont particularly know what you mean by "button id". Well, i'm just gonna assume that you want to make this button do exactly the same stuff that the button does you got the id from.

For functions of a button, you usually use the hMenu. Its basically a number that is just casted to the HMENU type. In the WinProc function, when a WM_COMMAND message is processed, the HMENU of the widget that sent the WM_COMMAND message is switched. something like this:
Code:
switch(LOWORD(wparam)) { //LOWORD(wparam) is the hmenu of the widget
      case 1: //id 1
                 doStuff();
                 return 0;
      case 2: //id 2
                 doOtherStuff();
                 return 0;
}


If thats the id you mean, all you have to do is giving the id as a parameter to the CreateWindowEx function. The hMenu is the 10th parameter.
looks like this:
Code:
CreateWindowEx(WS_EX_COOLDESIGNS,
                 "BUTTON", "Haxz0rz Skillz ",
                  WS_MAYBEOTHERCOOLDESIGNS,
                  200, 200, 50, 25, //<--- some positions
                  parentWindow,
                  (HWND)id, // <---- thats where you have to put your id
                  hInst,
                  NULL);
Back to top
View user's profile Send private message
The0neThe0nly
Expert Cheater
Reputation: 0

Joined: 08 Dec 2009
Posts: 119
Location: In a box

PostPosted: Mon Feb 13, 2012 3:39 pm    Post subject: Reply with quote

Corruptor wrote:
I dont particularly know what you mean by "button id". Well, i'm just gonna assume that you want to make this button do exactly the same stuff that the button does you got the id from.

For functions of a button, you usually use the hMenu. Its basically a number that is just casted to the HMENU type. In the WinProc function, when a WM_COMMAND message is processed, the HMENU of the widget that sent the WM_COMMAND message is switched. something like this:
Code:
switch(LOWORD(wparam)) { //LOWORD(wparam) is the hmenu of the widget
      case 1: //id 1
                 doStuff();
                 return 0;
      case 2: //id 2
                 doOtherStuff();
                 return 0;
}


If thats the id you mean, all you have to do is giving the id as a parameter to the CreateWindowEx function. The hMenu is the 10th parameter.
looks like this:
Code:
CreateWindowEx(WS_EX_COOLDESIGNS,
                 "BUTTON", "Haxz0rz Skillz ",
                  WS_MAYBEOTHERCOOLDESIGNS,
                  200, 200, 50, 25, //<--- some positions
                  parentWindow,
                  (HWND)id, // <---- thats where you have to put your id
                  hInst,
                  NULL);


But will I be able to use an ID from a different process?
Back to top
View user's profile Send private message
Corruptor
Advanced Cheater
Reputation: 3

Joined: 10 Aug 2011
Posts: 82

PostPosted: Tue Feb 14, 2012 11:23 am    Post subject: This post has 1 review(s) Reply with quote

principled, year, it should work, unless the program has some xtra hack protection.
The code sniped itself wouldnt even compile (wrote HWND instead of HMENU in it. lol) ^^
Just wrote 2 little test programs, one to hook and one that is hooked. I will paste the sorce at the end of this post, so that you can copy and compile them on your own if you want.
A function that adds the button could look like this:
Code:
bool hookStuff() {
   HWND hookWindow; //just some variable. It hold the window you are searching for.
   hookWindow = FindWindowEx(NULL,NULL,NULL, "TestWindow"); //replace "TestWindow" with the title of the window you want to add the button to
   if(!hookWindow) {
      return false; //window not found
   }
   MessageBoxA(NULL, "so far",
            "so good",0); //just a little message box to tell us that we actually found the window
   
   if(hookButton == NULL) {
      return false;
   }
   hookButton = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                     "BUTTON", "Haxz0rz Skillz",
                     BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,
                     100, 50, 100, 25,
                     hookWindow, (HMENU)(0xf1), NULL, 0);
   return true;
}

In the CreateWindowEx, you will have to replace the "0xf1" with the id you found. Also note that, on this way, the button will dissappear if the hooking programm dies.

-------------------------------------------------------------------------------------
As promised, here's the code:
the hacked program:
Code:
/*
 * This is the window you want to hook. Theres no need of knowing how this stuff works.
 * What you need to know is: It has 2 buttons. one has the (HMENU)0xf1,
 * one has(HMENU)0xae as a parameter (the id).
 */

#include <iostream>
#include <windows.h>
using namespace std;

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);


HINSTANCE hInst;
HWND button;
HWND wrongButton;
HWND mwnd;

int WINAPI WinMain (HINSTANCE hI, HINSTANCE hPrI, PSTR szCmdLine, int iCmdShow) {

   hInst = hI;
   char name[] = "Hauptfenster";
   WNDCLASSEX mc;
   mc.style = CS_HREDRAW | CS_VREDRAW;
   mc.hIcon = 0;
   mc.hIconSm = 0;
   mc.hbrBackground = CreateSolidBrush(RGB(0xfe, 0xfe, 0xfe));
   mc.hCursor = LoadCursor(NULL, IDC_ARROW);

   mc.lpszClassName = name;
   mc.lpszMenuName = NULL;
   mc.lpfnWndProc = WndProc;

   mc.cbClsExtra = 0;
   mc.cbWndExtra = 0;
   mc.hInstance = hI;

   mc.cbSize = sizeof(WNDCLASSEX);

   RegisterClassEx(&mc);

   mwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                           name, "TestWindow",
                    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                    500, 280, 300, 250,
                    NULL, 0, hInst, 0);
   ShowWindow(mwnd, iCmdShow);
   UpdateWindow(mwnd);

   MSG msg;
   while(GetMessage(&msg, NULL, 0, 0)) {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
   }
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
   switch(message) {
      case WM_CREATE:
         button = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                           "BUTTON", "click me",
                           BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,
                           20, 100, 100, 25,
                           hwnd, (HMENU)(0xf1), hInst, 0);
         wrongButton = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                           "BUTTON", "i do other stuff!",
                           BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,
                           170, 100, 100, 25,
                           hwnd, (HMENU)(0xab), hInst, 0);
         return 0;
      case WM_DESTROY:
         PostQuitMessage(0);
         return 0;
      case WM_COMMAND: {
         switch(LOWORD(wparam)) {
            case 0xf1: {
               //id 0xf1: lets make something
               MessageBoxA(NULL, "you want it\r\nyou want it\r\n"
                             "you want it\r\nyou want it\r\n well here it is!",
                             "button 1 pressed", 0);
               break;
            }
            case 0xab: {
               //the other button with the id ab
               MessageBoxA(NULL, "well...... \r\ni donno...",
                        "button 2 pressed",0);
            }
         }
         return 0;
      }
   }
   return DefWindowProc(hwnd, message, wparam, lparam);
}


And the hacking program:
Code:
#include <iostream>
#include <windows.h>
using namespace std;

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

bool hookStuff();

HINSTANCE hInst;
HWND button;
HWND hookButton;
HWND mwnd;


int WINAPI WinMain (HINSTANCE hI, HINSTANCE hPrI, PSTR szCmdLine, int iCmdShow) {

   hInst = hI;
   char name[] = "Hauptfenster";
   WNDCLASSEX mc;
   mc.style = CS_HREDRAW | CS_VREDRAW;
   mc.hIcon = 0;
   mc.hIconSm = 0;
   mc.hbrBackground = CreateSolidBrush(RGB(0xfe, 0xfe, 0xfe));
   mc.hCursor = LoadCursor(NULL, IDC_ARROW);

   mc.lpszClassName = name;
   mc.lpszMenuName = NULL;
   mc.lpfnWndProc = WndProc;

   mc.cbClsExtra = 0;
   mc.cbWndExtra = 0;
   mc.hInstance = hI;

   mc.cbSize = sizeof(WNDCLASSEX);

   RegisterClassEx(&mc);

   mwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                           name, "HookStuff ",
                    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                    500, 280, 250, 250,
                    NULL, 0, hInst, 0);
   ShowWindow(mwnd, iCmdShow);
   UpdateWindow(mwnd);

   MSG msg;
   while(GetMessage(&msg, NULL, 0, 0)) {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
   }
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
   switch(message) {
      case WM_CREATE:
         button = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                           "BUTTON", "click me to hook",
                           BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,
                           20, 100, 125, 25,
                           hwnd, (HMENU)(1), hInst, 0);
         return 0;
      case WM_DESTROY:
         PostQuitMessage(0);
         return 0;
      case WM_COMMAND: {
         switch(LOWORD(wparam)) {
            case 0x1: {
               if(hookStuff()) {
                  MessageBoxA(NULL, "success",
                           ":)",0);
               }
               else {
                  MessageBoxA(NULL, "failure",
                           ":(",0);
               }
               break;
            }
         }
         return 0;
      }
   }
   return DefWindowProc(hwnd, message, wparam, lparam);
}



bool hookStuff() {
   HWND hookWindow;
   hookWindow = FindWindowEx(NULL,NULL,NULL, "TestWindow");
   if(!hookWindow) {
      return false;
   }

   MessageBoxA(NULL, "so far",
            "so good",0);

   hookButton = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                     "BUTTON", "Haxz0rz Skillz",
                     BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,
                     20, 50, 100, 25,
                     hookWindow, (HMENU)(0xf1), NULL, 0);
   if(hookButton == NULL) {
      return false;
   }

   return true;
}
Back to top
View user's profile Send private message
The0neThe0nly
Expert Cheater
Reputation: 0

Joined: 08 Dec 2009
Posts: 119
Location: In a box

PostPosted: Tue Feb 14, 2012 8:37 pm    Post subject: Reply with quote

@Corruptor

Thanks, you have been a huge help.

I've encountered a new error though. When I try to click the button nothing happens. The button is there, but it will not let me click it.
Back to top
View user's profile Send private message
Corruptor
Advanced Cheater
Reputation: 3

Joined: 10 Aug 2011
Posts: 82

PostPosted: Wed Feb 15, 2012 5:06 pm    Post subject: Reply with quote

hm... in games with plain buttons (eg wc3), it worked for me. Also note that everything i can say from now on is guessing.

If the button-pressed animation doesnt even appear, its likely that the WM_COMMAND message hasn't been sent. If you can mange to find the WinProc, you may use the cheat engine to find out if the message has been sent at all by putting a break point to the code section that is handling the id you gave the button. As for me, i never managed to find the WinProc function and thus dont know how to do that either. Maybe someone else can tell you more. Would be worth a tutorial...

Maybe the button was added to the wrong window. Like it was added to the mainwindow, but the mainwindow holds a childwindow and the childwindow holds the buttons using a seperated WinProc or stuff like that. I May be testing around with that thought later on, but its 23:52 over here right now, and im kinda tired ^.^

have some other thoughts but they are rather the result of my wrecked mind combined with some music, an rather odd problem and the shattering cluelessness im facing right now, and since i have totally no idea of direct x programming i doubt that they would be of any use, making this whole paragraph entirely useless and... god, im gonna stop here Shocked

edit:
k, i got my sleep and im back. Im editing this post because i dont want to double post, although i fear that you may not recognise that i wrote this.

anyways: further information about where you want to put that button into would be quite usefull.
For example, the windows text editor has a big edit field where you type the text in (obviously). Simply adding the button like in the function i gave you will cause the button to be placed somewhere inside this textfield. however, its not the child of the textfield, but of the entire frame. If you try to click the button with your mouse, it will instead think you wanted to click the text field.
As for the editor, what works is adding the button to the correct child window. How to do that:

WinApi knows a function called "EnumChildWindows". This function iterates through every childwindow of a given window (mostlikely the window you found using FindWindowEx in this case) and do anything you want.

First of all, you need this function:
Code:
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lparam);

declare and define this function somewhere in your code.
This function will be called automatically, where hwnd is the handle to the childwindow. If your window has several child windows, this function will be called several times, one time for each child window. You can igonre the lparam, you dont need it.
In this function, you can do anything. For example, you can find the id of the child windows by using GetMenu(hwnd) and maybe cout it, write it in a file, messageBox it or write it in an edit field. You can also get the windows title using GetWindowText. Or other cool stuff.
What you will mostlikely want to do is creating your button though.

Once you defined this function, you can call the EnumChildWindows function. It takes the following parameters: the hwnd of the window you found using FindWindow, the name of the function you just defined (= EnumChildProc) and the lparam, which can be ignored and thus set to 0.

A call would look like this:
Code:

               hookWindow = FindWindowEx(NULL,NULL,NULL, processName.c_str());
               if(!hookWindow) {
                  MessageBoxA(NULL, "failure",
                           ":(",0);
                  return false;
               }
               EnumChildWindows(hookWindow, EnumChildProc, 0);


Of corse, that would create a button for every child window, but for now, it just matters if this works at all.
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 Gamehacking 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