Delphi x DLL x Resources

Asked

Viewed 1,477 times

1

I am creating a DLL in DELPHI, within 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 on EXE through RESOURCES ? Since I need to create a PROCEDURE in FORM, how to extract this DLL before the form "Upload" ?

I am using the following to upload the DLL:

procedure DllMessage; external 'teste.dll'

I would like to embed this DLL directly into the executable.

  • @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.

  • It’s even possible, but why not use the good old installer? Or compress into a zip file?

  • 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 ?

  • @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 ?

  • 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.

  • 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, I use DELPHI XE7.

  • 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"?

Show 3 more comments

1 answer

2


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.

  1. 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

  2. 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.
  3. 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

  1. 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} 
  1. 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;
  • Thank you @Eprogrammernotfound, everything worked out fine.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.