Witam. Mam problem gdyż chcę zapisać zmieniony zasób do postaci exe.
Wiem że pierwsza procedura potrafi zmieniać headera pliku (bity informujące o wielkości exeka).
Funkcja zamienia te nazki i zapisuje to do pliku. Jest ładnie ale jak chcę uruchomić exe to się k***a nie da...
Zmienna S to znaki do podmiany.Od godziny 18 prubowałem to zrobić ale nie mogłem sobie poradzić i prosze o pomoc.
Procedura do zmiany headera wyciągnięta za generatora (http://mhaker.pl/pages-print-59.html):
procedure SaveToExe(FName, Str: string);
var
F : File of Byte;
I : Integer;
begin
AssignFile(F, FName);
Reset(F);
try
Seek(F, FileSize(F) - SizeOf(I));
BlockRead(F, I, SizeOf(I));
if (I < SizeOf(I)) or (I > FileSize(F)) then I := 0;
Seek(F, FileSize(F) - I);
Truncate(F);
BlockWrite(F, Str[1], Length(Str));
I := Length(Str) + SizeOf(I);
BlockWrite(F, I, SizeOf(I));
finally
CloseFile(F);
end;
end;
Funkcja zamieniająca znaki:
function ReplaceTextInStream(SourceStream : TStream; TextToFind, NewText : string) : TMemoryStream;
var
B : Byte;
I : int64;
X : integer;
S, TmpS : string;
MS : TMemoryStream;
begin
MS := TMemoryStream.Create;
Result := TMemoryStream.Create;
if (TextToFind = '') or (NewText = '') then
begin
Exit;
end;
TmpS := Copy(TextToFind, 1, 1);
MS.LoadFromStream(SourceStream);
MS.Position := 0;
I := 0;
repeat
S := TmpS;
MS.Position := I;
MS.Read(B, 1);
Result.Write(B, 1);
//
if Length(TextToFind) = 1 then
begin
TextToFind := Chr(Integer(B));
end;
//Zamiana Textu
if B = Byte(TextToFind[1]) then
begin
for X := 1 to Length(TextToFind) - 1 do
begin
MS.Read(B, 1);
S := S + Chr(Integer(B));
end;
end;
if (S = TextToFind) then
begin
Result.Position := Result.Position - 1;
Result.Write(PChar(NewText)^, Length(NewText));
I := I + Length(TextToFind);
end
else
begin
I := I + 1;
end;
until I = MS.Size;
MS.Free;
end;
To wywołuje funkcję:
var
InFile,OutFile: String;
MS : TMemoryStream;
RS : TResourceStream;
Start, Stop : Cardinal;
TF : TextFile;
S : String;
begin
InFile:=ParamStr(1);
OutFile:=ParamStr(2);
AssignFile(TF, InFile);
Reset(TF);
Read(TF, S);
CloseFile(TF);
RS := TResourceStream.Create(HInstance, 'exec', RT_RCDATA);
Start := GetTickCount;
MS := ReplaceTextInStream(RS, 'PAUSE', S);
MS.SaveToFile(OutFile);
Stop := GetTickCount;
Writeln('#> Czas wykonania funkcji: ' + IntToStr(Stop - Start));
end;