Skip to content

Commit

Permalink
implement allocateSharedMemoryLocal
Browse files Browse the repository at this point in the history
  • Loading branch information
cheat-engine committed Jun 21, 2019
1 parent 8b41f1c commit ee6f9b4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
45 changes: 45 additions & 0 deletions Cheat Engine/LuaHandler.pas
Original file line number Diff line number Diff line change
Expand Up @@ -6037,6 +6037,47 @@ function createPaintBox(L: Plua_State): integer; cdecl;
result:=1;
end;

function allocateSharedMemoryLocal(L: PLua_State): integer; cdecl;
var
parameters: integer;
sharedmemoryname: string;
size: ptruint;
address: pointer;
begin
result:=0;
parameters:=lua_gettop(L);
if parameters>=1 then
begin
sharedmemoryname:=Lua_ToString(L,1);

if parameters>=2 then
size:=lua_tointeger(L, 2)
else
size:=4096;

lua_pop(L, parameters);

address:=allocateSharedMemory(sharedmemoryname, size);
if address<>nil then
begin
lua_pushinteger(L, ptruint(address));
result:=1;
end;
end else lua_pop(L, parameters);
end;

function deallocateSharedMemoryLocal(L: PLua_State): integer; cdecl;
var address: ptruint;
begin
result:=0;
if lua_gettop(L)>=1 then
begin
address:=lua_tointeger(L,1);

UnmapViewOfFile(pointer(address));
end;
end;

function allocateSharedMemory(L: PLua_State): integer; cdecl;
var
parameters: integer;
Expand Down Expand Up @@ -11653,6 +11694,10 @@ procedure InitializeLua;
lua_register(L, 'writeProcessMemoryCR3', lua_writeProcessMemoryCR3);



lua_register(L, 'allocateSharedMemoryLocal', allocateSharedMemoryLocal);
lua_register(L, 'deallocateSharedMemoryLocal', deallocateSharedMemoryLocal);

lua_register(L, 'allocateSharedMemory', allocateSharedMemory);
lua_register(L, 'deallocateSharedMemory', deallocateSharedMemory);
lua_register(L, 'getCheatEngineDir', getCheatEngineDir);
Expand Down
3 changes: 2 additions & 1 deletion Cheat Engine/bin/celua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,9 @@ allocateMemory(size, BaseAddress OPTIONAL, Protection OPTIONAL): Allocates some
deAlloc(address, size OPTIONAL): Frees allocated memory

allocateSharedMemory(name, size):
Creates a shared memory object of the given size if it doesn't exist yet. If size is not given and there is no shared region with this name then the default size of 4096 is used
Creates a shared memory object in the attached process of the given size if it doesn't exist yet. If size is not given and there is no shared region with this name then the default size of 4096 is used
It then maps this shared memory block into the currently targeted process. It returns the address of mapped region in the target process
allocateSharedMemoryLocal(name, size): Same as allocateSharedMemory but then for the Cheat Engine process itself


createSection(size) : Creates a 'section' in memory
Expand Down
32 changes: 26 additions & 6 deletions Cheat Engine/sharedMemory.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,36 @@ interface

const FILE_MAP_EXECUTE = $20;

function allocateSharedMemory(name: string; size: integer=4096): pointer;
function allocateSharedMemoryIntoTargetProcess(name: string; size: integer=4096): pointer;
procedure createSharedMemory(name: string; size: integer);

function createSharedMemory(name: string; size: integer): THandle;

implementation

uses ProcessHandlerUnit;


procedure createSharedMemory(name: string; size: integer);
function createSharedMemory(name: string; size: integer): THandle;
begin
CreateFileMapping(INVALID_HANDLE_VALUE,nil,PAGE_EXECUTE_READWRITE,0,size,pchar(name));
result:=CreateFileMapping(INVALID_HANDLE_VALUE,nil,PAGE_EXECUTE_READWRITE,0,size,pchar(name));
end;

function allocateSharedMemory(name: string; size: integer=4096): pointer;
var
access: dword;
h: thandle;
begin
access:=FILE_MAP_EXECUTE or FILE_MAP_READ or FILE_MAP_WRITE;


//if name does not exist allocate it first
h:=OpenFileMapping(access, false, pchar(name));

if h=0 then
h:=createSharedMemory(name, size);

result:=MapViewOfFile(h,access,0,0,0);
end;

function allocateSharedMemoryIntoTargetProcess(name: string; size: integer=4096): pointer;
var s: tstringlist;
Expand All @@ -35,13 +51,17 @@ function allocateSharedMemoryIntoTargetProcess(name: string; size: integer=4096)
address: ptruint;

access: dword;

h: THandle;
begin
access:=FILE_MAP_EXECUTE or FILE_MAP_READ or FILE_MAP_WRITE;


//if name does not exist allocate it first
if OpenFileMapping(access, false, pchar(name))=0 then
createSharedMemory(name, size);
h:=OpenFileMapping(access, false, pchar(name));

if h=0 then
h:=createSharedMemory(name, size);


result:=nil;
Expand Down

0 comments on commit ee6f9b4

Please sign in to comment.