Autor Beitrag
Jan2k
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Sa 10.12.05 15:58 
Hallo zusammen

Ich schreibe gerade an einem kleinen Programm welches ein anderes Programm ausliest und mir zu jedem Wert den ich suche die Adresse im Arbeitsspeicher zurückgibt. Nur leider ist der Befehl "ReadProcessMemory" seeeeeeeeeehr langsam. Hier mein Code:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  WindowName  :  integer;
  ProcessId  :  integer;
  ThreadId  :  integer;
  WHandle  :  Integer;
  BytesRead : DWord;
  puffer : DWord;
  p : pointer;
  x : integer;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
WindowName  :=  FindWindow(nil,'test');
ThreadId  :=  GetWindowThreadProcessId(WindowName,@ProcessId);
WHandle  :=  OpenProcess(PROCESS_ALL_ACCESS,False,ProcessId);


for x := 10000000 to 100000000
begin
p := ptr( x );
ReadProcessMemory( WHandle, p, @puffer, 4, BytesRead );
end;

Edit1.Text := IntToStr(puffer);

end;

end.


Dieser Code hier ergiebt keinen Sinn, das weis ich. Aber kann mir jemand sagen wie ich die Schleife dazu bringe schneller zu arbeiten? Ich lese hier den Speicher des Programmes von 10000000 bis 100000000 aus, da ich weiss, dass sich kein gesuchter Wert unter 10000000 befindet. Die obere Grenze (100000000) hab ich selbst gesetzt. Kann es sein, dass diese Grenze etwas zu hoch ist?

Bin über jede Hilfe dankbar!

Vielen Dank
Jan Ebbe
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 10.12.05 16:11 
er muss auf jedenfall ziemlich viele durchgänge machen, ich denke daran liegt es
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 10.12.05 16:55 
Verwende zum Lesen einen größeren Puffer, dann geht es schneller.

Jeder Aufruf von ReadProcessMemory erzeugt einen Kontext-Switch, der IMMER sehr viel Zeit in Anspruch nimmt. Daher solltest Du, wenn möglich, die gesamten Daten mit einem Aufruf lesen.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Jan2k Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Sa 10.12.05 17:15 
Vielen Dank

Wie kann ich sowas anstellen BenBe? Ich bin neu in Delphi... kann nur Basic und Php^^ Könntest du mir mein Beispiel abändern?

THXXXXX
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 10.12.05 17:52 
Das könnte z.B. so hier aussehen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
{...}
var
  Form1: TForm1;
  WindowName  :  integer;
  ProcessId  :  integer;
  ThreadId  :  integer;
  WHandle  :  Integer;
  BytesRead : DWord;
  puffer : Array[10000000 .. 100000000] of Byte;
  p : pointer;
  x : integer;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
WindowName  :=  FindWindow(nil,'test');
ThreadId  :=  GetWindowThreadProcessId(WindowName,@ProcessId);
WHandle  :=  OpenProcess(PROCESS_ALL_ACCESS,False,ProcessId);

p := ptr( 10000000 );
ReadProcessMemory( WHandle, p, @puffer[10000000], SizeOf(Puffer), BytesRead );

//Weitere Nutzung...
end;


Kommst Du damit klar?

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Jan2k Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Sa 10.12.05 17:57 
Jap, jetzt verstehe ich es. Vielen Dank.

Edit: Nur noch eine kleine Frage. Wird hier der Speicher in 1 Byte oder 4 Byte Felder gespeichert? Ich brauche 4...


P.S. Ech ein nettes Forum hier... ich kenne solche da bekommt man als Antwort einfach einen Link zu Google oder zur Bordsuche...
Jan2k Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Do 29.12.05 15:46 
Halt!

Mich dünkt es, es wird immer nur in das erste feld des Arrays etwas geschrieben, alles andere bleibt leer... was mache ich falsch?

THX
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Do 29.12.05 16:07 
so wie es BenBE geschrieben hat muss es stimmen! sizeof(bufffer) gibt die die Größe zurück, die gelesen werden soll, in BytesRead steht drin wieviel wirklich gelesen wurde. da solltest du überprüfen ob es stimmt (BytesRead = sizeof(Buffer))

