adding the DLL to your executable

Actually adding the DLL to your executable is not difficult, just follow the few steps listed below, but you do have a small complication if your target computer's OS is Vista or newer. Vista doesn't allow an application to write to a directory within the Program
Files directory without explicit permission from the user (User Access Controls). So GetCurrentDir will not work for you, you'll have to find a different place where you can extract and save the DLL to, such as to the user's APPDATA directory - so you'd have to create a subdirectory for your application, and write the DLL into that subdirectory.

1) Create a text file (with Notepad.exe) and add a line to the text file in the following format

ResourceName RCDATA "PathToFile\FileName.DLL"

so pick a resource name such as MYDLL (you'll use this within your application to find the resource),

example:

MYDLL RCDATA "C:\directoryX\directoryY\File.DLL"

Save the file to the same directory where your source code for your application is, (make sure you save the file as plain ASCII and the extension to the file must be .rc not the normal .txt that Nopepad uses.

2) From within Delphi, and with your application loaded, use the Project Menu and select Add To Project. Add the resource file you just saved to the project.

3) Select Project | Build to build your application.

Now the DLL is part of your executable (you can confirm via the increased file size of your executable)

(If you make changes to the DLL, and want the updated DLL to be part of the application, make sure you "Build" your application, not just compile - compile does not update the attached resources - while "build" does).

Now to extract the DLL

Add the method to extract the DLL to your form's private section
Code:
TForm1 = class(TForm)
...
private
  proceducre ExtractResource(const SaveToPath: String);  
  ..
public
  ..
end;
Now add the code:
Code:
procedure TForm1.ExtractResource(const SaveToPath: String);
var
  rs: TResourceStream;
  fs: TFileStream;
begin
  rs := TResourceStream.Create(hInstance, 'MYDLL', RT_RCDATA);
  try
    fs := TFileStream.Create(SaveToPath+'FileName.DLL', fmCreate);
    try
      fs.CopyFrom(rs, 0);
    finally
      fs.Free;
    end;
  finally
    rs.Free;
  end;
end;
Call it like so:

Code:
SaveLocation := 'C:\SaveToLocationDirectory\';
ExtractResource(SaveLocation);

Komentar

Postingan populer dari blog ini

Install Packages dan extRoot 3020

menghitung daya watt amplifier dengan sopware