Thema: Delphi Batch Dateien

Einzelnen Beitrag anzeigen

Benutzerbild von retnyg
retnyg

Registriert seit: 12. Feb 2005
193 Beiträge
 
#12

Re: Batch Dateien

  Alt 21. Mai 2005, 07:36
Delphi-Quellcode:
procedure bat2exe(filename, strBatch: string);
const
  zero: dword = 0;

   ExeData: string =
      'MZ'#0#1#3#0#3#4#0#3#255#255#0#2#184#0#7'@'+
      #0#35#192#0#3#14#31#186#14#0#1#180#9#205'!' +
      #184#1'L'#205'!This program cannot be run in DOS mode.' +
      #13#13#10'$'#0#7'Y—8'#3#29#246'VP'#29#246'VP'#29#246+
      'VP“'#233'EP'#19#246'VP'#225#214'DP'#28#246'VPRich'+
      #29#246'VP'#0#24'PE'#0#2'L'#1#2#0#1'L¦ŽB'#0#8#224#0#1+
      #15#1#11#1#5#12#0#1#2#0#3#2#0#6' '#16#0#3#16#0#3' '+
      #0#4'@'#0#2#16#0#3#2#0#2#4#0#7#4#0#8'0'#0#3#4#0#6#2+
      #0#5#16#0#2#16#0#4#16#0#2#16#0#6#16#0#11#220#16#0#2+
      '<'#0#84#16#0#2' '#0#27'.text'#0#3#176#1#0#3#16#0#3+
      #2#0#3#4#0#14' '#0#2'`.data'#0#4#2#0#3' '#0#3#2#0+
      #3#6#0#14'@'#0#2#192#0#255#0#249'D'#17#0#2'R'#17#0#2+
      '`'#17#0#2'z'#17#0#2'n'#17#0#6''#17#0#6#232#7#0#3+
      'j'#0#1#232''#0#3'U‹'#236'ƒ'#196'¤j'#0#1'h'#0#3#8+
      'j'#2'j'#0#1'j'#1'h'#0#3'@hD @'#0#1#232'n'#0#3'‰E'#168+
      #184'M @'#0#1'P'#232'r'#0#3'‰E¤'#21#0#1' @'#0#1'j'+
      #0#1'R'#255'u¤hM @'#0#1#255'u'#168#232'P'#0#3#255'u'#168+
      #232'6'#0#3#199'E'#216#1#0#3'f'#199'E'#220#5#0#1'j'+
      #1'j'#0#1'j'#0#1'hD @'#0#1'h? @'#0#1'j'#0#1#232'0'+
      #0#3''#192'u'#0#1'3'#210#235#2'@B€8'#0#1'u'#249'+'+
      #194''#255'%'#0#1#16'@'#0#1#255'%'#4#16'@'#0#1#255+
      '%'#8#16'@'#0#1#255'%'#16#16'@'#0#1#255'%'#12#16'@'+
      #0#1#255'%'#24#16'@'#0#1#204#204#24#17#0#10''#17#0+
      #3#16#0#2'0'#17#0#10'¤'#17#0#2#24#16#0#22'D'#17#0#2+
      'R'#17#0#2'`'#17#0#2'z'#17#0#2'n'#17#0#6''#17#0#6+
      'user32.dll'#0#2#26#0#1'CloseHandle'#0#1'0'#0#1+
      'CreateFileA'#0#1''#0#1'ExitProcess'#0#1'ž'#2+
      'WriteFile'#0#1#191#2'lstrlenA'#0#2'kernel32.dll'+
      #0#2'g'#0#1'ShellExecuteA'#0#1'shell32.dll'#0#85+
      'Minimum MASM'#0#1' --- Assembler Pure and Simple --- '+
      #0#1'cmd.exe'#0#1'OPEN'#0#1'temp.bat'#0#1;

var
  f: file;

var
  i, c, l: integer;
  cnt: dword;
  lz, p: pchar;
  s:string;
begin
  s:=strBatch;
  l := length(s);
  if l < 434 then begin
    setlength(s,434);
  end;

// deflate-code ist von i.MacLeod ausm DF
  AssignFile(f, filename);
  try
    Rewrite(f, 1);
    p := Pointer(exedata); lz := p; c := 0; i := 0;
    while i < Length(exedata) do
    begin
      if p^ = #0 then
      begin
        BlockWrite(f, lz^, c);

        inc(p);
        cnt := dword(p^);

        BlockWrite(f, zero, cnt and 3);
        cnt := cnt shr 2;
        while cnt > 0 do
        begin
          BlockWrite(f, zero, 4);
          dec(cnt);
        end;

        inc(p); inc(i, 2);
        lz := p;
        c := 0;
      end else
      begin
        inc(p); inc(c); inc(i);
      end;
    end;
    BlockWrite(f, lz^, c);
    BlockWrite(f, Pointer(s)^, Length(s)+1);
  finally
    CloseFile(f);
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
 bat2exe('test4.exe', '@echo off'#13#10'echo hallo welt'#13#10'pause');
end;
der code eignet sich für batchdateien < 434 byte, für grössere batches müsste man noch die virtsize der .data section patchen, und s nochmal um 512 länger machen.

edit: #168 wurde nicht richtig dargestellt
  Mit Zitat antworten Zitat