在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
{$R filename} {$R *.xxx} The $R directive specifies the name of a resource file to be included in an application or library. The * symbol has a special meaning in $R directives: {$R filename.res filename.rc} (where the two occurrences of 'filename' match) When a {$R filename} directive is used in a unit, the specified file name is simply recorded When an application or library is linked (after compiling the program or library source file), How to attach a resource file to an existing executable file?I have a resource file(.RES) and i want to add it into an existing executable file {$APPTYPE CONSOLE} uses Classes, Windows, SysUtils; procedure UpdateExeResource(Const Source,Dest:string); var Stream : TFileStream; hDestRes : THANDLE; lpData : Pointer; cbData : DWORD; begin Stream := TFileStream.Create(Source,fmOpenRead or fmShareDenyNone); try Stream.Seek(0, soFromBeginning); cbData:=Stream.Size; if cbData>0 then begin GetMem(lpData,cbData); try Stream.Read(lpData^, cbData); hDestRes:= BeginUpdateResource(PChar(Dest), False); if hDestRes <> 0 then if UpdateResource(hDestRes, RT_RCDATA,'DATA',0,lpData,cbData) then begin if not EndUpdateResource(hDestRes,FALSE) then RaiseLastOSError end else RaiseLastOSError else RaiseLastOSError; finally FreeMem(lpData); end; end; finally Stream.Free; end; end; begin try UpdateExeResource('C:\Users\Dexter\Documents\RAD Studio\Projects\Debug\Win32\Data.txt','C:\Users\Dexter\Documents\RAD Studio\Projects\Debug\Win32\project86.exe'); except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. You can use Colin Wilson's excellent Resource Utilities. http://www.wilsonc.demon.co.uk/files/d10/ResourceUtils100.zip Resource Utilities is a runtime package that contains classes that manipulate resources program AddResource; {$APPTYPE CONSOLE} uses SysUtils, Classes, unitNtModule, unitResFile, unitResourceRCData; procedure AddRes(exeName, resName: string); var exeModule: TNTModule; resFile : TResModule; begin if ExtractFileExt(exeName) = '' then exeName := ChangeFileExt(exeName, '.exe'); exeModule := TNTModule.Create; try exeModule.LoadFromFile(exeName); resFile := TResModule.Create; resFile.LoadFromFile(resName); exeModule.AddResource(resFile.ResourceDetails[0]); exeModule.SaveToFile(exeName); finally FreeAndNil(exeModule); end; end; { AddRes } begin if ParamCount <> 2 then Writeln('Usage: AddResource <exe file> <resource file>') else AddRes(ParamStr(1), ParamStr(2)); end. Uses Classes, Windows, SysUtils, Dialogs; Type TBuffer = Array[0..0] of Byte; PBuffer = ^TBuffer; Var FS : TFileStream; ResourceHandle : THandle; DataLength : DWord; Data : PBuffer; Ok : Boolean; Begin ResourceHandle := BeginUpdateResource(pChar('d:\someexefile.exe'), False); IF (ResourceHandle <> 0) Then Begin FS := TFileStream.Create('d:\somebitmap.bmp', fmOpenRead); FS.Seek(0, soFromBeginning); DataLength := FS.Size; GetMem(Data, DataLength); FS.Read(Data^, DataLength); FS.Free; Ok := True; IF (not UpdateResource(ResourceHandle, RT_RCDATA, pChar('MyNewResource'), LANG_SYSTEM_DEFAULT{MakeLangID(LANG_NEUTRAL, SUBLANG_NEUTRAL)}, Data, DataLength)) Then Ok := False; IF (not EndUpdateResource(ResourceHandle, False)) Then Ok := False; IF (Ok) Then ShowMessage('Update of resources successful!') Else ShowMessage('Update of resources failed!'); FreeMem(Data); End; End.
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论