Difference between revisions of "Lua:generateAPIHookScript"

From Cheat Engine
Jump to navigation Jump to search
(Major overhaul of the post.)
 
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
'''function''' generateAPIHookScript(''AddressToHook'', ''AddressToJumpTo'', ''AddressToStoreNewCallAddress'' OPTIONAL)
+
{{CodeBox|'''function''' generateAPIHookScript(''address'', ''addresstojumpto'', ''addresstogetnewcalladdress'' OPTIONAL, ''ext'' OPTIONAL, ''targetself'' OPTIONAL) ''':''' string}}
  
Returns an autoassemble script that when executed hooks the specific address and optionally writes an 'OriginalCall' stub and writes the address of that functionat the specified address.
+
Generates an Auto Assembler script that hooks the given address when the script is executed.
  
 
===Function Parameters===
 
===Function Parameters===
{|width="85%" cellpadding="10%" cellpadding="5%" cellspacing="0" border="0"
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 
!align="left"|Parameter
 
!align="left"|Parameter
 
!align="left"|Type
 
!align="left"|Type
 
!style="width: 80%;background-color:white;" align="left"|Description
 
!style="width: 80%;background-color:white;" align="left"|Description
 
|-
 
|-
|AddressToHook
+
|address
 
|[[CEAddressString]]
 
|[[CEAddressString]]
|The address to hook
+
|The address or symbol to hook.
 
|-
 
|-
|AddressToJumpTo
+
|addresstojumpto
 
|[[CEAddressString]]
 
|[[CEAddressString]]
|The address that the hook should jump to
+
|The address or symbol the hook should jump to.
 
|-
 
|-
|AddressToStoreNewCallAddress
+
|addresstogetnewcalladdress
|[[CEAddressString]]
+
|[[CEAddressString]] OPTIONAL
|Optional parameter. When provided it will write a routine that the hook code can use to call the original function
+
|Optional address or symbol used by the generated hook script to get the new call address.
 +
|-
 +
|ext
 +
|[[CEAddressString]] OPTIONAL
 +
|Optional extension text used by the generated script.
 +
|-
 +
|targetself
 +
|Boolean OPTIONAL
 +
|Optional flag that controls whether the generated hook targets the current process context.
 
|}
 
|}
  
 +
===Returns===
 +
String — The generated Auto Assembler script.
 +
 +
===Examples===
 +
 +
====Generate an API hook script====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
local script = generateAPIHookScript("kernel32.dll+1234", "MyHook")
 +
 +
print(script)
 +
</syntaxhighlight>
 +
 +
====Generate and execute the hook script====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
local script = generateAPIHookScript("kernel32.dll+1234", "MyHook")
 +
 +
if script ~= nil and script ~= "" then
 +
  autoAssemble(script)
 +
end
 +
</syntaxhighlight>
 +
 +
====Use symbols as hook targets====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
local script = generateAPIHookScript("SomeModule.SomeAPI", "MyAPIHook")
 +
 +
print(script)
 +
</syntaxhighlight>
 +
 +
====Use the optional new-call-address parameter====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
local script = generateAPIHookScript("SomeModule.SomeAPI", "MyAPIHook", "NewCallAddress")
 +
 +
print(script)
 +
</syntaxhighlight>
 +
 +
====Use all optional parameters====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
local script = generateAPIHookScript("SomeModule.SomeAPI", "MyAPIHook", "NewCallAddress", "", false)
 +
 +
print(script)
 +
</syntaxhighlight>
 +
 +
====Save the generated script to a file====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
local script = generateAPIHookScript("SomeModule.SomeAPI", "MyAPIHook")
 +
 +
if script ~= nil then
 +
  local file = io.open([[C:\\Temp\\generated_hook.cea]], "w")
 +
 +
  if file ~= nil then
 +
    file:write(script)
 +
    file:close()
 +
  end
 +
end
 +
</syntaxhighlight>
 +
 +
====Preview before execution====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
local script = generateAPIHookScript("SomeModule.SomeAPI", "MyAPIHook")
 +
 +
if script ~= nil then
 +
  print("Generated script:")
 +
  print(script)
 +
end
 +
</syntaxhighlight>
  
 
{{LuaSeeAlso}}
 
{{LuaSeeAlso}}

Latest revision as of 20:15, 25 June 2026

<> Lua API Reference

function generateAPIHookScript(address, addresstojumpto, addresstogetnewcalladdress OPTIONAL, ext OPTIONAL, targetself OPTIONAL) : string

Generates an Auto Assembler script that hooks the given address when the script is executed.

Function Parameters[edit]

Parameter Type Description
address CEAddressString The address or symbol to hook.
addresstojumpto CEAddressString The address or symbol the hook should jump to.
addresstogetnewcalladdress CEAddressString OPTIONAL Optional address or symbol used by the generated hook script to get the new call address.
ext CEAddressString OPTIONAL Optional extension text used by the generated script.
targetself Boolean OPTIONAL Optional flag that controls whether the generated hook targets the current process context.

Returns[edit]

String — The generated Auto Assembler script.

Examples[edit]

Generate an API hook script[edit]

1 local script = generateAPIHookScript("kernel32.dll+1234", "MyHook")
2 
3 print(script)

Generate and execute the hook script[edit]

1 local script = generateAPIHookScript("kernel32.dll+1234", "MyHook")
2 
3 if script ~= nil and script ~= "" then
4   autoAssemble(script)
5 end

Use symbols as hook targets[edit]

1 local script = generateAPIHookScript("SomeModule.SomeAPI", "MyAPIHook")
2 
3 print(script)

Use the optional new-call-address parameter[edit]

1 local script = generateAPIHookScript("SomeModule.SomeAPI", "MyAPIHook", "NewCallAddress")
2 
3 print(script)

Use all optional parameters[edit]

1 local script = generateAPIHookScript("SomeModule.SomeAPI", "MyAPIHook", "NewCallAddress", "", false)
2 
3 print(script)

Save the generated script to a file[edit]

 1 local script = generateAPIHookScript("SomeModule.SomeAPI", "MyAPIHook")
 2 
 3 if script ~= nil then
 4   local file = io.open([[C:\\Temp\\generated_hook.cea]], "w")
 5 
 6   if file ~= nil then
 7     file:write(script)
 8     file:close()
 9   end
10 end

Preview before execution[edit]

1 local script = generateAPIHookScript("SomeModule.SomeAPI", "MyAPIHook")
2 
3 if script ~= nil then
4   print("Generated script:")
5   print(script)
6 end

Main Pages

Core Lua documentation entry points

Lua
Script Engine