Some steps are required to perform the desired procedure. First, we will embed the dll inside a file .res
:
First create your dll and export it somewhere easy to access, so you can use the path more easily to generate the file .res
.
Creating a File with the Extension .rc
:
1.1 Open the Notepad or any text editor and have it saved with the
extension .rc
, for example: Dll_resoures.rc
Inserting the necessary information:
2.1 In your RC file add the following information:
Identifier DATE "Nomerquivodll.dll"
- The Identifier must be a name that does not repeat for each dll,
- RCDATA specifies that raw data will be.
- The dll file name must contain a valid path for the compiler to find this dll on its disk.
Compiling the file .rc
:
To compile the file .rc
, look for the executable BRCC32.exe
inside the Delphi bin folder, or open it at the command prompt. At the prompt, use the command:
BRCC32.exe DLL_Resoures.rc
The Resources compiler will generate a file .res
with the same file name .rc
by default, this name will be used to insert inside your executable
Configuring the executable:
4.1 You must set a location for this RES file to avoid having to manually control every time you make changes to the DLL, for ease of example, just copy the . RES into the EXE application folder and add the following line at the beginning of the EXE project:
program SeuPrograma;
uses
X, Y, Z;
{$R *.res}
(* Adicione seu RES aqui *)
{$R DLL_RESOURCES.res}
.....
The {$R} directive allows you to specify an absolute and relative path for the file, so if you set another place for res, just set a different path, for example:
(* Irá buscar o res um diretório acima *)
{$R ..\DLL_RESOURCES.res}
Extracting the RES and saving the DLL:
5.1 Create the following function:
procedure ExtrairDLL(const NomeResource, NomeArquivo: String);
var
RStream: TResourceStream;
begin
RStream:= TResourceStream.Create(HInstance, NomeResource, RT_RCDATA);
try
RStream.Seek(0, soFromBeginning);
RStream.SaveToFile(NomeArquivo);
finally
RStream.Free;
end;
end;
5.2 In creating your form call it by passing the following arguments:
procedure TForm1.FormCreate(Sender: TObject);
begin
ExtrairDLL( (* Nome da Resource: Mesmo nome que você colocou no arquivo DLL_RESOURCES.RC *)
'IdentificadorDLL',
(* - IncludeTrailingPathDelimiter: Incluiu uma / (barra) no final do caminho
- ExtractFilePath: Retorna apenas o caminho do nome de um arquivo
- ParamStr(0): Recupera o nome do executável: É o primeiro parâmetro do executável *)
IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + 'NomeArquivoDLL.dll'
);
end;
As you are in XE7 version you can just add delayed
at the end of their methods external
. As you will always extract the files before using, you will have no problem running the application
procedure DllMessage; external 'teste.dll' delayed;
@Eprogrammernotfound, actually, is as follows. Some functions will run in DLL, not to have to deliver the project to the client with multiple DLL, would like to put in the EXE (using Resource) and when the form is created (CREATE FORM) it unzips these DLL, understand ? is possible will be ? the DLL’s are created in the same Delphi itself.
– user7605
It’s even possible, but why not use the good old installer? Or compress into a zip file?
– EProgrammerNotFound
Eprogrammernotfound, because I want to deliver only the executable for the client to open and ready. Anyway, for several other reasons here at the company, how could it be possible to do this ? could help me ?
– user7605
@Eprogrammernotfound, this I wanted to do even, because if I use Static linking, it will never go up even rsrss, I wanted to use something like LOADLIBRAY even, could give me this strength ?
– user7605
Thanks friend @Eprogrammernotfound, I tried to do using FREELIBRAY, but I don’t know what I call the function within the DLL using it rsrsrs.
– user7605
What version of Delphi are you using? In new versions you can use the delayed keyword and maybe you can keep the code in the current form. I’m sorry I didn’t answer sooner, I’ve been busy at work, I’m
– EProgrammerNotFound
@Eprogrammernotfound, I use DELPHI XE7.
– user7605
What is the difference between using the installer that will extract the DLL and you do that ? The file anyway won’t stay in the folder ? This anyway would not go against "the company’s motives"?
– Felippe Tadeu