Skip to content

Commit

Permalink
improve winhook by letting you edit the parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
cheat-engine committed Aug 23, 2016
1 parent 1f81a32 commit 214d5f6
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 68 deletions.
7 changes: 4 additions & 3 deletions Cheat Engine/LuaHandler.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7205,10 +7205,11 @@ function lua_hookWndProc(L: Plua_State): integer; cdecl;
s.add('wndhooklist={}');
s.add('function CEWindowProcEvent_Internal(hWnd, Msg, lParam, wParam)');
s.add(' if (wndhooklist[hWnd].f) then');
s.add(' local r=wndhooklist[hWnd].f(hWnd, Msg, lParam, wParam)');
s.add(' if (r==0) or (r==1) then return r end');
s.add(' local r, hWnd2, Msg2, lParam2, wParam2');
s.add(' r,hWnd2,Msg2,lParam2,wParam2=wndhooklist[hWnd].f(hWnd, Msg, lParam, wParam)');
s.add(' if (r==0) or (r==1) then return r,(hWnd2 or hWnd),(Msg2 or Msg),(lParam2 or lParam),(wParam2 or wParam) end');
s.add(' end');
s.add(' return wndhooklist[hWnd].orig');
s.add(' return wndhooklist[hWnd].orig,(hWnd2 or hWnd),(Msg2 or Msg),(lParam2 or lParam),(wParam2 or wParam)');
s.add('end');

LUA_DoScript(s.Text);
Expand Down
2 changes: 1 addition & 1 deletion Cheat Engine/bin/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ getWindowProcessID(windowhandle): processid - Returns the processid of the proce
getForegroundWindow() - windowhandle : Returns the windowhandle of the topmost window
sendMessage(hwnd, msg, wparam, lparam): result - Sends a message to a window. Those that wish to use it, should know how to use it (and fill in the msg id's yourself)
hookWndProc(hwnd, function(hwnd, msg, wparam, lparam)) - Hooks a window's wndproc procedure. The given function will receive all functions. Return 0 to say you handled it. 1 to let the default windows handler deal with it. Or anything else, to let the original handler deal with it
hookWndProc(hwnd, function(hwnd, msg, wparam, lparam)) - Hooks a window's wndproc procedure. The given function will receive all functions. Return 0 to say you handled it. 1 to let the default windows handler deal with it. Or anything else, to let the original handler deal with it. Besides the return value, you can also return hWnd, Msg, lParam and wParam, modified, or nil for the original value
unhookWndProc(hwnd) - call this when done with the hook. Not calling this will result in the process window behaving badly when you exit CE
Expand Down
109 changes: 108 additions & 1 deletion Cheat Engine/lua_server.pas
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ TLuaServerHandler=class(TThread)
pipe: THandle;
exec: tstringlist;
result: qword;

returncount: byte;
results: array of qword;
procedure ExecuteLuaScript;
procedure ExecuteLuaScriptVar;
//executeLuaFunction
procedure ExecuteScript;
protected
procedure execute; override;
Expand Down Expand Up @@ -84,7 +89,7 @@ destructor TLuaServerHandler.destroy;

procedure TLuaServerHandler.ExecuteScript;
var
i: integer;
i,j: integer;
top: integer;

begin
Expand All @@ -97,13 +102,113 @@ procedure TLuaServerHandler.ExecuteScript;
else
result:=0;

if returncount>0 then
begin
if length(results)<returncount then
setlength(results, returncount);

for i:=0 to returncount-1 do
results[(returncount-1)-i]:=lua_tointeger(Luavm, -1-i);

end;

lua_settop(Luavm, top);

finally
luacs.leave;
end;
end;


{
todo: ExecuteLuaScriptEx
Variable paramcount
setup:
functionref: byte
if functionref=0 then
functionnamelength: byte
functionname[functionnamelength]: char
end
paramcount: byte
params[paramcount]: record
paramtype: byte - 0=nil, 1=integer64, 2=double, 3=string, 4=table perhaps ?
value:
--if paramtype=2 then
stringlength: word
string[strinbglength]: char
--else
value: 8byte
end
returncount: byte
--returns:
actualreturncount: byte
}

procedure TLuaServerHandler.ExecuteLuaScriptVar;
{
Same as ExecuteLuaScript but can return more than one return value qword
}
procedure error;
begin
OutputDebugString('Read error');
terminate;
end;

var
scriptsize: integer;
br: dword;
script: pchar;

parameter: qword;
i: integer;
begin
if readfile(pipe, scriptsize, sizeof(scriptsize), br, nil) then
begin
getmem(script, scriptsize+1);

try
if readfile(pipe, script^, scriptsize, br, nil) then
begin
script[scriptsize]:=#0;

if readfile(pipe, parameter, 8, br, nil) then
begin
if readfile(pipe, returncount, 1, br, nil) then
begin
exec.clear;
exec.Text:=script;

exec.Insert(0, 'function _luaservercall'+inttostr(GetCurrentThreadId)+'(parameter)');
exec.add('end');
exec.add('return _luaservercall'+inttostr(GetCurrentThreadId)+'('+inttostr(parameter)+')');

setlength(results, returncount);
synchronize(executescript);

for i:=0 to returncount-1 do
if writefile(pipe, results[i], 8, br, nil)=false then error;

end;
end
else
error;
end
else
error;

finally
freemem(script);
end;
end
else
error;
end;

procedure TLuaServerHandler.ExecuteLuaScript;
procedure error;
begin
Expand All @@ -118,6 +223,7 @@ procedure TLuaServerHandler.ExecuteLuaScript;

parameter: qword;
begin
returncount:=1;
if readfile(pipe, scriptsize, sizeof(scriptsize), br, nil) then
begin
getmem(script, scriptsize+1);
Expand Down Expand Up @@ -167,6 +273,7 @@ procedure TLuaServerHandler.execute;
ReadFile(pipe, command, sizeof(command), br, nil);
case command of
1: ExecuteLuaScript;
2: ExecuteLuaScriptVar;
else terminate;
end;
end;
Expand Down
28 changes: 28 additions & 0 deletions Cheat Engine/winhook/com.pas
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ TCEConnection=class
connected: boolean;
public
function DoCommand(s: string): qword;
procedure DoCommandMR(s: string; returncount: integer; results: PQword);
constructor create;
destructor destroy; override;
end;
Expand Down Expand Up @@ -131,6 +132,33 @@ destructor TCEConnection.destroy;
inherited destroy;
end;

procedure TCEConnection.DoCommandMR(s: string; returncount: integer; results: PQword);
var
m: tmemorystream;
r: qword=0;
x: dword=0;
i: integer;
begin
if not connected then exit;

m:=TMemoryStream.Create;
m.writebyte(2); //execute lua function, with a variable paramcount and returncount
m.WriteDWord(length(s));
m.WriteBuffer(s[1],length(s));
m.writeQword(0);
m.writeByte(returncount);

cs.Enter;
WriteFile(pipe, m.Memory^, m.Size, x, nil);
for i:=0 to returncount-1 do
ReadFile(pipe, results[i], sizeof(QWORD), x, nil);

cs.Leave;

m.free;

end;

function TCEConnection.DoCommand(s: string): qword;
var
m: tmemorystream;
Expand Down
5 changes: 4 additions & 1 deletion Cheat Engine/winhook/proc.pas
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ function wp (hWND:HWND; Msg:UINT; wParam:WPARAM; lParam:LPARAM):LRESULT;stdcall;
var
r: QWORD;
PrevWndFunc: WNDPROC absolute r;
results: array of QWord;
begin
r:=CEConnection.DoCommand('return CEWindowProcEvent_Internal('+IntToStr(hWnd)+','+IntToStr(Msg)+','+IntToStr(wParam)+','+IntToStr(lParam)+')');
setlength(results,5);
CEConnection.DoCommandMR('return CEWindowProcEvent_Internal('+IntToStr(hWnd)+','+IntToStr(Msg)+','+IntToStr(wParam)+','+IntToStr(lParam)+')',5,@results[0]);
r:=results[0];
if r=0 then exit(0);
if r=1 then exit(DefWindowProc(hwnd, Msg, wParam, lParam));

Expand Down
Loading

0 comments on commit 214d5f6

Please sign in to comment.