1

I have created an application that manage an embedded database

My Customer want this application to be in one file

My Task is to modify my application so it can extract database file from exe , edit it ,and include if again at run time not in compile time

2
  • Are you saying that you want to use your exe for read/write storage? Commented Mar 28, 2016 at 6:40
  • Why don't you have the exe create a self extracting archive? You'd need a new more things in the archive, but it would be easier to code. Commented Mar 28, 2016 at 17:04

2 Answers 2

9

An executable file cannot be modified while the executable is running. Which means that in order to achieve your goal you would need another process. You could do the following:

  1. Start your process.
  2. Extract the DB from the process image.
  3. Make changes to the DB.
  4. Extract another executable file from the original image.
  5. Start a second process based on this extracted images.
  6. Terminate the first process.
  7. Have the second process update the disk image with the modified DB.

Frankly this is a quite terrible idea. Don't even attempt this. The complexity serves no useful purpose, and the whole concept feels brittle.

Keep the data in a file separate from the program, as nature intended.

Sign up to request clarification or add additional context in comments.

1 Comment

I can understand why the customer would want this: It's just easier to copy a single file rather than copy a file + data. But you are right: It's a terrible idea to implement it that way. Maybe a different approach would be better: Create a copy of the executable and start that one, passing it the file name of the original file, and terminate. That copy then uses the original file as a data file. This gets rid of some of the nastiness.
3

This is of course a bad idea, just think of what virusscanners are going to think of this approach. Also what happens if the exe crashes, will your db now lose all of its updates?

You can have the exe create a self extracting archive containing all the files needed.

This works as follows (the steps are the same as @David above, except that the components listed do most of the work for you).

  1. Extract self extracting zip.
    this contains: the real exe to be started upon extract
    the database
    some files needed to recreate a new self extracting exe

  2. Upon closing the program it will create a new zip file, including:
    Itself (in readonly form) The database some files needed to recreate a new self extracting exe

  3. It will then transform the zip-file into a new self-extracting exe
    the new self-extracting archive will start the exe included in it's embedded zip file as per #1.

Here's some sample code from sfx-zip-delphi.

program SelfExtractingZip;

{$APPTYPE CONSOLE}

uses
  // Add a ZipForge unit to the program
  SysUtils, ZipForge, Classes;

var
  archiver : TZipForge;

begin
  // Create an instance of the TZipForge class
  archiver := TZipForge.Create(nil);
  try
  with archiver do
  begin
    // Set the name of the archive file we want to create.
    // We set extension to exe because we create a SFX archive
    FileName := 'C:\test.exe';
    // See SFXStub demo in ZipForge\Demos\Delphi folder
    // to learn how to create a SFX stub
    SFXStub := 'C:\SFXStub.exe';
    // Because we create a new archive,
    // we set Mode to fmCreate
    OpenArchive(fmCreate);
    // Set base (default) directory for all archive operations
    BaseDir := 'C:\';
    // Add the C:\test.txt file to the archive
    AddFiles('c:\test.txt');
    CloseArchive();
  end;
  except
  on E: Exception do
    begin
      Writeln('Exception: ', E.Message);
      // Wait for the key to be pressed
      Readln;
    end;
  end;
end.

Solutions for self extracting exe's

Paid
If you want a paid solution: http://www.componentace.com/sfx-zip-delphi.htm

Free
Or free: http://tpabbrevia.sourceforge.net/Self-Extracting_Archives
The abbrevia docs for self-extracting files are here: https://sourceforge.net/projects/tpabbrevia/postdownload?source=dlp
See page 293.

1 Comment

I like how your thinking outside the box on this one. A solution that takes in account the customers (i'll advised)requirements but should work with current systems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.