Help!.. My program needs to read it's own exe file
Posted on 1999-07-28
I can't get a delphi program to open it's own .exe file on disk for read access :( for example.. a program 'test.exe'
needs to open and read the file 'test.exe' so that it can make a new copy of itself.... 'newtest.exe' I know it can somehow be done because other windows programs open their own file easily.... my hex editor can easily open itself and read the data in... it won't write to it but reading works just fine...Aspack can open itself as well... so HOW can I do this using Delphi? all my attempts fail :( here is the source for the latest attempt... what am I doing wrong? or what am I not doing right?
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, ComCtrls, Gauges;
TForm1 = class(TForm)
procedure Button1Click(Sender: TObject);
procedure TForm1.Button1Click(Sender: TObject);
SrcFile, DestFile: File;
BytesRead, BytesWritten, TotalRead: Integer;
Buffer: array[1..500] of byte;
Reset(SrcFile, 1); // Open source file for read access.
Rewrite(DestFile, 1); // Open destination file for write access.
TotalRead := 0;
FSize := FileSize(SrcFile);
BlockRead(SrcFile, Buffer, SizeOf(Buffer), BytesRead);
if BytesRead > 0 then begin
BlockWrite(DestFile, Buffer, BytesRead, BytesWritten);
if BytesRead <> BytesWritten then
raise Exception.Create('Error copying file')
TotalRead := TotalRead + BytesRead;
ProgressBar1.Position := Trunc(TotalRead / Fsize) * 100;
until BytesRead = 0;
CloseFile(DestFile); // Close destination file.
CloseFile(SrcFile); // Close source file.