Delphi x DLL x Resources

1

I am creating a DLL in DELPHI, inside it there are some functions that my system will use. I would like to know the following, is there a way to embed this DLL created in DELPHI direct in the EXE through RESOURCES? Given that I need to create a PROCEDURE in FORM, how do I extract that DLL before the form "upload"?

I'm using the following to upload the DLL:

procedure DllMessage; external 'teste.dll'

I would like to embed this direct DLL in the executable.

    
asked by anonymous 17.10.2015 / 13:05

1 answer

2

Some steps are required to perform the desired procedure. First, let's embed the dll inside a .res file:

First create your dll and export it to some easily accessible place, so you can use the path more easily to generate the .res file.

  • Creating a file with the extension .rc :

    1.1 Open the notepad or any text editor and save with the extension .rc , for example: DLL_Resoures.rc

  • Entering the required information:

    2.1 In your RC file add the following information:
     DLLID RCDATA "DLLFileName.dll"

    • Identifier should be a name that is not repeated for each dll,
    • RCDATA specifies that it will be raw.
    • The dll file name must contain a valid path for the compiler to find this dll on your Disk.
  • Compiling the file .rc :

    To compile the .rc file, look for the BRCC32.exe executable inside the delphi bin folder, or open it at the command prompt. At the prompt, use the command:

    BRCC32.exe DLL_Resoures.rc

  • The Resource Compiler will generate a .res file with the same name as the .rc file by default, this name will be used to insert into your executable

  • Configuring the executable:

    4.1 You should define a location for this RES file to avoid having to manually handle 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 in the start 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 to the file, so if you set another place for the 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;
    
  • Since you are in the XE7 version you can only add delayed at the end of your external methods. As you will always extract the files before using, you will have no problem to run the application

    procedure DllMessage; external 'teste.dll' delayed;
    
        
    20.10.2015 / 16:56