How to embed images and scripts in exe file and how to use them?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
utvex
Posts: 10
Joined: 05 Feb 2023, 14:52

How to embed images and scripts in exe file and how to use them?

Post by utvex » 05 Feb 2023, 15:07

I want to create a exe file containing all program files using ahk2exe. I don't want to extract these files before using them so fileinstall is not an option. I know I can use compiler directives to add resources e.g. ;@Ahk2Exe-AddResource FileName but I don't know how to use them in the code. For example let's consider my program works like this theres main.ahk script which shows gui. On button click it runs second script named secondary.ahk that can't be combined with main.ahk and waits for it to complete. Depending on the result the appropriate image is loaded from exe file resources and is displayed in the gui and set as tray icon. Could somebody help me with that?

utvex
Posts: 10
Joined: 05 Feb 2023, 14:52

Re: How to embed images and scripts in exe file and how to use them?

Post by utvex » 06 Feb 2023, 12:36

This is simplified code

main.ahk

Code: Select all

gui add, button, gf x32 y118 w80 h20, run
gui show, w400 h256, window
return
f(){
	runwait %a_ahkpath% secondary.ahk
	if errorlevel
		gui add, picture, x144 y0 w256 h256, image/success.png
	else
		gui add, picture, x144 y0 w256 h256, image/fail.png
}
guiclose:
    exitapp
secondary.ahk

Code: Select all

random r,0,1
exitapp r
I want to embed secondary.ahk and success.png and fail.png in main.exe using ahk2exe ;@Ahk2Exe-AddResource directive. I can't save files to disk so extracting resources using FileInstall is not possible.
I don't know how to use resources added with compiler directive. Can someone tell my how to use images in this scenario? I also can't use a_ahkpath variable so I need a way to run embedded secondary.ahk script.
Success and fail images in attachments.
Attachments
success.png
success.png (14.63 KiB) Viewed 817 times
fail.png
fail.png (14.65 KiB) Viewed 817 times

utvex
Posts: 10
Joined: 05 Feb 2023, 14:52

Re: How to embed images and scripts in exe file and how to use them?

Post by utvex » 06 Feb 2023, 13:04

I made a mistake and success and fail images switched places but you get the idea. I can't see post edit option.

utvex
Posts: 10
Joined: 05 Feb 2023, 14:52

Re: How to embed images and scripts in exe file and how to use them?

Post by utvex » 08 Feb 2023, 08:14

@TAC109 You are the creator of ahk2exe so I think you are the only one capable of helping me with this problem. Since I wait quite long and nobody is willing to help I took the liberty of addressing you directly and I ask the moderator to allow this post despite it's apparently against the rules.


teadrinker
Posts: 4295
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to embed images and scripts in exe file and how to use them?

Post by teadrinker » 08 Feb 2023, 13:29

Try this:

Code: Select all

;@Ahk2Exe-Base %A_AhkPath%
;@Ahk2Exe-AddResource secondary.ahk, SECONDARY.AHK
;@Ahk2Exe-AddResource image/fail.png
;@Ahk2Exe-AddResource image/success.png

SetWorkingDir, % A_ScriptDir

gui add, button, gf x32 y118 w80 h20, run
gui add, picture, x144 y0 w256 h256
gui show, w400 h256, window
return

f() {
    if A_IsCompiled {
        runwait %A_AhkPath% /script SECONDARY.AHK
        GuiControl,, Static1, % "HBITMAP:" . HBitmapFromResource(ErrorLevel ? "success.png" : "fail.png")
    }
    else {
        runwait secondary.ahk
        GuiControl,, Static1, % "image/" . (ErrorLevel ? "success.png" : "fail.png")
    }
}
guiclose:
exitapp

HBitmapFromResource(resName) {
    hMod := DllCall("GetModuleHandle", "Ptr", 0, "Ptr")
    hRes := DllCall("FindResource", "Ptr", hMod, "Str", resName, "UInt", RT_RCDATA := 10, "Ptr")
    resSize := DllCall("SizeofResource", "Ptr", hMod, "Ptr", hRes)
    hResData := DllCall("LoadResource", "Ptr", hMod, "Ptr", hRes, "Ptr")
    pBuff := DllCall("LockResource", "Ptr", hResData, "Ptr")
    pStream := DllCall("Shlwapi\SHCreateMemStream", "Ptr", pBuff, "UInt", resSize, "Ptr")

    Gdip := new GDIplus
    pBitmap := Gdip.CreateBitmapFromStream(pStream)
    hBitmap := Gdip.CreateHBITMAPFromBitmap(pBitmap)
    Gdip.DisposeImage(pBitmap)
    ObjRelease(pStream)
    Return hBitmap
}

class GDIplus {
    __New() {
        if !DllCall("GetModuleHandle", "Str", "gdiplus", "Ptr")
            DllCall("LoadLibrary", "Str", "gdiplus")
        VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
        DllCall("gdiplus\GdiplusStartup", "UPtrP", pToken, "Ptr", &si, "Ptr", 0)
        this.token := pToken
    }
    __Delete() {
        DllCall("gdiplus\GdiplusShutdown", "Ptr", this.token)
        if hModule := DllCall("GetModuleHandle", "Str", "gdiplus", "Ptr")
            DllCall("FreeLibrary", "Ptr", hModule)
    }
    CreateBitmapFromStream(pStream) {
        DllCall("gdiplus\GdipCreateBitmapFromStream", "Ptr", pStream, "PtrP", pBitmap)
        Return pBitmap
    }
    CreateHBITMAPFromBitmap(pBitmap, background := 0xffffffff) {
        DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "Ptr", pBitmap, "PtrP", hbm, "UInt", background)
        return hbm
    }
    DisposeImage(pBitmap) {
        return DllCall("gdiplus\GdipDisposeImage", "Ptr", pBitmap)
    }
}

Post Reply

Return to “Ask for Help (v1)”