Difference between revisions of "Lua:getAddress"

From Cheat Engine
Jump to navigation Jump to search
(Added more detailed documentation with useful examples)
m (Added related function template.)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
'''function''' getAddress(''SymbolName'', ''Local'' OPTIONAL) ''':''' Integer
+
{{CodeBox|'''function''' getAddress(''string'', ''local'') ''':''' integer}}
  
Resolves a symbol name (symbol, module, export, address, or expression) to a memory address. Raises an error if the symbol cannot be resolved.
+
Returns the address of a symbol.
  
This resolver supports module bases, module+offset expressions, registered symbols, labels from auto-assembler scripts, exported/debug symbols, and pointer arithmetic (e.g., "[module+offset]+8"). If the first argument is already a number, it is returned unchanged.
+
The symbol can be a registered symbol, a module name, or an exported symbol. Set ''local'' to true to query the symbol table of the Cheat Engine process instead of the currently opened target process.
  
 
+
===Function Parameters===
=== Function Parameters ===
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 
 
{|width="85%" cellpadding="10%" cellpadding="5%" cellspacing="0" border="0"
 
 
!align="left"|Parameter
 
!align="left"|Parameter
 
!align="left"|Type
 
!align="left"|Type
!style="width: 80%;" align="left"|Description
+
!style="width: 80%;background-color:white;" align="left"|Description
 
|-
 
|-
|SymbolName
+
|symbolname
|string or number
+
|String or [[CEAddressString]]
|The symbol, module name, export, label, or expression to resolve to an address. If a number is provided, it's returned unchanged
+
|The symbol, module name, or exported symbol to resolve.
 
|-
 
|-
|Local
+
|local
|boolean
+
|Boolean (optional)
|'''Optional.''' If true, uses Cheat Engine's self symbol handler instead of the target process. Default: false
+
|If true, queries the symbol table of the Cheat Engine process.
 
|}
 
|}
 
  
 
===Returns===
 
===Returns===
An integer representing the resolved memory address.
+
integer — The resolved address of the given symbol.
 
 
'''Important:''' This function '''raises an exception''' if symbol resolution fails. For safe resolution that returns nil on failure, use [[Lua:getAddressSafe|getAddressSafe]] instead.
 
 
 
 
 
=== Description ===
 
 
 
The function resolves various types of address expressions:
 
 
 
'''Module names:'''
 
* "game.exe" → Base address of game.exe
 
* "kernel32.dll" → Base address of kernel32.dll
 
 
 
'''Module + offset:'''
 
* "game.exe+1234" → Base address + 0x1234
 
* "game.exe+ABC" → Hex offsets automatically recognized
 
 
 
'''Symbol names:'''
 
* Function names from debug symbols
 
* Exported function names
 
* Registered symbols from registerSymbol()
 
 
 
'''Labels:'''
 
* Auto-assembler script labels
 
* Custom registered labels
 
 
 
'''Expressions:'''
 
* "[game.exe+100]+8" → Pointer arithmetic
 
* "[[game.exe+100]]+4" → Multi-level pointers
 
 
 
 
 
=== Behavior ===
 
 
 
* If '''SymbolName''' is already a number, it's returned unchanged
 
* Symbol resolution waits for symbols to load if necessary
 
* On '''64-bit Windows''': Raises a Lua error with the exception message
 
 
 
 
 
=== Usage Examples ===
 
 
 
'''Get the address of a registered symbol in the target process:'''
 
registerSymbol("MySymbol", 0x401000)
 
local symAddr = getAddress("MySymbol")
 
print(string.format("MySymbol = 0x%X", symAddr))
 
 
 
'''Get the address of a symbol in the target process:'''
 
local ceAddr = getAddress("cheatengine-x86_64-SSE4-AVX2.exe+5000")
 
print(string.format("CE process address = 0x%X", ceAddr))
 
 
 
'''Basic module resolution:'''
 
local baseAddress = getAddress("game.exe")
 
print(string.format("Game base: %X", baseAddress))
 
 
 
'''Module with offset:'''
 
local address = getAddress("game.exe+12345")
 
print(string.format("Address: %X", address))
 
 
 
'''Using registered symbols:'''
 
registerSymbol("PlayerHealth", "game.exe+ABCD")
 
local healthAddr = getAddress("PlayerHealth")
 
print("Health at: " .. healthAddr)
 
 
 
'''Numeric input (pass-through):'''
 
local addr = getAddress(0x12345678)
 
-- Returns 0x12345678 unchanged
 
 
 
'''With error handling:'''
 
local success, result = pcall(function()
 
  return getAddress("NonExistentModule")
 
end)
 
 
if success then
 
  print("Address: " .. result)
 
else
 
  print("Error: " .. result)
 
end
 
 
 
'''Reading from resolved address:'''
 
local playerBase = getAddress("game.exe+100000")
 
local health = readInteger(playerBase + 0x50)
 
print("Health: " .. health)
 
 
 
'''Using local symbol handler:'''
 
-- Resolve symbols from Cheat Engine itself
 
local ceFunction = getAddress("SomeCEFunction", true)
 
 
 
 
 
=== Error Handling ===
 
 
 
Since getAddress raises exceptions on failure, wrap calls in pcall for safe execution:
 
 
 
'''Safe wrapper pattern:'''
 
function safeGetAddress(symbol, local)
 
  local success, result = pcall(getAddress, symbol, local)
 
  if success then
 
    return result
 
  else
 
    print("Failed to resolve: " .. symbol)
 
    return nil
 
  end
 
end
 
 
local addr = safeGetAddress("game.exe+1234")
 
if addr then
 
  -- Use address
 
end
 
 
 
'''Alternative - Use getAddressSafe:'''
 
-- Recommended for code that expects possible failures
 
local addr = getAddressSafe("game.exe+1234")
 
if addr then
 
  -- Symbol resolved
 
else
 
  -- Symbol failed, no exception raised
 
end
 
 
 
 
 
=== Platform-Specific Behavior ===
 
 
 
'''Windows 64-bit:'''
 
local addr = getAddress("InvalidSymbol")
 
-- Raises Lua error, can be caught with pcall
 
 
 
'''Windows 32-bit:'''
 
local addr = getAddress("InvalidSymbol")
 
-- Re-raises original exception (harder to catch in Lua)
 
 
 
'''Linux/macOS:'''
 
local addr, errMsg = getAddress("InvalidSymbol")
 
-- Returns: nil, "error message"
 
if not addr then
 
  print("Error: " .. errMsg)
 
end
 
 
 
 
 
=== Advanced Examples ===
 
 
 
'''Resolve and register multiple symbols:'''
 
local symbols = {
 
  PlayerHealth = "game.exe+10000",
 
  PlayerMana = "game.exe+10004",
 
  PlayerExp = "game.exe+10008"
 
}
 
 
for name, expr in pairs(symbols) do
 
  local addr = getAddress(expr)
 
  registerSymbol(name, addr)
 
  print(string.format("%s = %X", name, addr))
 
end
 
 
 
'''Dynamic offset calculation:'''
 
local base = getAddress("game.exe")
 
local patternAddr = AOBScanUnique("48 8B 05 ?? ?? ?? ??", "+X")
 
 
if patternAddr then
 
  local offset = patternAddr - base
 
  print(string.format("Pattern at game.exe+%X", offset))
 
end
 
 
 
'''Multi-level pointer resolution:'''
 
-- Note: For complex pointers, use readPointer or pointer notation
 
local base = getAddress("game.exe+100000")
 
local ptr1 = readPointer(base)
 
local ptr2 = readPointer(ptr1 + 0x10)
 
local value = readInteger(ptr2 + 0x20)
 
 
 
 
 
=== Symbol Resolution Process ===
 
 
 
When you call getAddress("game.exe+1234"), the function:
 
 
 
1. Checks if input is already a number → return it
 
2. Parses the expression ("game.exe+1234")
 
3. Looks up "game.exe" in the module list
 
4. Gets the base address of the module
 
5. Adds the offset (0x1234)
 
6. Returns the final address
 
 
 
For complex expressions, the symbol handler evaluates:
 
* Module bases
 
* Registered symbols
 
* Debug symbols (PDB/DWARF)
 
* Exported functions
 
* Mathematical operations
 
 
 
 
 
=== When to Use getAddress vs getAddressSafe ===
 
 
 
'''Use getAddress when:'''
 
* The symbol MUST exist (critical for script functionality)
 
* You want the script to fail loudly if something is wrong
 
* You're in a protected call context (pcall)
 
  
'''Use [[Lua:getAddressSafe|getAddressSafe]] when:'''
+
===Examples===
* Symbol might not exist (e.g., version-dependent features)
+
<syntaxhighlight lang="lua" line>
* You want to handle failure gracefully
+
local address = getAddress("KERNEL32.AddAtomA")
* You're checking for optional features
 
  
 +
print(string.format("%X", address))
 +
</syntaxhighlight>
  
=== See Also ===
+
{{LuaSeeAlso}}
  
* [[Lua:getCommonModuleList|getCommonModuleList]]
+
{{Address}}
* [[Lua:inModule|inModule]]
 
* [[Lua:inSystemModule|inSystemModule]]
 
* [[Lua:targetIs64Bit|targetIs64Bit]]
 
* [[Lua:enumModules|enumModules]]
 
* [[Lua:getAddressSafe|getAddressSafe]] - Safe version that returns nil on failure
 
* [[Lua:registerSymbol|registerSymbol]] - Register custom symbol names
 
* [[Lua:unregisterSymbol|unregisterSymbol]] - Remove registered symbols
 
* [[Lua:getNameFromAddress|getNameFromAddress]] - Reverse lookup (address to name)
 
* [[Lua:AOBScan|AOBScan]] - Find addresses by byte pattern
 
* [[Lua:readPointer|readPointer]] - Read pointer values
 

Latest revision as of 22:48, 26 June 2026

<> Lua API Reference

function getAddress(string, local) : integer

Returns the address of a symbol.

The symbol can be a registered symbol, a module name, or an exported symbol. Set local to true to query the symbol table of the Cheat Engine process instead of the currently opened target process.

Function Parameters[edit]

Parameter Type Description
symbolname String or CEAddressString The symbol, module name, or exported symbol to resolve.
local Boolean (optional) If true, queries the symbol table of the Cheat Engine process.

Returns[edit]

integer — The resolved address of the given symbol.

Examples[edit]

1 local address = getAddress("KERNEL32.AddAtomA")
2 
3 print(string.format("%X", address))

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Symbol Related Lua Functions

Symbol lookup, address resolution, module checks, and lookup callbacks