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 


Lua scrip change the title game when the click Button

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Sting9x
Expert Cheater
Reputation: 0

Joined: 27 Jul 2016
Posts: 124

PostPosted: Sat Nov 26, 2016 6:20 am    Post subject: Lua scrip change the title game when the click Button Reply with quote

How to hack " button" using this code ... the title will change as me wish. If yes, please give me that code.
Thank admin and members Embarassed
Back to top
View user's profile Send private message
Sting9x
Expert Cheater
Reputation: 0

Joined: 27 Jul 2016
Posts: 124

PostPosted: Tue Nov 29, 2016 5:46 am    Post subject: Reply with quote

up Sad
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 941

PostPosted: Tue Nov 29, 2016 7:37 am    Post subject: Reply with quote

Like this?
Code:

function TitleText(caption)
  local pid = GetOpenedProcessID()
  if pid and pid~=0 then
    local pkl = package.loaded
    pkl['pid2hwnd'] = pkl['pid2hwnd'] or {}
    local p2w = require'pid2hwnd'
    local wn =p2w[pid] and getWindowProcessID(p2w[pid][1])
    wn = wn~=0 and pid == wn and p2w[pid][2]
    wn = wn~=0 and findWindow(wn)--[[use saved windowclass]]or
                   findWindow(nil,GetWindowlist()[pid])--[[use caption, not exact?]]
    if wn~=0 then
      p2w[pid] = {wn,getWindowClassName(wn)}
      local lpStr = createMemoryStream()
      lpStr.write(stringToByteTable(tostring(caption).."\0"))
      sendMessage(wn,0xc--[[WM_SETTEXT ]],nil--[[not used]],lpStr.Memory)
      lpStr.Destroy()
    end
  end
end

function YourButtonClick()
  TitleText"Hello"
end


FIXED?
It seems findWindow using caption text is not exact,
a second call of old version function may fail.
New version still not always work, eg. not work if attached to CE itself.
Sorry, don't know how the window handles work...

bye~

_________________
- Retarded.


Last edited by panraven on Wed Nov 30, 2016 3:10 am; edited 1 time in total
Back to top
View user's profile Send private message
akumakuja28
Master Cheater
Reputation: 16

Joined: 28 Jun 2015
Posts: 432

PostPosted: Wed Nov 30, 2016 2:06 am    Post subject: Reply with quote

@ panraven what is the "require"?
_________________
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Wed Nov 30, 2016 2:28 am    Post subject: Reply with quote

Code:

8.1 – The require Function

Lua offers a higher-level function to load and run libraries, called require. Roughly, require does the same job as dofile, but with two important differences. First, require searches for the file in a path; second, require controls whether a file has already been run to avoid duplicating the work. Because of these features, require is the preferred function in Lua for loading libraries.

