Lazarus

Programming => General => Topic started by: AsleyCruz on April 04, 2020, 05:33:07 pm

Title: SOLVED: A simple project
Post by: AsleyCruz on April 04, 2020, 05:33:07 pm
Hi team

I have to create a simple project at my university and would like to create a "Installer Creator" / "Setup Creator"
like the attached image.

The program will work like this:
* Select directory of files
* Select destination folder (i.g. C\Program files\My Application)
* Compile files (create a setup with the forms designed in Lazarus)

This is the idea... but the big question is... How can I compile the files into a simple exe using FPC?
Maybe someone can help me and any idea is appreciated.

Thanks a lot coders.

Title: Re: A simple project
Post by: jamie on April 04, 2020, 05:53:43 pm
look at "Inno Setup" which creates an installable file in a single EXE if you wish..

I don't know about the program you are showing here.
Title: Re: A simple project
Post by: AsleyCruz on April 04, 2020, 05:57:38 pm
look at "Inno Setup" which creates an installable file in a single EXE if you wish..

I don't know about the program you are showing here.

Hi Jamie
I know about Inno Setup and others installers creators but in this case I would like to create a simple
installer creator (it is a university project). I just need to know how to compile files into an exe file using Free Pascal.

Thanks.
Title: Re: A simple project
Post by: Handoko on April 04, 2020, 07:02:39 pm
I'm not sure how others do it but I ever did such the thing you mentioned. Actually, what you want to do is not simple, you really need some or maybe good programming knowledge to be able to do it.

Here is how I did it:

First, I wrote code to merge to 2 binary files into a single file. But I have to put a marker between them. The first part is the installer itself, then marker, and the last is the data: exe + marker + data.

The installer itself has the ability to load itself's file to memory and scan for the position of the marker. So it can know where is the position the data started.

Then I improved the installer to be able extract the data to files. The data (in the memory) actually is the compressed files (into a single file in memory), so I can uncompressed them back to files and save them to the disk.

I haven't write code make it automatically compress the files into single file, it is not hard but I can simply compress files using my Linux file manager.

The installer I wrote actually is a 2 programs in a single exe file:
- It can scan itself, separate the data and uncompressed files to disk
- It can merge binary and zip file into a single binary

I wrote it as a feature for my game builder. It works, I quickly tested on Linux and Windows. But I think it may trigger some antivirus or Windows security issue.
Title: Re: A simple project
Post by: 440bx on April 04, 2020, 07:10:00 pm
I just need to know how to compile files into an exe file using Free Pascal.

Thanks.
It's quite simple.  Compile/build the .exe then, include the .exe as a binary resource file in your installer.  That way the .exe is just another resource in the installer program.  I'd give you some code but, I only know how to do it using straight Windows API.  I have no clue how to get that done in the Lazarus environment.  Hopefully someone else can fill that part.
Title: Re: A simple project
Post by: AsleyCruz on April 04, 2020, 07:28:49 pm
I'm not sure how others do it but I ever did such the thing you mentioned. Actually, what you want to do is not simple, you really need some or maybe good programming knowledge to be able to do it...

Interesting work. Maybe you could share us some code to have a better idea.
Thanks
Title: Re: A simple project
Post by: AsleyCruz on April 04, 2020, 07:29:51 pm
I just need to know how to compile files into an exe file using Free Pascal.

Thanks.
It's quite simple.  Compile/build the .exe then, include the .exe as a binary resource file in your installer.  That way the .exe is just another resource in the installer program.  I'd give you some code but, I only know how to do it using straight Windows API.  I have no clue how to get that done in the Lazarus environment.  Hopefully someone else can fill that part.

Please, share your code. It will be easier to have an idea on how to do it..
Thanks a lot
Title: Re: A simple project
Post by: lucamar on April 04, 2020, 07:34:56 pm
It's quite simple.  Compile/build the .exe then, include the .exe as a binary resource file in your installer.  That way the .exe is just another resource in the installer program.  I'd give you some code but, I only know how to do it using straight Windows API.  I have no clue how to get that done in the Lazarus environment.  Hopefully someone else can fill that part.

Shouldn't be too difficult combining a TResourceStream to read the binary with a TFileStream to save it to file.

Note also that it's relatively easy to compile the "original", copy it to the "setup" project folder and build the "setup" project (so as to include the newly created exe). Just a matter of using FindAllFiles() to find the original .lpi, a TProcess to execute lazbuild, CopyFile() to copy and another (or the same) TProcess to build the installer. Piece of cake :D
Title: Re: A simple project
Post by: fabiopesaju on April 04, 2020, 08:01:51 pm
https://github.com/jrsoftware/issrc

can this help you?
Title: Re: A simple project
Post by: 440bx on April 06, 2020, 01:25:27 am
Please, share your code. It will be easier to have an idea on how to do it..
Thanks a lot
Sure.

Here is a little test program I wrote over 20 years ago.  Some of the comments refer to Windows 95, you can ignore them but, they apply to current versions of Windows too.

When you load the .lpr select "Simple program" not anything else because if you do, Lazarus will overwrite the .res file with a res file it creates and doesn't even warn you that the res file is overwritten.

Also, the .rc file included is compiled with Borland's resource compiler (BRCC32.exe) I doubt it can be compiled with something else.

This test program defines binary data directly in the .rc file (have a look in the .rc if you are curious.)  To replace that data with your executable, use ResourceHacker to edit the .res file (or the resulting program directly, your choice.)  You will find resource hacker at http://www.angusj.com/resourcehacker/  it is a very nice and very useful utility.

In this test program, the binary data is displayed in the main window during the processing of the WM_PAINT.  In the case of an embedded executable, it's quite unlikely you'd want to do that.  Instead you'd probably want to load the resource and write it out in some other part of your program.  Where the code should go in your program is up to you.

I know Lazarus most likely provides a way to do this with less code but, as I stated before, I only use OS functions, Windows API in this case, therefore I have no clue how the same thing is done the Lazarus/FPC way.

You're welcome.  Hopefully, it will be helpful to you in spite of being pure Windows API.



Title: Re: A simple project
Post by: avk on April 06, 2020, 04:19:46 pm
It seems that there is another (more Lazarus-ish?) way to save separate binary files inside your application. Somewhere I had the simplest non-visual component that allows you to load binary data at design time and use it at runtime. If you are interested, I will find it.
Title: Re: A simple project
Post by: AsleyCruz on April 06, 2020, 07:41:34 pm
It seems that there is another (more Lazarus-ish?) way to save separate binary files inside your application. Somewhere I had the simplest non-visual component that allows you to load binary data at design time and use it at runtime. If you are interested, I will find it.
Hi avk, it would be great if you share that component. Thanks in advance.

Thank to all of you... still looking for a way to bind files in a stub through Lazarus.
Title: Re: A simple project
Post by: avk on April 06, 2020, 08:55:13 pm
Well, I hope you find it useful.
Title: Re: A simple project
Post by: AsleyCruz on April 08, 2020, 02:25:03 am
Well, I hope you find it useful.
Many thanks avk. It works great.

It saves the file into the exe but in design time.
The component has only two procedures: SaveToStream and SaveToFile.

Is it possible to save any file into the exe but in run time?
I have tried but nothing.

Thanks again for your time.
Title: Re: A simple project
Post by: 440bx on April 08, 2020, 02:29:20 am
Is it possible to save any file into the exe but in run time?
Only if you create a copy of the exe at runtime because, while the exe is running, the O/S won't allow you to make any changes to its file.
Title: Re: A simple project
Post by: jamie on April 08, 2020, 02:31:52 am
so you are trying to make a self extraction system...

 The best solution is to write a small console based bootloader that knows its own file size so that
when started, it will read at the end of the file and thus read all the attached files.

  Of course you also need another app that is combine these together..
Title: Re: A simple project
Post by: AsleyCruz on April 08, 2020, 02:46:41 am
so you are trying to make a self extraction system...

Yes Jamie, it is a little project at the college.
Let's say:
- Load some files with main .exe.
- Generate a new .exe with the files saved inside.
- Open the new generated .exe and select the folder to extract the files.

I hope to achieve this goal.
Title: Re: A simple project
Post by: jamie on April 08, 2020, 02:53:05 am
Ok, I kind of figured that is what you are after..

You need to create a small installer.EXE program.. basically a sub program or boot loader

that will read its own file from disk but move the file pointer to a known position and start reading the attached files..

 Your main program needs to add these files at the end but you also need to directory structure at the start of each one that will report the size of each chunk and also the name of the file..
etc

 But first you need to make the little EXE program that reads at the end of itself from disk
Title: Re: A simple project
Post by: avk on April 08, 2020, 07:42:52 am
@AsleyCruz, I want to note that I used this component specifically for a small installer.

UPD: added a small example.
TinyPortal © 2005-2018