delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (5)


loading an exe in a memo fieldGo to Teun spaans's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
how can i load exec file into memo or richedit
Product:
Delphi all versions
Category:
Files Operation
Skill Level:
Scoring:
Last Update:
06/27/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet fileopen, tmemo, executable
Times Scored:
6
Visits:
4685
Uploader: Teun spaans
Company: pinkroccade
Reference: N/A
 
Question/Problem/Abstract:
How can I read a binary file?
How can I show a binary file in a memo field?

This article has been writtten in reply to an old artcile request by the same name by Ismael U.
Answer:










How to load an exec in a memo field




How to load an exec in a memo field


Why?


This article has been written in answer to an old request by ismael u, asking
how an executable can be loaded in a memo or rich memo field.


First a remark, executables should usually not be stored in a tmemo field,
but rather in some blob field. However, there are some occasions on which one
would like to view an executable. Studying (differences between) compiled
executables comes to mind.


I assume that Ismael means executable when he says exec, and the solution is
rather simple. 


How?


Loading a an executable in a memo field basically comes down to 2 steps. The
first step is reading the file from disk and loading the file into memory, the
second step is showing the loaded contents in the tmemo field. 


The first step, reading the file from disk and loading it into memory, is
rather easy. Perhaps TFileStream could be used, but I prefer the rather low
level FileOpen function because of its performance. Also, when working with
binary files, we must keep in mind that these files may contain #0 and many
pointer based operations regard this as an end/of/string character.


Basically, here is the code, mostly a copy of the delphi5 help after fixing
some minor bugs. Just create a form, add a button and a fileopendialog, 



  
    
      
    
  
procedure TForm1.Button1Click(Sender : TObject);

        var

  iFileHandle : Integer;

  iFileLength : Integer;

  iBytesRead : Integer;

  Buffer : PChar;

        begin

          opendialog1.filter := 'executables|*.exe';

          if opendialog1.Execute then

          begin

            try

            iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead or fmShareDenyNone);

            if iFileHandle > 0 then

            begin

              iFileLength := FileSeek(iFileHandle, 0, 2);

              FileSeek(iFileHandle, 0, 0);

              Buffer := PChar(AllocMem(iFileLength + 2));

              iBytesRead := FileRead(iFileHandle, Buffer^, iFileLength);    // note that ^ is missing in D5 help.

              FileClose(iFileHandle);

            end;

            finally

              FreeMem(Buffer);

            end;

          end;

        end;


The second step again poses us some questions. As the contents of the binary
file will contain #0, how will we show them?


The first way is to convert the entire Buffer read above into a string and
add this string to the memo. Doing this causes no technical problem, but the
memo shows just a few characters. That's probably now what we want. The cause
are the aforementioned #0 characters.


The second way is to go through the Buffer bit by bit, and switch to a new
line whenever we encounter a #0. Doing so is easy, and reveals that an ordinary
executable contains lots of #0 characters.


The third and probably best way is to show all characters in a hexagonal
notation. 



  
    
      
    
  
procedure TForm1.Button1Click(Sender : TObject);

        var

         iFileHandle : Integer;

         iFileLength : Integer;

         iBytesRead : Integer;

         Buffer : PChar;

         i,linelength : integer;

         s : string;

         line : string;

         c : char;

         ordval, ordval1, ordval2 : integer;

        begin

         opendialog1.filter := 'executables|*.exe';

         if opendialog1.Execute then

         begin

         try

          iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead or fmShareDenyNone);

          if iFileHandle > 0 then

          begin

           iFileLength := FileSeek(iFileHandle, 0, 2);

           FileSeek(iFileHandle, 0, 0);

           Buffer := PChar(AllocMem(iFileLength + 2));

           iBytesRead := FileRead(iFileHandle, Buffer^, iFileLength);    // note that ^ is missing in D5 help.

        

           // 3 ways of conversion and show:

           // way 1: exe will contain \0 so this code shows only part of exe

           memo1.lines.add('way 1*********************************************');

           s := string(Buffer);

           memo1.lines.add(s);

        

           // way 2: use \0 as newline for purpose of displaying in memo1.

           memo1.lines.add('way 2*********************************************');

           LineLength := 0;

           Line:= '';

           For i:= 0 To iFileLength-1 do

           begin

            If Buffer[i] = #0 then

            begin

             memo1.lines.add(Line);

             LineLength := 0;

             Line := '';

            end

            else

            begin

             inc(LineLength);              // perhaps provision should be added for LineLength > max delphi stringlength

             Line := Line+Buffer[i];       // memo1 will handle normal new line chars

            end;

           end;

        

           // way 3: display every char as ord

           memo1.lines.add('way 3*********************************************');

           Line:= '';

           For i:= 0 To iFileLength-1 do

           begin

            c := Buffer[i];

            ordval := ord(c);

            ordval1 := ordval div 16;

            ordval2 := ordval mod 16;

            Line := Line + '0123456789ABCDEF'[ordval1+1]+'0123456789ABCDEF'[ordval2+1];

            if Length(Line) = 80 then

            begin

             memo1.lines.add(line);

             line := '';

            end;

           end;

        

           FileClose(iFileHandle);

          end;

          finally

           FreeMem(Buffer);

          end;

         end;

        end;

      


 


 


 


 











Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
loading an exe to a blob field
    Greg Washburn (Dec 5 2002 8:46AM)

Reading the file worked great for me.  Instead of viewing it in a memo control, I want to store it in a blob field.  Casting the buffer to a string works great if the file read is a text file:
s := string(Buffer);
This doesn't work if it's a binary file (exe,,jpg,gif,etc).  The buffer is fine but s is empty.  Can these files be stored in String vars?  My goal is to eventually:

MyQueryMYBLOB.Value := Buffer;

How can this be done?
Respond

Source formatting....
    S7eG n/a (Jul 3 2002 2:22PM)

Do you always write your code with a line space between each line? .. Are you paid by the quantity (not quality) of your source codes.? :)
Respond

RE: Source formatting....
Teun spaans (Aug 19 2002 10:19AM)

unintended result when pasting source in MS Frontpage....
Respond

Thought this may of been some use
    Matt Harrison (Jun 28 2002 3:11PM)

There is an inbuild delphi function (which I think appears in pre delphi 6)

BinToHex(Buffer, Text: PChar; BufSize: Integer);

which would create an output buffer in hex format.

Might be of some use .......


Respond

RE: Thought this may of been some use
Teun spaans (Jul 2 2002 10:26AM)

The function is indeed available in D5, though the helpfile doesn't know about it's existence!

Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
S. Carter
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)