Including media files directly in the exe?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Including media files directly in the exe?

24 Jan 2018, 12:11

Hi guys,

I was already searching for a solution but haven't found anything yet.
I'm working on a Gui which contains pictures and sounds.
Normally these files are in my AHK dir.
However, I would like to include them directly in the exe.

So once the code has been compiled and I put the script on some other PC,
I would like that it's just an exe file.
Sure it's no problem to include a folder which contains all pictures and sounds,
but there are millions of tools which just consist of a single exe file.
For this reason I thought AHK might be able to realize that too.

Is it possible?

Best regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: Including media files directly in the exe?

24 Jan 2018, 12:13

There's no easy way to push items into the program, however, FileInstall is a good way to start.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Including media files directly in the exe?

24 Jan 2018, 17:45

Hi,

From what I read I see no difference.
"Later, when the compiled script is run, the files are extracted back out onto the disk."
So it doesn't matter if I create a zip file which contains the exe + the media files
or if I use FileInstall.
The end result will be the same...

Any way to create some kind of package?
I just want to prevent that there is 1 exe and 200 files inside of a folder.
This makes a dumb impression, in my opinion.
1 exe and 1 pack file would be fine, too.
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Including media files directly in the exe?

25 Jan 2018, 01:54

There may be millions of single file programs but there are many millions more that are not. The reason being in general, in most programming language it is a pain. Most computers are full of folders that hold programs' files.

Some little programs go through the trouble of doing it.

One easy approach on NTFS systems is to put the support files in the alternate data stream of the exe file.

Below is a link that shows that approach.
https://autohotkey.com/boards/viewtopic.php?t=27719

AHK scripts are text files. Even compiled AHK is just a glorified self-extracting text file. So to really store stuff within an AHK script you have to convert the data to characters and then back to your data from the characters. Usually it is converted characters of hex data. It can definitely be done and their are several examples on the forum. It is clumbersome but it does add a nice touch to a script.

To not make a "dumb impression", you actually have to be pretty smart in your coding skills.

Luckily, just me (the user, not me), made a utility that makes it somewhat less painful.
https://gist.github.com/AHK-just-me/5559658

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: Including media files directly in the exe?

25 Jan 2018, 03:27

There is also a version of the ahk2exe compiler that can add resources , i only tried the demo but it seems to work for images and html.
https://autohotkey.com/boards/viewtopic.php?f=24&t=521

