Forum > Windows

[Solved] How to read local variable value with Read Memory ?

(1/2) > >>

loaded:
Hi All,
When I want to use the variable I defined with its memory address, it's ok. When I want to read it using memoryread api in windows, the result fails. Where am I going wrong or what should I do? I would be glad if you share your ideas and suggestions. Respects.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  ival:integer=2022;  rval:integer=0;  l:DWORD;begin  ReadProcessMemory(Handle,@ival,@rval,sizeof(rval),l);  // uses add windows  showmessage(format('%p -> %d | ReadProcessMemory %d',[@ival,PInteger(@ival)^,rval]));end;

GetMem:
@loaded

--- Quote ---When I want to use the variable I defined with its memory address, it's ok. When I want to read it using memoryread api in windows, the result fails. Where am I going wrong or what should I do? I would be glad if you share your ideas and suggestions. Respects.
--- End quote ---
Handle in your example refers to the form's window handle. You need to pass the handle off the process to ReadProcessMemory:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---//instead off:  ReadProcessMemory(Handle,@ival,@rval,sizeof(rval),l);//you need:  ReadProcessMemory(GetCurrentProcess,@ival,@rval,sizeof(rval),l); 
Edit: Corrected typo.

Thaddy:
Will that work with stack allocations? Simple local variables are allocated on the stack, not the heap!
Process memory is heap...

440bx:

--- Quote from: Thaddy on January 17, 2022, 11:07:44 am ---Will that work with stack allocations? Simple local variables are allocated on the stack, not the heap!
Process memory is heap...

--- End quote ---
It will work with stack allocations or any other memory block as long as the process that attempts to read it has the right to read the location and, of course, there actually is memory there.  As long as those two conditions are true the read will succeed.

Thaddy:
So you must read it inside a method or procedure, otherwise the stack allocations go out of scope / is released on exit?

Navigation

[0] Message Index

[#] Next page

Go to full version