desweiteren solltest schauen ob if (ReadProcess...) auch überhaupt funktioniert hat.

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit
Jan2k Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Do 29.12.05 16:12 
Werden mit Dword 4 Bytes gelesen?
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Do 29.12.05 16:14 
ein dword ist 4 bytes groß ja, wenn du das so meinst musst dich halt klarer ausdrücken.

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit
Jan2k Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Do 29.12.05 16:22 
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
WindowName := FindWindow(nil, 'test');
  if WindowName = 0 then
  begin
    MessageDlg('Spiel nicht gestartet!', mtWarning, [mbOK], 0);
    exit;
  end;
  ThreadId := GetWindowThreadProcessId(WindowName, @ProcessId);
  WHandle := OpenProcess(PROCESS_ALL_ACCESS, False, ProcessId);


  x := 10000000;
  p := ptr(x);
  ReadProcessMemory(WHandle, p, @puffer[10000000], SizeOf(Puffer), BytesRead);


  repeat
    x := x + 1;
    MessageDlg(IntToStr(puffer[x]), mtWarning, [mbOK], 0);
    case puffer[x] of
      54321:
        begin
          ya := ya + 1;
          a[ya] := x;
        end;
      54322:
        begin
          yb := yb + 1;
          b[yb] := x;
        end;
      54323:
        begin
          yc := yc + 1;
          c[yc] := x;
        end;
      54324:
        begin
          yd := yd + 1;
          d[yd] := x;
        end;
      54325:
        begin
          ye := ye + 1;
          e[ye] := x;
        end;
      54326:
        begin
          yf := yf + 1;
          f[yf] := x;
        end;
      54327:
        begin
          yg := yg + 1;
          g[yg] := x;
        end;
      54328:
        begin
          yh := yh + 1;
          h[yh] := x;
        end;
      54329:
        begin
          yi := yi + 1;
          i[yi] := x;
        end;
      54330:
        begin
          yj := yj + 1;
          j[yj] := x;
        end;
      54331:
        begin
          yk := yk + 1;
          k[yk] := x;
        end;
      54332:
        begin
          yl := yl + 1;
          l[yl] := x;
        end;
    end;
  until x = 20000000;


Also, hier wird zuerst der Arbeitsspeicher ausglesen und danach in einer Schleife Feld für Feld das Array durchsucht. Nur leider findet er nichts. Mit einem Cheat Tool könnte ich feststellen, dass die gesuchten Werte wirklich in diesem Bereich liegen müssen. Kann mir jemand sage, was ich da falsch mache?

THX
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Do 29.12.05 16:47 
Haste wieder nicht das wichtigste kopiert.

Ist pbuffer ein array of dword?

Desweiteren ist es ein großer unterschied ob man bei 1000000 oder 1000001 anfängt und ob der bereich nicht doch eventl. in Hex dargestellt wird ($)

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit
Jan2k Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Do 29.12.05 16:53 
Ja, der buffer ist dword. Wie meinst du das mit hex?
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Do 29.12.05 17:01 
Jung du kommst von PHP nach Delphi, willst dann direkt was mit anderen Prozessen machen und hast keinen Plan vom Speichermanagement. Ich sagt dir bestimt nciht was 'hex' bedeutet, ein bisschen Eigeninitiative kann man ja wohl erwarten. In deinem Code sind noch so viele programmiertechnische Fehler bzw Unstimmigkeiten, vielleicht solltest du dir erstmal den Crashkurs anschaun. Ansonsten kann ich dir auch für ne Gegenleistung das Teil programmieren.
Aber jeden Pisselscode erklären zu müssen ist halt nicht der Sinn, fang bitte mit was leichterem an. denn schon die Wahl von dem Array, dem Anfangswert, das dann in dezimal, ohne Abfrage ob es funktioniert hat usw. ist super schlecht programmiert. Wenn du so weitermachst will ich deine späteren Quellcodes gar nicht sehen. Lieber klein anfangen aber dafür richtig.

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit