Search code examples
delphireadprocessmemorycopymemory

Read the memory of my own process without using ReadProcessMemory


Using this way I can get the correct value, but I would like an example of how to read the memory of my own process without using ReadProcessMemory.

var
  Modulo : HMODULE;
  Value1, Value2, Read : Cardinal;
  GetWindowTextAAPI: function (hWnd: HWND; lpString: PChar; nMaxCount: integer):integer; stdcall;
begin
  Modulo := GetModuleHandle('user32.dll');
  if (Modulo <> 0) then
  begin
    @GetWindowTextAAPI := GetProcAddress(Modulo, 'GetWindowTextA');
    if (@GetWindowTextAAPI <> nil) then
    begin
      ReadProcessMemory(GetCurrentProcess, Pointer(@GetWindowTextAAPI), Addr(Value1), 4, Read);
      ReadProcessMemory(GetCurrentProcess, Pointer(DWORD(@GetWindowTextAAPI)+4), Addr(Value2), 4, Read);
      ShowMessage(
      IntToStr(Value1)
      + ' ' +
      IntToStr(Value2)
      );
    end;
  end;
end;

How to Use the function CopyMemory correctly?


Solution

  • There's nothing special you need to do to read memory from your own process. It's what your program already does all the time. You certainly don't need ReadProcessMemory. Instead, you just dereference a pointer.

    Since it doesn't look like you're interested in calling the API function, you can start by simplifying your function-pointer declaration:

    var
      GetWindowTextAAPI: PDWord;
    

    Then, assign the pointer and read the value:

    GetWindowTextAAPI := GetProcAddress(Modulo, 'GetWindowTextA');
    Value1 := GetWindowTextAAPI^;