The path used by require is a little different from typical paths. Most programs use paths as a list of directories wherein to search for a given file. However, ANSI C (the abstract platform where Lua runs) does not have the concept of directories. Therefore, the path used by require is a list of patterns, each of them specifying an alternative way to transform a virtual file name (the argument to require) into a real file name. More specifically, each component in the path is a file name containing optional interrogation marks. For each component, require replaces each `?´ by the virtual file name and checks whether there is a file with that name; if not, it goes to the next component. The components in a path are separated by semicolons (a character seldom used for file names in most operating systems). For instance, if the path is

    ?;?.lua;c:\windows\?;/usr/local/lua/?/?.lua

then the call require"lili" will try to open the following files:

    lili
    lili.lua
    c:\windows\lili
    /usr/local/lua/lili/lili.lua

The only things that require fixes is the semicolon (as the component separator) and the interrogation mark; everything else (such as directory separators or file extensions) is defined in the path.

To determine its path, require first checks the global variable LUA_PATH. If the value of LUA_PATH is a string, that string is the path. Otherwise, require checks the environment variable LUA_PATH. Finally, if both checks fail, require uses a fixed path (typically "?;?.lua", although it is easy to change that when you compile Lua).

The other main job of require is to avoid loading the same file twice. For that purpose, it keeps a table with the names of all loaded files. If a required file is already in the table, require simply returns. The table keeps the virtual names of the loaded files, not their real names. Therefore, if you load the same file with two different virtual names, it will be loaded twice. For instance, the command require"foo" followed by require"foo.lua", with a path like "?;?.lua", will load the file foo.lua twice. You can access this control table through the global variable _LOADED. Using this table, you can check which files have been loaded; you can also fool require into running a file twice. For instance, after a successful require"foo", _LOADED["foo"] will not be nil. If you then assign nil to _LOADED["foo"], a subsequent require"foo" will run the file again.

A component does not need to have interrogation marks; it can be a fixed file name, such as the last component in the following path:

    ?;?.lua;/usr/local/default.lua

In this case, whenever require cannot find another option, it will run this fixed file. (Of course, it only makes sense to have a fixed component as the last component in a path.) Before require runs a chunk, it defines a global variable _REQUIREDNAME containing the virtual name of the file being required. We can use these facilities to extend the functionality of require. In an extreme example, we may set the path to something like "/usr/local/lua/newrequire.lua", so that every call to require runs newrequire.lua, which can then use the value of _REQUIREDNAME to actually load the required file.
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 941

PostPosted: Wed Nov 30, 2016 3:18 am    Post subject: Reply with quote

As Corroder's paste~
more info~
http://q-syshelp.qschome.com/Content/Control%20Scripting/Lua%205.3%20Reference%20Manual/Standard%20Libraries/2%20-%20Modules.htm

I use it there to avoid making global variable.

_________________
- Retarded.
Back to top
View user's profile Send private message
Sting9x
Expert Cheater
Reputation: 0

Joined: 27 Jul 2016
Posts: 124

PostPosted: Wed Nov 30, 2016 7:41 am    Post subject: Reply with quote

Not work , There is no easy way? I just wanted to change the titlebar ? Sad
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Wed Nov 30, 2016 9:17 am    Post subject: Reply with quote

I'd do it all in assembly because that's easier for me.
Code:
function changeWindowText(oldname, newname)
  autoAssemble(string.format([[globalalloc(changeWindowText,512)
  label(exit)
  label(oldname)
  label(newname)
  createthread(changeWindowText)
  changeWindowText:
    [64-bit]
    sub rsp,20
    xor rcx,rcx
    mov rdx,oldname
    call User32.FindWindowA
    test rax,rax
    jz exit
    mov rcx,rax
    mov rdx,newname
    call User32.SetWindowTextA
  exit:
    add rsp,20
    [/64-bit]
    [32-bit]
    push oldname
    push 0
    call User32.FindWindowA
    test eax,eax
    jz exit
    push newname
    push eax
    call User32.SetWindowTextA
  exit:
    [/32-bit]
    ret
  oldname:
    db '%s',0
  newname:
    db '%s',0]],oldname, newname), true)
end

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 941

PostPosted: Wed Nov 30, 2016 10:03 am    Post subject: Reply with quote

Third tries, previous post is in comment of attached Lua.

There are some functions defined in the attached Lua file, copy and paste to your ct table, or put the lua file in autorun directory if only for own use.

Code:

EnumWindows()
  - return a lua table of all top-level windows' handle. top-level windows may contain hidden or invisible windows.

GetWindowByCaptionPID(cap,pid,onlyVisible)

  cap: case-insensitive caption text to be found, can be partial, eg. 'firefox'   can match a windows caption of 'FireFoxWindow';
  pid: the process id, if not specified, use attached process id;
  onlyVisible: if not equal 'false', the match will only look for visible window;

  - return the window handle (integer, 0 if not valid) that 1st match of window with partial case-insensitive caption with the given pid, optionally only match visible windows.

SetWindowCaption(hwnd,caption)
  hwnd: the window handle
  caption: text to be set as caption

TitleText(HWNDOrOriginalCaption,pid,onlyVisible)

  HWNDOrOriginalCaption:
    if it is a number it is use as the window handle obtained by other way, pid and onlyVisible is ignored,
    otherwise it must be the caption text to be found. The function will use GetWindowByCaptionPID to locate the target window;
  pid: process id, check GetWindowByCaptionPID;
  onlyVisible: check GetWindowByCaptionPID;

  - this function return another function to change the caption afterward


-- usage:
function YourButtonClick()
  if not NewCaption then
    NewCaption = TitleText("A Game") -- original caption must be given
  end
  if NewCaption then -- NewCaption is Global Variable, may be fail to get a right window
    NewCaption("Hello")
  end
end



bye~



TitleText.lua
 Description:

Download
 Filename:  TitleText.lua
 Filesize:  6.21 KB
 Downloaded:  677 Time(s)


_________________
- Retarded.
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Thu Dec 01, 2016 4:40 am    Post subject: Reply with quote

I am not sure what is @Sting9x exactly meant :

"How to hack " button" using this code ... the title will change as me wish.."

and

"I just wanted to change the titlebar ?..."

First question shall be near to change a "button caption."
Second, wish to change "Titlebar" caption. What title bar ?. Windows title bar ? / An application title bar ? / A game title bar ? / or a trainer title bar ?

He posted his wish in Cheat Engine Lua Scripting section, but he not told about "How to hack" use lua, AA, CE or other....

Regards
Back to top
View user's profile Send private message
Sting9x
Expert Cheater
Reputation: 0

Joined: 27 Jul 2016
Posts: 124

PostPosted: Fri Dec 02, 2016 11:38 pm    Post subject: Reply with quote

panraven wrote:
Like this?
Code:

function TitleText(caption)
  local pid = GetOpenedProcessID()
  if pid and pid~=0 then
    local pkl = package.loaded
    pkl['pid2hwnd'] = pkl['pid2hwnd'] or {}
    local p2w = require'pid2hwnd'
    local wn =p2w[pid] and getWindowProcessID(p2w[pid][1])
    wn = wn~=0 and pid == wn and p2w[pid][2]
    wn = wn~=0 and findWindow(wn)--[[use saved windowclass]]or
                   findWindow(nil,GetWindowlist()[pid])--[[use caption, not exact?]]
    if wn~=0 then
      p2w[pid] = {wn,getWindowClassName(wn)}
      local lpStr = createMemoryStream()
      lpStr.write(stringToByteTable(tostring(caption).."\0"))
      sendMessage(wn,0xc--[[WM_SETTEXT ]],nil--[[not used]],lpStr.Memory)
      lpStr.Destroy()
    end
  end
end

function YourButtonClick()
  TitleText"Hello"
end


FIXED?
It seems findWindow using caption text is not exact,
a second call of old version function may fail.
New version still not always work, eg. not work if attached to CE itself.
Sorry, don't know how the window handles work...

bye~

Thanks for the code of you. It has been operating under my desire. There is a question. When I exit the game. hack my program is still active. Next, I continued playing. button activation hack I did not change the title bar. Instead I turned off the program. Turn back, how to use it when the program There remains hack operation without turning off and still change? Very Happy
Code:
function TitleText(OriginalCaption,RootOwner,classNameOrHWND)
  if type(OriginalCaption)=='boolean' then
    OriginalCaption,RootOwner,classNameOrHWND = nil,OriginalCaption,RootOwner
  end
  if type(RootOwner)~='boolean' then
    RootOwner,classNameOrHWND = true,RootOwner
  end
  local pid,wn = GetOpenedProcessID()
  if type(classNameOrHWND)=='number' then
    wn = classNameOrHWND                      -- use windowhandle directly
  elseif type(classNameOrHWND)=='string' then
    wn = findWindow(classNameOrHWND)          -- by ClassName
  elseif type(OriginalCaption)=='string' then
    wn = findWindow(nil,OriginalCaption)      -- by caption
  elseif pid~=0 then
    wn = findWindow(nil,GetWindowlist()[pid]) -- by attached pid
  end
  if wn and wn~=0 then
    local own,max = GetWindow(wn,4),1000
    if RootOwner then                          -- find the topmost owner
      while own~=0 and own~=wn and max>0 do
        max,wn,own = max-1,own,GetWindow(own,4)--get owner window
      end
    end
    return function(newCaption)  -- new function to change Caption
      local lpStr = createMemoryStream()
      lpStr.write(stringToByteTable(tostring(newCaption).."\0"))
      sendMessage(wn,0xc--[[WM_SETTEXT ]],nil--[[not used]],lpStr.Memory)
      lpStr.Destroy()
    end,wn
  end
end
function okclick()
 if not NewCaption then
    NewCaption = TitleText(CPModz) -- no parameter to find root owner/topmost window of the attached process
  end
  if NewCaption then -- NewCaption is Global Variable, may be fail to get a right window
    NewCaption("Hello")
    openProcess("Game.exe")
showMessage("Bạn Đã Chọn Hack Client. Mời Vào Game.")
  end
end
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 941

PostPosted: Sun Dec 04, 2016 1:11 am    Post subject: Reply with quote

Sorry your English is not comprehensible for me as another non native English speaker.

When game exist, the window handle Lua remembered no longer exist, it need to reset somehow when reload the game, eg. by CE onOpenProcess, or restart the Lua/CE side.

It may be better separating the application logic between \change title text' and 'open/attach the game process',ie. one button/function to change text, another button to open process.

Also, the older version of TitleText may not be used, since it may find the windows handle by caption which use caption text to "findWindows" likely not accurately select the right windows handle.

For example, the follow list some windows handles, one by EnumWindows, which list all top-level windows on desktop, while others list by GetWindowslist which map each pid with a caption of one of its windows handle.
It can be seen that the same Caption can be in different Process, eg.
caption "Rogue Wizards" is in process 0x46B4 (the Game), and process 0x14a4 which is the windows explorer (one of many folder instance it has), so the older version has higher possibility to locate a wrong windows handle.

Try use GetWindowsByCaptionPID.

Code:

  by EnumWindows()
...
  4A0A24 (14A4) [1]:                                  This PC  CabinetWClass
  36087A (14A4) [1]:                               BladeBones  CabinetWClass
...
  4B0A4E (14A4) [0]:                              Default IME  IME
   614E0 (46B4) [0]:                              Default IME  IME
   50918 (14A4) [0]:                              GDI+ Window  GDI+ Hook Window Class
...
  57108E (46B4) [0]:                              MSCTFIME UI  MSCTFIME UI
   20068 (14A4) [1]:                            Rogue Wizards  CabinetWClass
   D14F6 (46B4) [1]:                            Rogue Wizards  UnityWndClass
   C0872 (14A4) [0]:   .NET-BroadcastEventWindow.4.0.0.0.165f  .NET-BroadcastEventWindow.4.0.0.0.165f26b.0

  by GetWindolist()
pid=14A4 ; cap=This PC
pid=46B4 ; cap=Rogue Wizards


bye~

_________________
- Retarded.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting 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