วันจันทร์ที่ 30 มกราคม พ.ศ. 2555

attactfiletoapplication

toring dGina.dll in a Delphi exe file
I've already written several articles about using resource files, in order to store (and use) sound files, video clips, animations and more generally any kind of binary files in a Delphi executable.

This time our oponent is dGina.dll. The process of storing dGina.dll inside your exe is simple as the following 5 steps.
Before you start make sure your Delphi project, for the sake of simplicity, has only one form (Form1) hosting the TdWinLock component.

0. Note: the following steps are described in detail in the article "Inside the EXE"

1. Create a new resource file (call it dGinaDLL.rc). This text document needs to have at least one line:
dGina RCDATA dGina.dll
2. Compile this resource using the brcc32 command line compiler. This will create dGinaDLL.res file.
3. Go to Project | View Source to display your Delphi PROJECT source file. It should look like:

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

4. Now replace the project source file with the next code:

program Project1;

uses
  Forms, Windows, Classes, SysUtils,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

{$R dGinaDLL.RES}  <-- DON'T FORGET

var
 rStream: TResourceStream;
 fStream: TFileStream;
 fname: string;
 sAppPath : string;
begin
  Application.Initialize;

  //start extract dGina.dll if not fond
    sAppPath:=IncludeTrailingPathDelimiter
              (ExtractFileDir(Application.ExeName));
   if not FileExists(sAppPath +'dGina.dll') then
   begin
     fname:=sAppPath+'dGina.dll';
     rStream := TResourceStream.Create
                (hInstance, 'dGina', RT_RCDATA);
     try
      fStream := TFileStream.Create(fname, fmCreate);
      try
       fStream.CopyFrom(rStream, 0);
      finally
       fStream.Free;
      end;
     finally
      rStream.Free;
     end;
   end;
  //stop extract dGina.dll if not fond

  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Quick description of the code:
In general, this is what the code does: Before the Project creates Form1 (hosting dWinLock), we need to make sure that dGina.dll can be found in the Project startup folder. We use the FileExists function to see if dGina.dll exists in the Application exe file's folder. If not found, the code looks "inside" the exe to extract the dGina.dll in the same folder. Once complete the dGina.dll will be saved and the "Big Brother Delphi Application" can proceed...

That's All!

ไม่มีความคิดเห็น:

แสดงความคิดเห็น