Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 12th, 2005, 6:26 AM   #1
karamellz
Newbie
 
karamellz's Avatar
 
Join Date: May 2005
Posts: 2
Rep Power: 0 karamellz is on a distinguished road
Hex Editing Module

Hello everyone! Yesterday I was very bored, and came up with the idea to make a module for hex-editing. I made two overloaded procedures for writing to the executable, with either a string or array of byte as input. I also made a procedure that converts a string into array of byte (StrToByte):
procedure StrToByte(input:string; Out output:array of byte);
var
i:byte;
begin
for i := 1 to Length(input) do
  begin
  output[i-1] := Ord(input[i]);
  end;
end;
var
   Stream:TFileStream;

.....................

procedure WriteInfo(buffer:array of byte; offset:integer; exepath:string);overload;
begin
Stream := TFileStream.Create(exepath, fmOpenReadWrite);
Stream.Seek(offset, soFromBeginning);
Stream.WriteBuffer(buffer, SizeOf(buffer));
Stream.Free;
end;

procedure WriteInfo(buffer:string; offset:integer; exepath:string);overload;
var
bytes:array of byte;
begin
SetLength(bytes, Length(buffer));
StrToByte(buffer, bytes);
Stream := TFileStream.Create(exepath, fmOpenReadWrite);
Stream.Seek(offset, soFromBeginning);
Stream.WriteBuffer(bytes, SizeOf(bytes));
Stream.Free;
end;
Now my problem is that when I try use the "string"-version of the WriteInfo (1) it writes a bunch of nonsense to the executable. When I instead make the stringconversion in my button and then call the "array"-version of my WriteInfo (2) it works as it should, even tho it does the same in both situations:
(1)
procedure TForm1.Button2Click(Sender: TObject);
begin
WriteInfo('Testing',$AFAFAF,'TestProg.exe');
end;

(2)
procedure TForm1.Button1Click(Sender: TObject);
var
bytes:array of byte;
begin
SetLength(bytes,7);
StrToByte('Testing', bytes);
WriteInfo(bytes,$AFAFAF,'TestProg.exe');
end;
If anyone knows what is wrong help would be greatly appreciated. Thanks in advance.
Karamellz~
karamellz is offline   Reply With Quote
Old Jun 24th, 2005, 2:29 AM   #2
Code Wizard
Newbie
 
Join Date: Jun 2005
Posts: 1
Rep Power: 0 Code Wizard is on a distinguished road
I'm not sure, there is several things that seem odd to me. But I've only been working with Delphi 4 & 5, nothing newer.
procedure StrToByte(input:string; Out output:array of byte);
I don't know that way of notation. Is this new? It used to be
procedure StrToByte(input:string; var output:array of byte);

And I think you can not pass "array of" in a "var" argument without defining its own type for it. (type A_O_B=array of byte; var output: A_O_B;).
But if the compiler doesn't complain, they seem to have fixed this.

Try passing "bytes[0]" instead of "bytes" to the Stream.WriteBuffer() method. IIRC Pascal handles arrays in memory a little different from C.
Code Wizard is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC