Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Little-endian Number to Float (in bytes) routine

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
MaDDoX
How do I cheat?
Reputation: 0

Joined: 03 Aug 2009
Posts: 1

PostPosted: Mon Aug 10, 2009 11:12 am    Post subject: Little-endian Number to Float (in bytes) routine Reply with quote

Hey guys, I'm coding a trainer in Delphi 7 and it's working just fine, the problem is that the game's numeric (with decimals) values are stored as Float in memory. I can update the values in memory correctly by checking Cheat Engine's memory browser window in Float, editing the numeric value to what I want then changing the display to byte to know the byte sequence I should write to memory. That'd do just fine if all I had to mod were a couple a fixed values, but I want the user to be able to input some of those. The full conversion process seems quite complicated from what I've researched, so I was thinking if any of you guys already have a routine to do it that you could share. I can convert C code to delphi no problem if needed.

I'd be super-appreciative if you can help or give me some hints. Thanks in advance!
Back to top
View user's profile Send private message
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Mon Aug 10, 2009 11:22 am    Post subject: Reply with quote

Floating point numbers work in a little bit more complex way. (IEEE 754: en.wikipedia.org/wiki/IEEE_754-2008)

You can easily solve this problem, if your inside the process memory then in C, you could've done something like:
Code:
*(float*)Address = 3.14159;

Or just have a buffer of 4 bytes (or 8 for double) and copy the memory to an array of bytes (or simply convert, if the compiler will allow) like this:
Code:
float x = 3.14159;
WriteProcessMemory(Handle, (BYTE*)x, sizeof(x), ...);
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 474

Joined: 09 May 2003
Posts: 25973
Location: The netherlands

PostPosted: Mon Aug 10, 2009 11:28 am    Post subject: Reply with quote

delphi's floating point variable types are single and double
so:
Code:

var myvalue: single;
...
myvalue:=99.9;
WriteProcessMemory(handle, @myvalue, sizeof(myvalue), ....);


also, StrToFloat is a nice function to look into
and you can just do:
floatvalue:=intvalue; (not the other way around though)

_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
MaDDoX
How do I cheat?
Reputation: 0

Joined: 03 Aug 2009
Posts: 1

PostPosted: Tue Aug 11, 2009 4:00 pm    Post subject: Reply with quote

Dark Byte wrote:
delphi's floating point variable types are single and double
so:
Code:

var myvalue: single;
...
myvalue:=99.9;
WriteProcessMemory(handle, @myvalue, sizeof(myvalue), ....);


OMG, it worked.. so all I had to do was directly assign the float value to the right type of variable? Jeez.

Thanks a lot for the great help, I thought I'd be flamed or something, you guys rock Smile

Here's my (WIP) code in case anyone wants it:

Code:
procedure TForm1.BtnLaunchClick(Sender: TObject);

var pID: Cardinal;
    hProcess: Cardinal;
    lpNumOfBytes: Cardinal;
    lpBuffer: Integer;
    wrtBuffer: Single;
begin
  try
    GetWindowThreadProcessId(FindWindow(nil, 'Z Steel Soldiers'), pID);
    hProcess := OpenProcess(PROCESS_ALL_ACCESS, FALSE{!!}, pID{!!});
    //7E22BC + C = Tough Unit + Vision Ofs
    if ReadProcessMemory(hProcess, Ptr($007E22BC), @lpBuffer, sizeof(lpBuffer), lpNumOfBytes) = false then label2.caption := 'Read Error';
    lpBuffer:=lpBuffer+$C;   //Apply Offset
    wrtBuffer := 11.0; // New value, original is 14
    if WriteProcessMemory(hProcess, Ptr(lpBuffer), @wrtBuffer, sizeof(wrtBuffer), lpNumOfBytes) = false then label2.caption := 'Write Error';
  finally
    CloseHandle(hProcess);
  end;
end;
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites