Lua:mapMemory

From Cheat Engine
Revision as of 23:39, 26 June 2026 by Leunsel (talk | contribs) (Added related function template.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

<> Lua API Reference

function mapMemory(address, size, frompid OPTIONAL, topid OPTIONAL) : integer, integer

Maps a specific address range into the usermode context of another process.

The memory is mapped from the given source process ID to the given target process ID. If a process ID is 0 or not specified, the Cheat Engine process is used.

This function returns two values: the mapped address and an MDL handle. The MDL value is required when calling unmapMemory.

Function Parameters

Parameter Type Description
address Integer The address to map.
size Integer The number of bytes to map.
frompid Integer OPTIONAL The source process ID. If 0 or omitted, the Cheat Engine process is used.
topid Integer OPTIONAL The target process ID. If 0 or omitted, the Cheat Engine process is used.

Returns

Return Value Type Description
mappedAddress Integer The mapped address in the target usermode context.
mdl Integer The MDL handle required to unmap the mapped memory with unmapMemory.

Examples

Map memory from the opened process to Cheat Engine

1 local address = getAddress("game.exe+1234")
2 local size = 4096
3 local frompid = getOpenedProcessID()
4 
5 local mappedAddress, mdl = mapMemory(address, size, frompid, 0)
6 
7 print(string.format("Mapped address: %X", mappedAddress))
8 print(string.format("MDL: %X", mdl))

Map memory and unmap it later

 1 local address = getAddress("game.exe+1234")
 2 local size = 4096
 3 local frompid = getOpenedProcessID()
 4 
 5 local mappedAddress, mdl = mapMemory(address, size, frompid, 0)
 6 
 7 if mappedAddress ~= nil and mappedAddress ~= 0 then
 8   print(string.format("Mapped address: %X", mappedAddress))
 9 
10   -- Access mapped memory here
11 end
12 
13 if mdl ~= nil and mdl ~= 0 then
14   unmapMemory(mappedAddress, mdl)
15 end

Map memory using the default process IDs

1 local address = allocateMemory(4096)
2 local size = 4096
3 local mappedAddress, mdl = mapMemory(address, size)
4 
5 print(string.format("Mapped address: %X", mappedAddress))
6 
7 unmapMemory(mappedAddress, mdl)

Map memory from Cheat Engine to the opened process

 1 local address = allocateMemory(4096)
 2 local size = 4096
 3 local frompid = 0
 4 local topid = getOpenedProcessID()
 5 
 6 local mappedAddress, mdl = mapMemory(address, size, frompid, topid)
 7 
 8 print(string.format("Mapped address in target: %X", mappedAddress))
 9 
10 unmapMemory(mappedAddress, mdl)

Validate the returned values

 1 local address = getAddress("game.exe+1234")
 2 local size = 4096
 3 local frompid = getOpenedProcessID()
 4 
 5 local mappedAddress, mdl = mapMemory(address, size, frompid, 0)
 6 
 7 if mappedAddress == nil or mappedAddress == 0 or mdl == nil or mdl == 0 then
 8   print("mapMemory failed")
 9   return
10 end
11 
12 print("Memory mapped successfully")
13 
14 unmapMemory(mappedAddress, mdl)

Read from mapped memory

 1 local address = getAddress("game.exe+1234")
 2 local size = 4096
 3 local frompid = getOpenedProcessID()
 4 
 5 local mappedAddress, mdl = mapMemory(address, size, frompid, 0)
 6 
 7 if mappedAddress ~= nil and mappedAddress ~= 0 then
 8   local value = readInteger(mappedAddress)
 9 
10   print("Value: " .. tostring(value))
11 end
12 
13 if mdl ~= nil and mdl ~= 0 then
14   unmapMemory(mappedAddress, mdl)
15 end

Use pcall to ensure cleanup

 1 local address = getAddress("game.exe+1234")
 2 local size = 4096
 3 local frompid = getOpenedProcessID()
 4 
 5 local mappedAddress, mdl = mapMemory(address, size, frompid, 0)
 6 
 7 local ok, err = pcall(function()
 8   if mappedAddress ~= nil and mappedAddress ~= 0 then
 9     print(readInteger(mappedAddress))
10   end
11 end)
12 
13 if mdl ~= nil and mdl ~= 0 then
14   unmapMemory(mappedAddress, mdl)
15 end
16 
17 if not ok then
18   print(err)
19 end

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Sections / Views

Memory Mapping

Memory Copy / Compare

Memory Streams