I use the method FanaticGuru suggested ( using just me 's include) but only for a few images because it is a lot of work !!

Running sound files from a memory buffer without creating a "real" file only works for WAV format as far as i know , they are ten times bigger as its mp3 counterpart!
Even a small alarm sound gives you 700kb added size.

If you have 200 images fileinstall will be the best option.........
Guest

Re: Including media files directly in the exe?

25 Jan 2018, 11:11

Scr1pter wrote:I just want to prevent that there is 1 exe and 200 files inside of a folder.
Jumping through hoops just to be "clean" isn't very helpful (neither for yourself nor your user(s)) - you can of course have your EXE in the main folder and all the resources in sub-folders (images + sound in "media\", other resources (dlls, whatever) in "resources\" - AutoHotkey also has "lots of files" in its folder. Is that stupid? No of course not, it is practical.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Including media files directly in the exe?

25 Jan 2018, 15:04

Because this comes up pretty often, so for the sake of googling here is collected information for ways to embed images into scripts and executables.

As easy way on NTFS systems is the alternate data stream.
https://autohotkey.com/boards/viewtopic.php?t=27719
The Alternate Data Stream was basically designed for holding data and resources attached to a file.

Skan shows how to take a picture, convert it to hex, put the hex in an AHK script, then convert the hex to an image in memory.
http://www.autohotkey.com/forum/viewtopic.php?p=147052
This has the benefit that the script does not have to be compiled into an executeable to have an image all in one file. It is also highly portable because the information is converted to plain text. The script with image data can literal be cut and pasted as text.

just me created a nice utility to make the Skan code above easier to implement. They also convert to Base64 instead of hex to make the encoded image a shorter string of characters.
https://gist.github.com/AHK-just-me/5559658

fincs takes Skan's concept one step further by instead of converting the image to hex and putting in the script, they use FileInstall to have the image bundled into the exe package when compiled. But then cleverly they never actually have FileInstall execute in the flow of their script so the image is never extracted into a separate file as would be the norm. Instead they use some DllCall magic to access the image as a resource in the exe bundle. Once they get the image as a resource, basically getting the hex image, they use Skan's process to put the hex into memory and get a handle to it as an image.

Here is a snippet of fincs' script that gets LOGO.PNG into their exe without creating an additional file.

Code: Select all

AddPicture:
; Code based on http://www.autohotkey.com/forum/viewtopic.php?p=147052
Gui, Add, Text, x29 y16 w240 h78 +0xE hwndhPicCtrl

hRSrc := DllCall("FindResource", "ptr", 0, "str", "LOGO.PNG", "ptr", 10, "ptr")
sData := DllCall("SizeofResource", "ptr", 0, "ptr", hRSrc, "uint")
hRes  := DllCall("LoadResource", "ptr", 0, "ptr", hRSrc, "ptr")
pData := DllCall("LockResource", "ptr", hRes, "ptr")
hGlob := DllCall("GlobalAlloc", "uint", 2, "uint", sData, "ptr") ; 2=GMEM_MOVEABLE
pGlob := DllCall("GlobalLock", "ptr", hGlob, "ptr")
DllCall("msvcrt\memcpy", "ptr", pGlob, "ptr", pData, "uint", sData, "CDecl")
DllCall("GlobalUnlock", "ptr", hGlob)
DllCall("ole32\CreateStreamOnHGlobal", "ptr", hGlob, "int", 1, "ptr*", pStream)

hGdip := DllCall("LoadLibrary", "str", "gdiplus")
VarSetCapacity(si, 16, 0), NumPut(1, si, "UChar")
DllCall("gdiplus\GdiplusStartup", "ptr*", gdipToken, "ptr", &si, "ptr", 0)
DllCall("gdiplus\GdipCreateBitmapFromStream", "ptr", pStream, "ptr*", pBitmap)
DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "ptr", pBitmap, "ptr*", hBitmap, "uint", 0)
SendMessage, 0x172, 0, hBitmap,, ahk_id %hPicCtrl% ; 0x172=STM_SETIMAGE, 0=IMAGE_BITMAP
GuiControl, Move, %hPicCtrl%, w240 h78

DllCall("gdiplus\GdipDisposeImage", "ptr", pBitmap)
DllCall("gdiplus\GdiplusShutdown", "ptr", gdipToken)
DllCall("FreeLibrary", "ptr", hGdip)
ObjRelease(pStream)
return

Never:
FileInstall, logo.png, NEVER
return
Ahk2Exe Preview 2a basically does the same thing to provide an option to bundle stuff into the exe package without having to use the FileInstall-that-never-gets-executed trick.

It is not easy but if you want to be cool like Skan, just me, and fincs, then you can do it the way they do.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: Including media files directly in the exe?

26 Jan 2018, 00:35

Nice summary FanaticGuru ,i would also suggest another from Robert Eding i use often https://autohotkey.com/board/topic/6448 ... etc/page-1
It allows fontfiles and dll to be inserted in non_compiled ( or compiled ) code.

Inserting images as bitmaps not only is "neat" but there is no loading time when used and recent ahk supports the use of hbitmap as "filename" which makes it a superior option in imagesearch for example.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Including media files directly in the exe?

26 Jan 2018, 02:44

noname wrote:i would also suggest another from Robert Eding i use often https://autohotkey.com/board/topic/6448 ... etc/page-1
It allows fontfiles and dll to be inserted in non_compiled ( or compiled ) code.
Maybe I am missing something but this technique seems to require the file to be recreated which basically is the same as FileInstall. I guess it has the benefit of a non-compiled script can have the information to create an additional file.

It looks very similar to just me's script in that it converts stuff to Base64 and makes an #include file with a function. The difference being just me's script takes the Base64 string and puts it directly in to memory without creating a file.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: Including media files directly in the exe?

26 Jan 2018, 03:37

You can load a font without having to create a file using PrivateFontCollection.AddMemoryFont .But as you guessed the main purpose of having used Rseding's code is to include everything in one ahk file and no need to bother with a lot of files when you post it on this forum or keep them on PC.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, jaka1, mikeyww, RussF and 321 guests