[ mica99 @ 21.07.2015. 01:26 ] @
Hocu da napravim mali programcic koji upisuje neke vrednosti u exe ili dll fajl tipa user name i password. Sa borland delphi 7 sam to uspeo pomocu citanje i pisanje konstanti ali u ebarcadero to ne radi. Nasao sam ovo http://edn.embarcadero.com/article/27979 Zatim sam nasao i kod koji radi dobro ali ima jednu manu. Moze da se upisuje samo po jedna vrednost. tj. svaaka sledeca vrednost brise prethodnu Evo i programa pomocu kojeg mogu da generisem i upisujem password, ali kako mogu da na drugu poziciju postavim drugu vrednost.? unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, Spin; //const //our ExeBuffer signature // ExeBufSig = 'EB1.0'; type //the Footer for our executable format TExeBufFooter = record OriginalSize : Integer; Sig : Integer; // Array[0..4] of char; end; TExeBuf = array of char; TForm1 = class(TForm) btn1: TBitBtn; btn2: TBitBtn; dlgOpen1: TOpenDialog; edt1: TEdit; edt2: TEdit; se1: TSpinEdit; se2: TSpinEdit; procedure btn1Click(Sender: TObject); procedure btn2Click(Sender: TObject); procedure FormCreate(Sender: TObject); private procedure SetExeData (ExeName : String; ExeBuf : TExeBuf ; ExeBufSig:integer); procedure GetExeData (ExeName : String; var ExeBuf : TExeBuf;ExeBufSig:integer); procedure StringToExeBuf (const S : String; var ExeBuf : TExeBuf); function ExeBufToString (const ExeBuf : TExeBuf) : String; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} function RandomPassword(PLen: Integer): string; var str: string; begin Randomize; //string with all possible chars str := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; Result := ''; repeat Result := Result + str[Random(Length(str)) + 1]; until (Length(Result) = PLen) end; procedure TForm1.SetExeData (ExeName : String; ExeBuf : TExeBuf ; ExeBufSig:integer); var F : File; BufSz,OrigSz : Integer; Footer : TExeBufFooter; begin AssignFile (F,ExeName); Reset (F,1); try //obtaining the original file size OrigSz := FileSize(F); //go to the EOF of the file Seek (F,OrigSz); //Writing our custom data beyond the EOF BufSz := Length(ExeBuf); BlockWrite (F,Pointer(ExeBuf)^,BufSz); //Writing our footer FillChar (Footer,SizeOf(Footer),0); Footer.OriginalSize := OrigSz; Footer.Sig := ExeBufSig; BlockWrite (F,Footer,Sizeof(Footer)); finally CloseFile (F); end; end; procedure TForm1.GetExeData (ExeName : String; var ExeBuf : TExeBuf;ExeBufSig:integer); var F : File; CurrSz, BufSize : Integer; OldFileMode : Integer; Footer : TExeBufFooter; begin AssignFile (F,ExeName); //Saving the old FileMode OldFileMode := FileMode; //Setting the FileMode to ReadOnly FileMode := 0; try Reset (F,1); try //Getting the current file size CurrSz := FileSize (F); //Seeking to the footer position //and reading it Seek (F,CurrSz-SizeOf (Footer)); BlockRead (F,Footer,Sizeof(Footer)); //if there's no signature, boom! //no data in this executable! if Footer.Sig <> ExeBufSig then begin //raise EExeBuf.Create ShowMessage ('No Data in EXE!'); Exit; end; //calculating the buffer size that was written //to this executable file BufSize :=CurrSz-Footer.OriginalSize-SizeOf(Footer); SetLength (ExeBuf,BufSize); //seek and read! Seek (F,Footer.OriginalSize); BlockRead(F,Pointer(ExeBuf)^, BufSize); finally CloseFile (F); end; finally //returning to the previous saved //FileMode FileMode := OldFileMode; end; end; procedure TForm1.StringToExeBuf (const S : String; var ExeBuf : TExeBuf); begin SetLength(ExeBuf,Length(S)*SizeOf(Char)); Move (Pointer(S)^,Pointer(ExeBuf)^,Length(S)*SizeOf(Char)); end; function TForm1.ExeBufToString (const ExeBuf : TExeBuf) : String; begin SetLength (Result,Length(ExeBuf) div SizeOf(Char)); Move (Pointer(ExeBuf)^,Pointer(Result)^,Length(ExeBuf)* SizeOf(Char)); end; procedure TForm1.btn1Click(Sender: TObject); var ExeBuf, ExeBuf2 : TExeBuf; begin if not dlgOpen1.Execute then Exit; StringToExeBuf(edt1.Text, ExeBuf); SetExeData(dlgOpen1.FileName, ExeBuf,se1.Value); //----'c:\Project1.exe' end; procedure TForm1.btn2Click(Sender: TObject); var ExeBuf, ExeBuf2 : TExeBuf; begin if not dlgOpen1.Execute then Exit; GetExeData(dlgOpen1.FileName, ExeBuf2,se2.Value); // ShowMessage(ExeBufToString(ExeBuf2)); edt2.Text :=ExeBufToString(ExeBuf2) end; procedure TForm1.FormCreate(Sender: TObject); begin edt1.text:=RandomPassword(20); end; end. |