Reading/Writing in other process memory

1. i have this code that shows a chunk of memory in delphi. This was just a test before i can use it on a game but i ran into some problems. earlier i was able to read from it and after restarting i get an error about access violation. what could have caused this. im using windows xp sp2

2. Also looking at r:='gg';, when i just put r:=''; it wont show the read memory. why?

3. currently i can only access proccesses that have names like "delphi 7" on application but not the processes in ctrl+alt+del. how can i also read memory from proccess list in windows process manager

any help would be great. thank you. a sample code of reading and writing in other process memory would be really great that will work on windows xp sp2

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Button1: TButton;
    Button2: TButton;
    Label5: TLabel;
    Label6: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
  r: String;
  HandleWindow, mez, len, c2: cardinal;
  ThreadId, ProcessId,WindowName : integer;
    { Public declarations }
  end;

var
  Form1: TForm1;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

begin
WindowName := FindWindow(nil,'Delphi 7');
If WindowName = 0 then
Label3.Caption := 'not found'
else
begin
ThreadId := GetWindowThreadProcessId(WindowName,@ProcessId);
Label3.Caption := '' + InttoStr(ThreadId);
HandleWindow := OpenProcess(PROCESS_ALL_ACCESS,False,ProcessId);
Label4.Caption := '' + InttoStr(HandleWindow);
Label6.Caption := '' + InttoStr(HandleWindow);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
result: boolean;
addr: pointer;
begin
result := false;
r:='gg';
len := 50;
addr := ptr($00400050);
result := ReadProcessMemory(HandleWindow,addr,pointer(r),len,len);
Label6.Caption := r;
end;
end.
pogimezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MadshiCommented:
You're reading in 50 bytes. But where to? You're giving in "pointer(r)" as the address where to write to. That's ok, but there must be enough space so that you don't overwrite other important variables. Use SetLength(r, 50) before calling ReadProcessMemory, that should solve the problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.