線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3990
推到 Plurk!
推到 Facebook!

exemod.pas

 
jessechan
版主


發表:109
回覆:394
積分:254
註冊:2002-04-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-08-25 15:31:45 IP:203.67.xxx.xxx 未訂閱
exemod.pas    This is the first beta of the full version 1.00 That allows an ExeMod exe to work properly even from a CD..for info on how an exe on CD is handled please see the notes in the Interface section.    Written by G.A. Carpenter (works on D2 thru D7) This is FreeWare for any purpose...use as you will. Consider it public domain... feel free to make any changes you want... if you improve it I'd love to get a copy of the improvements :-)    gacarpenter386@yahoo.com  You can use this in any program, commercial or freeware, and you DO NOT need to mention where it came from. (no attribution needed) If you use NT/2000/XP please read the NT-NOTE below! This unit enables you to make a delphi application that can alter its own exe file on disk... this appears to happen at runtime but actually the switch quickly takes place after a shutdown of the running app during which a copy of the exe deletes the old version and replaces it with a new version. While it is also possible to alter a running exe directly by altering the hd sectors that hold the exe's data. I abandoned that scheme because although it worked quickly and transparently it proved to be risky :-( This unit lets you easily create... -Self altering exe files -Program Generators -Apps that update live over the internet -Apps that keep an internal record of data (no ini file or registry usage needed) -File handling utils -Copy protection schemes -Self extracting archives -Programs that alter and store data in other programs and files. ...and many other strange things ;-) NOTE! ExeMod will not delete a demarc delimited string from an external file if the deletion would take the file down to 0 bytes in length! This is because some disk cleanup utils can be set to automatically remove 0 length files. If you intend to use an external file to hold data that will be handled by ExeMod you should begin by placing at least a single byte of data at the beginning of the file. ------ NT-NOTE! If you use ExeMod.pas on an NT machine then you may need to run the exe outside the delphi ide... for some reason NT causes the debugger to faint at the sight of my code :-) the exe files run just fine on NT but just not from inside the ide.... ------- Here are some simple demos for you to look over. (Remember to put ExeMod.pas into your compiler's path or into your project's directory... then add ExeMod to your program's uses clause) ------------------------------------------------------------------------ -Code to alter a byte in an exe and then alter the exe at runtime and put the change onto the exe's file on disk procedure TForm1.Button1Click(Sender: TObject); begin ReadExe; //Read exe file from disk and put into global string named Exe Exe[3] := 'A'; //Change byte 3 of Global string Exe AlterExe; //Alter the exe file on disk then quickly restart program end; ------------------------------------------------------------------------ -Code to add a file from disk to the end of the exe at runtime. Assume a small txt file named C:\myfile.txt exists and contains the text 'test' procedure TForm1.Button1Click(Sender: TObject); begin AddFile2Exe('gac1','C:\myfile.txt'); AlterExe; end; The string 'gac1' serves to locate the data at the end of the exe. I call gac1 a 'Demarc name' because when combined with a few other characters it serves as a demarcation line in the exe file... this gives you an easy way to search through the file and find the location of the start of your data. Here is what is added to the exe at runtime when you click the button SO!#GAC1鈣estEO!#GAC1 The data from the file is the 4 bytes 'test' all the rest is just delimiter data so that ExeMod can locate the data in the exe file. The Character '? in the string is to tell the program where the name given to the data string ('GAC1') ends. it is chr(182) please do NOT use chr(182) as a character in any demarc name or you will cause an error! AddFile2Exe will load the exe from disk if it has not already been loaded... it checks to see if Exe = '' and if so does a ReadExe; ----------------------------------------------------------------------- -Code to extract an ExeMod data block from the exe at runtime and save it to a file on disk... then remove the block from the exe and alter the exe on disk so that it no longer contains any trace of the data block. procedure TForm1.Button1Click(Sender: TObject); begin ExtractAndStrip('gac1','myfile2.txt'); AlterExe; end; This extracts the ExeMod data block gac1 to a file named 'myfile2.txt' the file is placed into the directory where the program's exe file is located. This sort of code is good for making a self-extracting installation program in delphi. All of your help files,dlls..etc can be extracted during the first run of your program... exe size will be small because the data will be removed from the exe after extraction. All files will be extracted to the program's dir unless a full path name is given. ----------------------------------------------------------------------- Code to save a copy of the running exe procedure TForm1.Button1Click(Sender: TObject); begin Exe2File('MyNewExe.exe'); Exe := ''; //set Exe to empty string (save space if data no longer needed) end; Saves a copy of the running exe as myNewExe.exe in the same directory where the running exe is. If Exe has been altered the altered version will be saved... if Exe has not been loaded yet then ReadExe is called. ----------------------------------------------------------------------- Code to save a modified copy of the running exe... this simple code gives you a program generator. procedure TForm1.Button1Click(Sender: TObject); begin ReadExe; //add code here to modify the string named 'Exe' Exe2File('MyNewExe.exe'); Exe := ''; //set Exe to empty string (save space if data no longer needed) end; This simple code is actually VERY powerful and VERY useful... ----------------------------------------------------------------------- ---> Much more Demo code and many more procedures to come..soon as I get some more free time :-) procedure Delay(ms: longint); procedure ReadExe; procedure String2File(String2BeSaved, FileName: string); function WinDrv: char; function GetDemarcCount: integer; procedure GetDemarcName(DNumber: Integer; var DName: String); function File2String(FileName: string): string; function PeekExeByte(Byte2Get: Integer): byte; function PeekExeWord(Word2Get: Integer): word; procedure PeekExeString(StartByte,Count: Integer; var ReturnedStr: String); procedure PokeExeByte(Byte2set: Integer; ByteVal: Byte); procedure PokeExeByteI(Byte2set: Integer; ByteVal: Byte); procedure PokeExeString(StartByte: Integer; String2Insert: String); procedure PokeExeStringI(StartByte: Integer; String2Insert: String); procedure ExtractFromExe(DemarcStr: string; var ExtractedStr: string); procedure ExtractFromFile(DemarcStr: string; DataFile: string; var ExtractedStr: string); procedure DelFromString(DemarcStr: string; var String2Change: string); procedure DelFromExe(DemarcStr: string); procedure DelFromFile(DemarcStr, FileName: string); procedure Add2File(DemarcStr, FileName, String2Add: string); procedure ReplaceInFile(DemarcStr, FileName, ReplacementString: string); procedure TackOnFile(DemarcStr, FileName, File2Add: string); procedure Add2String(DemarcStr, String2Add: string; var String2Alter: string); procedure ReplaceInString(DemarcStr, ReplacementString: string; var String2Alter: string); procedure ReplaceInExe(DemarcStr, ReplacementString: string); procedure InsOrReplaceInString(DemarcStr, ReplacementString: string; var String2Alter: string); procedure InsOrReplaceInExe(DemarcStr, ReplacementString: string); procedure AlterExe; procedure ExtractAndStrip(DemarcStr, FileName: string); procedure Exe2File(FileName: string); procedure Extract2File(DemarcStr, FileName: string); procedure AddFile2Exe(DemarcStr, FileName: string); procedure Add2Exe(DemarcStr, String2Add: string); procedure Stream2Exe(TempStream: TMemoryStream); procedure String2Stream(a: String;var b: TMemoryStream); procedure Stream2String(b: TMemoryStream;var a: String); Jesse Chan
------
Jesse Chan
附加檔案:36294_ExeMod.pas
takdick
一般會員


發表:50
回覆:63
積分:22
註冊:2002-08-05

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-06-22 19:53:36 IP:218.102.xxx.xxx 未訂閱
我有一程序的源碼如下: procedure TForm1.Button1Click(Sender: TObject); var fme:TMemoryStream; clf:TfileStream; size:integer; stf:string; begin stf:=ExtractFilePath(Paramstr(0)) 'test.exe'; if fileexists(stf) then deletefile(stf); fme:=TMemoryStream.Create; clf:=Tfilestream.Create(application.ExeName,fmShareDenyNone); try clf.Seek(-sizeof(size),soFromEnd); clf.ReadBuffer(size,sizeof(size)); clf.Seek(-size,soFromEnd); fme.CopyFrom(clf,size-sizeof(size)); fme.SaveToFile(stf); finally fme.Free; clf.Free; end; procedure TForm1.Button2Click(Sender: TObject); VAR f:TEXTfile; st:string; F1,F2:TFileStream; i,SS:integer; st:string; begin assignfile(f,ExtractFileDir(Application.Exename) '\st.txt'); st:=edit1.Text '>>>' edit2.Text '>>>' edit3.Text '>>>'; rewrite(f); writeln(f,st); closefile(f); st:=ExtractFileDir(Application.Exename) '\st.txt'; F1:=TfileStream.Create(ExtractFileDir(Application.Exename '\temp.exe',fmOpenWrite); F2:=TfileStream.Create(st,fmOpenRead); try F1.Seek(0,soFromEnd); F1.CopyFrom(F2,0); SS:=F2.Size sizeof(SS); F1.WriteBuffer(SS,sizeof(SS)); application.MessageBox ('文件已生成'); form1.Enabled :=true; form4.Visible :=false; finally F1.Free; F2.Free; deletefile(st); end; 我知道Button1是生成源文件,Button2是把edit輸入的信息加入到源文件裡再生成一個新的執行文件, 我想問一下Button1調用的源文件放在哪裡?我找不到呀. 編譯後在執行Button1時提示'Stream read error',是否調用源文件的位置錯誤?
bugmans
高階會員


發表:95
回覆:322
積分:188
註冊:2003-04-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-02-23 10:38:23 IP:125.225.xxx.xxx 未訂閱
這個元件可以將其他的檔案塞到exe檔內,本文所附的exemod.pas只是其中一部份而已
在原作者的網站http://www.geocities.com/gacarpenter386/
有完整的範例提供下載http://www.torry.net/vcl/vcltools/codertools/exemod.zip
還有新版的exemod.pas可以下載http://www.geocities.com/gacarpenter386/Latest.zip
還有其他的範例就請你到作者網頁瀏覽

Ktop相關文章
在 Runtime 直接在 Exe 改變圖片示範
http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=31613
將上一篇改成BCB的寫法
http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=31723
也是建議使用exemod
http://delphi.ktop.com.tw/board.php?cid=30&fid=72&tid=73676

想自己動手作的話可以利用資源檔達到相同的功能
http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=57442

系統時間:2024-11-21 19:50:17
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!