Posts: 2,709
Threads: 328
Joined: Apr 2022
Reputation:
219
On Discord, we were talking about various things (as usual -- all you guys are invited to join us there!), and the topic came up about how to retrieve the source code from the EXE. With the new embedding commands, it's simple as heck to embed the BAS source into your program and then retrieve it and restore it later.
For example, something as simple as this works for us:
Code: (Select All)
'Step One: Embed the source code into your EXE
$Embed:'MyProgramName.Bas','sourcehandle'
'Step Two: Assign a variable to represent the source code
source_code$ = _Embedded$("sourcehandle")
'Step Three: Write a simple routine to retrieve the source
c$ = UCase$(Command$)
If c$ = "-SOURCE" Then
Open "MyProgramName.tmp" For Output As #1 'so you don't overwrite the original source while testing
Print #1, source_code$
Close #1
System
End If
We have now added a command line paramater to the EXE which is trigged by "-SOURCE", and which dumps the source file to the drive for us.
It doesn't get much simpler than this.
Feel free to make use of this style code for any/all of your own progams where you might want to store the source with the EXE for future reference/retrieval.
Posts: 3,997
Threads: 180
Joined: Apr 2022
Reputation:
222
What are some uses of this? I can think that you might want to compare the EXE source with other .bas sources to see maybe which the EXE came from? I doubt I guessed main use.
b = b + ...
Posts: 2,709
Threads: 328
Joined: Apr 2022
Reputation:
219
To extract the source back from the EXE in the future, in case you can't find it anywhere anymore.
Posts: 3,997
Threads: 180
Joined: Apr 2022
Reputation:
222
Ah in case I accidently delete the .bas instead of the EXE. Thanks
b = b + ...
Posts: 2,709
Threads: 328
Joined: Apr 2022
Reputation:
219
(10-20-2024, 03:55 PM)bplus Wrote: Ah in case I accidently delete the .bas instead of the EXE. Thanks
Or, as was posted so perfectly on the Discord:
Quote:Weird question, but is there a way to get back the .bas version of a .exe please? I made programs, but I lost the .bas, and only have the .exe
Which is why, if you embed the source with the EXE, you never have that issue. It's a great way of doing things, if you tend to create EXE files and use them as various utilities on your PC and yet upgrade QB64PE versions into new folders and such, and those BAS source files get lost/buried/archieved elsewhere over time.
For less than 10 lines of code, you can now write your programs so that the EXE itself will contain the BAS sources and you can always retrieve it for you. (14 above, with lines of space and comments which can be stripped out easily.) It's one of those little utilities/habits that anyone could adopt incredibly easy, yet which we all tend to just gloss over and overlook (a lot like adding comments to code when we're in the zone and coding like a drunk monkey on cheetos). It's also the type of thing which we always look back on and then go, "GAH! WHY DIDN'T I DO THIS?!!"
As I like to watch people squirm and suffer, I just had to share this. Now, in the future when they ask about getting their source code back from the EXE, I can giggle like an Evil Overlord and point them to this thread and say, "I KNEW THIS WAS GOING TO HAPPEN! If you guys would just all listen to me, like good little minions... errr.... programmers!"
Posts: 67
Threads: 18
Joined: Aug 2022
Reputation:
12
(10-20-2024, 09:58 AM)SMcNeill Wrote: On Discord, we were talking about various things (as usual -- all you guys are invited to join us there!), and the topic came up about how to retrieve the source code from the EXE. I have seen this before, it is a ...
(10-20-2024, 12:23 PM)bplus Wrote: What are some uses of this? I can think that you might want to compare the EXE source with other .bas sources to see maybe which the EXE came from? I doubt I guessed main use. ... self-extracting archive.
Back in the MSDOS/pre-Windows 3.1 days, i.e. the mid 1980s until the 1990s, someone got the idea of saving disk space - it was very expensive back then (I bought a large-capacity hard drive around then, and had to max out my credit card to afford the $401 (plus tax, call it another $20), and got a massive capacity 80 MB drive.) Well, then they started including other files in with applications to be distributed. Which brings up a chicken-and-egg problem, how do you unpack the archiving/unarchiving program when you don't have an unarchiver? So, there was invented the "self-extracting archive" unpacking stub program. It was attached to the front of the archive and the archive extension was changed to .exe. The stub code would unpack the archive into its component files.
"And everything old is new again."
While 1
Fix Bugs
report all bugs fixed
receive bug report
end while
Posts: 202
Threads: 5
Joined: Apr 2022
Reputation:
22
It's still possible to create self-extracting archives using any of the major archive utilities (7Zip, WinZip, Rar, whatever).
This feature is more reminiscent of (but not) a quine ( https://en.wikipedia.org/wiki/Quine_(computing)) than an archive.
It seems to me that one could store other types of data using $Embed then extract it at run-time using _Embedded$, but for archival purposes a dedicated archive utility such as 7Zip would be much more efficient. A utility like 7Zip will give much better compression and the extractor stubs used by archivers are much smaller than the minimum PE program size.
Posts: 733
Threads: 103
Joined: Apr 2022
Reputation:
14
(10-20-2024, 01:59 PM)SMcNeill Wrote: To extract the source back from the EXE in the future, in case you can't find it anywhere anymore. Or just as a diagnostic tool, or as a way to provide some transparency into what the program is doing.
Of course there's no guarantee the source code is really where the EXE came from - unless we can added some kind of checksum and saved the compiler version the EXE came from, and a "verifier" utility that uses that info to verify the binary against the code?
Either way, I just think it's a neat option to be able to include the source code with the EXE.
|