Resource file (Delphi)
{$R filename}
{$RESOURCE filename}
{$R *.xxx}
{$R 'My file'}.
{$R filename.res filename.rc}
The $R directive specifies the name of a resource file to be included in an application or library.
The named file must be a Windows resource file and the default extension for filenames is .res.
To specify a file name that includes a space, surround the file name with single quotation marks:
{$R 'My file'}.
The * symbol has a special meaning in $R directives:
it stands for the base name (without extension) of the source-code file where the directive occurs.
Usually, an application's resource (.res) file has the same name as its project (.dpr) file;
in this case, including {$R *.res} in the project file links the corresponding resource file to the application.
Similarly, a form (.dfm or nfm) file usually has the same name as its unit (.pas) file;
including {$R *.nfm} in the .pas file links the corresponding form file to the application.
{$R filename.res filename.rc} (where the two occurrences of 'filename' match)
makes the .rc file appear in the Project Manager.
When the user opens the .rc file from the Project Manager, the String Table editor is invoked.
When a {$R filename} directive is used in a unit, the specified file name is simply recorded
in the resulting unit file.
No checks are made at that point to ensure that the filename is correct
and that it specifies an existing file.
When an application or library is linked (after compiling the program or library source file),
the resource files specified in all used units as well as in the program or library itself are processed,
and each resource in each resource file is copied to the executable being produced.
During the resource processing phase, the linker searches for .res files in the same directory
as the module containing the $R directive, and in the directories specified in the Search path input box
on the Directories/Conditionals page of the Project|Options dialog box
(or in the directories specified in a -R option on the dccil command line).
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
without recompiling and using the IDE! is it possible?
{$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
in Windows modules (.EXE, .DLL, .BPL, etc.) and resource files (.RES).
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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本