Autor Beitrag
lam0r
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Di 21.06.05 12:13 
Hallo!
Bin neu hier, und ehlich gesagt froh, das Forum hier gefunden zu haben -> deutschsprachig und gut besucht!
Hier mein Problem: Ich will für die neue Battlefield 2 Demo nen SinglePlayer Trainer schreiben, habe auch schon ne gute Vorlage für Delphi gefunden, ich poste die hier einfach mal:
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:
program trainer;

{$R DLG.RES}

uses
 Windows,
 Messages;

const
 ID_DLG = 101;
 ID_INFO = 1;
 ID_QUIT = 2;

 // If you want to get the window by the window caption, assign the GAME_CAPTION constant
 // and assign <nil> to the GAME_CLSNAME constant. If you wanted to get it by the class
 // name you could assign <nil> to GAME_CAPTION and the game's class name to GAME_CLSNAME.
 GAME_CAPTION = 'Notepad'; // This is the game window caption.
 GAME_CLSNAME = nil;       // This is the game window class name.


 // Below is the message that will show when they click the Info button in the dialog box
 INFO_MESSAGE = 'This is a FIXED Delphi trainer engine.' + #13#10 +
                'Originally coded by MiCRaL, fixed by spookie';

// This is the dialog box's message procedure.
function DialogProc(hWin, uMsg, wParam, lParam: dword): Integer; stdcall;
begin
 case uMsg of
   WM_DESTROY: EndDialog(hWin, 0); // If we get the WM_DESTORY message, end the dialog.
   WM_COMMAND: // If we get the WM_COMMAND message (user pressed a button)...
     case wParam of
       ID_QUIT: EndDialog(hWin, 0); // If it's the <quit> button, end the dialog.
       // If its the <info> button, show the info messagebox.
       ID_INFO: MessageBox(hWin, INFO_MESSAGE, 'Info', MB_OK or MB_ICONINFORMATION);
     end;
 end;
 // Return 0.
 Result:= 0;
end;

procedure Poke(Address: Cardinal; Bytes: Array Of Byte);
var
 hWin: hWnd;
 ProcId: Cardinal;
 hProc: THandle;
 NBW: Cardinal;
begin
 // Find the game's window using the constants defined at the top of this file.
 hWin:= FindWindow(GAME_CLSNAME, GAME_CAPTION);
 if hWin = 0 then begin // If we cant find the window...
   // Display "game not running" message.
   MessageBox(0, 'The game is not running.', 'Error', MB_ICONERROR);
   Exit; // Exit the <poke> procedure.
 end;
 GetWindowThreadProcessId(hWin, @ProcId); // Get the game's process id.
 hProc:= OpenProcess(PROCESS_ALL_ACCESS, False, ProcId); // Open the game process.
 // Write to the game's memory!
 WriteProcessMemory(hProc, Ptr(Address), @Bytes[0], Length(Bytes), NBW);
 CloseHandle(hProc); // Close the game process handle.
end;

procedure DoTimer;
begin
 // The below 4 lines check if a key is pressed, then Poke the memory if it's been
 // pressed.
 If (GetAsyncKeyState(VK_F1) and 1) = 1 Then Poke($41D090, [$AB, $CD, $EF]);
 If (GetAsyncKeyState(VK_F2) and 1) = 1 Then Poke($4CC0FF, [$01, $02]);
 If (GetAsyncKeyState(VK_F3) and 1) = 1 Then Poke($41EF23, [$7F]);
 If (GetAsyncKeyState(VK_F4) and 1) = 1 Then Poke($57DC02, [$01]);
 // So if F1 is pressed, 3 bytes (0xAB, 0xCD, 0xEF) get written to 0x41D090
 // and if F2 is pressed, 2 bytes (0x01, 0x02) get written to 0x4CC0FF etc.
end;

begin
 SetTimer(0, 0, 250, @DoTimer); // Set the hotkey checking timer.
 // Show the dialog box.
 DialogBoxParam(hInstance, MAKEINTRESOURCE(ID_DLG), 0, @DialogProc, 0);
 ExitProcess(hInstance); // Exit the program when the dialog box closes.
end.

Ist nicht von mir, wollt ich nur mal anmerken. So, mein Problem ist folgendes:
Batlefield 2 (Die Demo) ändert bei jedem Start nicht nur die Caption sondern auch die Class. Dabei sind diese beiden immer absolut identisch. Das sieht in etwa so aus:
Caption= "BF2 (v1.2442.3, pid:*ProzessID*)" d.h. die ProzessID wird in die Caption und die Class mit übernommen.
Ich habe schon die SUFU hier bemüht, bin aber leider zu keinem brauchbaren Ergebnis gekommen. Meine Frage ist nun: Wie kann ich das Handle oder die pid des Fensters von Battlefield ermittlen, ohen daß ich die Caption/Class (genau) kenne? In C und VB gibts glaub ich die Funktion "FindWindowLike", da kann man mit Wildcards arbeiten.
Vielen Dank im Voraus!
-lam0r

Moderiert von user profile iconMotzi: Code- durch Delphi-Tags ersetzt.
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: Di 21.06.05 12:16 
:welcome:

aber die exe ändert sich net, dann such doch das fenster nach dem namen der exe und alles ist in butter
lam0r Threadstarter
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Di 21.06.05 12:19 
das ging echt flott, danke für die antwort :)
genau das ist mein problem, wie heißt der befehl dafür?
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: Di 21.06.05 12:20 
ausblenden Delphi-Quelltext
1:
findwindow					
lam0r Threadstarter
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Di 21.06.05 12:23 
hmm, soweit war ich doch auch schon, versteh ich grad was falsch? ich dachte, findwindow geht nur mit caption und/class...

ausblenden Delphi-Quelltext
1:
hWin:= FindWindow(GAME_CLSNAME, GAME_CAPTION);					
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: Di 21.06.05 12:25 
handle := findwindow('bf', nil);

bf ist der name der exe datei 8)
lam0r Threadstarter
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Di 21.06.05 12:27 
wenn das wirklich funzt, dann schon mal ein riesen dankeschön! :roll:
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: Di 21.06.05 12:29 
hast du auch schon die richtigen adressen in die du schreibst, ansonsten würde ich das writeprocessmemory erstmal weglassen, sonst zerschießt du den speichern deines spiels :lol:

vielleicht solltest du dir mal das game trainer tutorial hier im forum anschauen, das ist echt ausführlich und gut erklärt, da sind auch schon viele fragen beantwortet :wink:
lam0r Threadstarter
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Di 21.06.05 13:14 
sorry, ich kriegs einfach nicht hin...
Wo in dem Trainer Template (s.o.) muss ich das denn wie genau einbauen? Weil wenn ich anstatt der Caption einfach nur 'BF2' bzw 'BF2.exe' eintrag, funzt es nicht. Ich bin halt noch Anfänger, was die programmierung der Windows API angeht. Wäre nett, wenn mir das jemand (am besten noch mit Source) erklären könnte...
Vielen Dank
-lam0r

Achso, das mit dem Speicher zerschießen ist mir schon ein paar mal passiert, mit nem Trainer Maker Kit funzen die Poke Codes jetzt auch, von daher möchte ich eigentlich nur wissen, wie man das ganze in Delphi umsetzen kann. Ausserdem ist das einzige Trainer Kit, das funktioniert TMK, und da werden die EXE Dateien 200 kb und größer.
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: Di 21.06.05 13:42 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure Poke(Address: Cardinal; Bytes: Array Of Byte);  
var  
  hWin: hWnd;  
  ProcId: Cardinal;  
  hProc: THandle;  
  NBW: Cardinal;  
begin  
  hWin:= FindWindow('bf2', nil);  
  if hWin = 0 then 
    begin  
      MessageBox(0, 'Bitte erst das Spiel starten!', 'Error', MB_ICONERROR);  
      Exit;.  
    end;  
  GetWindowThreadProcessId(hWin, @ProcId);  
  hProc := OpenProcess(PROCESS_ALL_ACCESS, False, ProcId);  
   
  WriteProcessMemory(hProc, Ptr(Address), @Bytes[0], Length(Bytes), NBW);  
  CloseHandle(hProc);
end;

so gehts nicht ?

wenn du was freezen willst (also bytes noppen), dann kannst das auch hiermit machen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
function NopBytes(Exename, Title: pchar; Address: integer; Bytes: byte): boolean;
const
  nop = $90;
var
  Handle, ThreadID, ProcessID: Integer;
  buf: PChar;
  write: cardinal;
begin
  result := true;
  Handle := FindWindow(Exename, Title);
  if Handle = 0 then result := false;
  GetWindowThreadProcessId(Handle, @Handle);
  Handle := OpenProcess(PROCESS_ALL_ACCESS, False, Handle);
  GetMem(buf, 1);
  buf^ :=  Chr(nop);
  WriteProcessMemory(Handle, ptr(address), buf, 4, write);
  FreeMem(buf);
  closehandle(Handle);
end;

Beispiel aufruf (adresse kann inkorekt sein, also lieber eigene benutzen :P)
ausblenden Delphi-Quelltext
1:
2:
  if not nopbytes('hl2', nil, $2221839C, 6) then
    showmessage('Bitte starten Sie erst das Spiel bevor Sie den Trainer aktivieren!');

die anzahl der bytes kriegt man leicht raus, wenn man zb bei TSearch eine Funktion nopt, dann kann man nachzählen (kann sein, dass es auch dabei steht)
lam0r Threadstarter
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Di 21.06.05 13:53 
Hey Danke, jetzt geht's! Hab kein Plan, woran es lag :) Vielen Dank nochmal!!!
retnyg
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2754

SNES, GB, GBA, CPC, A500, 486/66, P4/3.0HT: NintendOS, AmigaOS, DoS
Delphi 5, Delphi 7
BeitragVerfasst: Di 05.07.05 18:14 
@fear: is dir schonmal aufgefallen dass deine nop-prozedur immer 4 nops schreibt, egal was man bei byte übergibt ?
dann doch lieber das hier www.delphi-forum.de/...der=asc&start=60

und bei findwindow kommt zuerst die klasse, dann der titel :roll:

_________________
es gibt leute, die sind genetisch nicht zum programmieren geschaffen.
in der regel haben diese leute die regel...
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: Di 05.07.05 21:03 
cih weiß, ist mir kürzlich aufgefallen als ich einen Trainer geschrieben habe, auf meinem Pc habe ich das natürlich angepasst, die Variable hatte gar keien Verwendung das hat mich verwundert, habs halt vergessen :lol: