Difference between revisions of "Lua:executeCodeEx"

From Cheat Engine
Jump to navigation Jump to search
(Created page with "<b>executeCodeEx(callmethod, timeout, address, {type=x,value=param1} or param1,{type=x,value=param2} or param2,...)</b> <pre> callmethod: 0=stdcall, 1=cdecl timeout: Numb...")
 
 
Line 1: Line 1:
<b>executeCodeEx(callmethod, timeout, address, {type=x,value=param1} or param1,{type=x,value=param2} or param2,...)</b>
+
[[Category:Lua]]
 +
{{NeedWork}}
 +
{{CodeBox|'''function''' executeCodeEx(''callmethod'', ''timeout'', ''address'', ''param1'', ''...'') ''':''' integer or nil}}
 +
 
 +
Executes code at the specified address.
 +
 
 +
The ''callmethod'' parameter determines the calling convention. The ''timeout'' parameter determines how long Cheat Engine waits for a result. Function parameters can either be passed directly, in which case Cheat Engine will try to guess the type, or as explicit parameter tables containing a ''type'' and ''value'' field.
 +
 
 +
If ''timeout'' is 0, Cheat Engine will not wait for a result. In that case, the call memory will not be freed automatically, which can cause a memory leak.
 +
 
 +
When calling compiled C++ instance methods, keep in mind that the object instance is passed implicitly by the compiler. The instance is not written as a normal argument in the C++ source code, but the compiled function still expects it. On 64-bit Windows, this pointer is commonly passed in RCX, so it must be provided as the first parameter.
 +
 
 +
For example, a method such as ''CheatManager::Fly'' may need to be called by passing the CheatManager instance as the first argument. '''(Reference provided by SunBeam)'''
 +
 
 +
===Function Parameters===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Parameter
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|callmethod
 +
|Integer
 +
|The calling convention to use. 0 uses stdcall, and 1 uses cdecl.
 +
|-
 +
|timeout
 +
|Integer or nil
 +
|The number of milliseconds to wait for a result. nil or -1 waits indefinitely. 0 does not wait for a result.
 +
|-
 +
|address
 +
|Integer or [[CEAddressString]]
 +
|The address to execute.
 +
|-
 +
|param1
 +
|Any or Table (optional)
 +
|The first parameter to pass to the executed code. This can be a direct value or a table containing a type and value field.
 +
|-
 +
|...
 +
|Any or Table (optional)
 +
|Additional parameters to pass to the executed code.
 +
|}
 +
 
 +
===Call Method Values===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Value
 +
!align="left"|Calling Convention
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|0
 +
|stdcall
 +
|Uses the stdcall calling convention.
 +
|-
 +
|1
 +
|cdecl
 +
|Uses the cdecl calling convention.
 +
|}
 +
 
 +
===Timeout Values===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Value
 +
!align="left"|Meaning
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|nil
 +
|Wait indefinitely
 +
|Waits indefinitely for the executed code to finish.
 +
|-
 +
|-1
 +
|Wait indefinitely
 +
|Waits indefinitely for the executed code to finish.
 +
|-
 +
|0
 +
|Do not wait
 +
|Does not wait for a result. The call memory will not be freed automatically, which can cause a memory leak.
 +
|-
 +
|> 0
 +
|Timed wait
 +
|Waits up to the specified number of milliseconds for a result.
 +
|}
 +
 
 +
===Parameter Table Fields===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Field
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|type
 +
|Integer
 +
|The explicit type of the parameter value.
 +
|-
 +
|value
 +
|Any
 +
|The value to pass to the executed code.
 +
|}
 +
 
 +
===Parameter Type Values===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Value
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|0
 +
|Integer or pointer
 +
|A 32-bit or 64-bit integer value. This can also be used for pointers.
 +
|-
 +
|1
 +
|Float
 +
|A 32-bit floating point value.
 +
|-
 +
|2
 +
|Double
 +
|A 64-bit floating point value.
 +
|-
 +
|3
 +
|ASCII string
 +
|An ASCII string. The string will be converted to a pointer to that string.
 +
|-
 +
|4
 +
|Wide string
 +
|A wide string. The string will be converted to a pointer to that string.
 +
|}
 +
 
 +
===Returns===
 +
integer or nil — The result of the executed code when Cheat Engine waits for completion, or nil when no result is available.
 +
 
 +
===Examples===
 
<pre>
 
<pre>
  callmethod: 0=stdcall, 1=cdecl
+
local address = getAddress("SomeFunction")
  
   timeout: Number of milliseconds to wait for a result. nil or -1, infitely. 0 is no wait (will not free the call memory, so beware of it's memory leak)
+
local result = executeCodeEx(
 +
  0,
 +
  1000,
 +
  address,
 +
   {type = 0, value = 123}
 +
)
 +
 
 +
print("Result: " .. tostring(result))
 +
</pre>
 +
 
 +
<pre>
 +
local address = getAddress("SomeFunction")
  
  address: Address to execute
+
-- Let Cheat Engine guess the parameter types
 +
local result = executeCodeEx(0, 1000, address, 123, 456)
  
  {type,value} : Table containing the value type, and the value
+
print("Result: " .. tostring(result))
    {
 
    type: 0=integer (32/64bit) can also be a pointer
 
          1=float (32-bit float)
 
          2=double (64-bit float)
 
          3=ascii string (will get converted to a pointer to that string)
 
          4=wide string (will get converted to a pointer to that string)
 
   
 
    value: anything base type that lua can interpret
 
    }
 
 
</pre>
 
</pre>
  
if just param is provided CE will guess the type based on the provided type
+
<pre>
 +
local address = getAddress("SomeFunction")
  
 +
-- Pass an ASCII string parameter
 +
local result = executeCodeEx(
 +
  0,
 +
  1000,
 +
  address,
 +
  {type = 3, value = "Hello World"}
 +
)
 +
 +
print("Result: " .. tostring(result))
 +
</pre>
  
<h3>Example:</h3>
 
 
<pre>
 
<pre>
print( executeCodeEx(0, nil, "MessageBoxA", 0, {type=3,value="title"}, {type=3,value="content"}, 0) )
+
local flyAddress = getAddress("CheatManager.Fly")
 +
local cheatManagerInstance = getAddress("CheatManagerInstance")
 +
 
 +
-- Pass the class instance as the first argument
 +
executeCodeEx(0, nil, flyAddress, cheatManagerInstance)
 
</pre>
 
</pre>
 +
 +
{{LuaSeeAlso}}

Latest revision as of 05:04, 21 June 2026

⚠️ Work Needed

This page still needs significant improvements.

Please help by expanding or correcting this article where possible.

For suggestions or discussion, see the talk page.

<> Lua API Reference

function executeCodeEx(callmethod, timeout, address, param1, ...) : integer or nil

Executes code at the specified address.

The callmethod parameter determines the calling convention. The timeout parameter determines how long Cheat Engine waits for a result. Function parameters can either be passed directly, in which case Cheat Engine will try to guess the type, or as explicit parameter tables containing a type and value field.

If timeout is 0, Cheat Engine will not wait for a result. In that case, the call memory will not be freed automatically, which can cause a memory leak.

When calling compiled C++ instance methods, keep in mind that the object instance is passed implicitly by the compiler. The instance is not written as a normal argument in the C++ source code, but the compiled function still expects it. On 64-bit Windows, this pointer is commonly passed in RCX, so it must be provided as the first parameter.

For example, a method such as CheatManager::Fly may need to be called by passing the CheatManager instance as the first argument. (Reference provided by SunBeam)

Function Parameters[edit]

Parameter Type Description
callmethod Integer The calling convention to use. 0 uses stdcall, and 1 uses cdecl.
timeout Integer or nil The number of milliseconds to wait for a result. nil or -1 waits indefinitely. 0 does not wait for a result.
address Integer or CEAddressString The address to execute.
param1 Any or Table (optional) The first parameter to pass to the executed code. This can be a direct value or a table containing a type and value field.
... Any or Table (optional) Additional parameters to pass to the executed code.

Call Method Values[edit]

Value Calling Convention Description
0 stdcall Uses the stdcall calling convention.
1 cdecl Uses the cdecl calling convention.

Timeout Values[edit]

Value Meaning Description
nil Wait indefinitely Waits indefinitely for the executed code to finish.
Wait indefinitely Waits indefinitely for the executed code to finish.
0 Do not wait Does not wait for a result. The call memory will not be freed automatically, which can cause a memory leak.
> 0 Timed wait Waits up to the specified number of milliseconds for a result.

Parameter Table Fields[edit]

Field Type Description
type Integer The explicit type of the parameter value.
value Any The value to pass to the executed code.

Parameter Type Values[edit]

Value Type Description
0 Integer or pointer A 32-bit or 64-bit integer value. This can also be used for pointers.
1 Float A 32-bit floating point value.
2 Double A 64-bit floating point value.
3 ASCII string An ASCII string. The string will be converted to a pointer to that string.
4 Wide string A wide string. The string will be converted to a pointer to that string.

Returns[edit]

integer or nil — The result of the executed code when Cheat Engine waits for completion, or nil when no result is available.

Examples[edit]

local address = getAddress("SomeFunction")

local result = executeCodeEx(
  0,
  1000,
  address,
  {type = 0, value = 123}
)

print("Result: " .. tostring(result))
local address = getAddress("SomeFunction")

-- Let Cheat Engine guess the parameter types
local result = executeCodeEx(0, 1000, address, 123, 456)

print("Result: " .. tostring(result))
local address = getAddress("SomeFunction")

-- Pass an ASCII string parameter
local result = executeCodeEx(
  0,
  1000,
  address,
  {type = 3, value = "Hello World"}
)

print("Result: " .. tostring(result))
local flyAddress = getAddress("CheatManager.Fly")
local cheatManagerInstance = getAddress("CheatManagerInstance")

-- Pass the class instance as the first argument
executeCodeEx(0, nil, flyAddress, cheatManagerInstance)

Main Pages

Core Lua documentation entry points

Lua
Script Engine