Porting SKAN's "Functions for Resource-Only DLL" to v2

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Baconfry
Posts: 14
Joined: 07 Jan 2022, 04:07

Porting SKAN's "Functions for Resource-Only DLL" to v2

Post by Baconfry » 30 Jan 2023, 03:35

I'm using SKAN's functions to load images into GUIs for speed and to avoid having many small files. I tried to port them by just fixing the syntaxes of the legacy commands, and not surprisingly, it mostly failed. Flipeador ported DllCreateEmpty() to v2, but it doesn't seem to be working now. It shows up an error and the v1 DllPackFiles() won't work on the DLL that it creates.

I was able to convert DllPackFiles(), but I inserted a try statement before the DllCall to block the error :oops:

Code: Select all

; AHK v2 port
DllPackFiles(Folder, DLL, Section := "Files")
{
    Section := Format("{:U}", Section)
    ; DLL file must be created by DllCreateEmpty() manually, since the function is not ported
    ;DLL := FileExist(DLL) ? DLL : DllCreateEmpty(DLL)
    hUPD := DllCall("BeginUpdateResource", "Str",DLL, "Int",0, "Ptr")

    Loop Files, Folder "\*.*"
    {
        Key := Format("{:U}", A_LoopFileName)
        BIN := FileRead(A_LoopFileFullPath, "RAW")
        
        try DllCall( "UpdateResource", "Ptr",hUPD, "Str",Section, "Str",Key
                , "Int",0, "Ptr",&BIN, "UInt",A_LoopFileSize )
    }
    DllCall( "EndUpdateResource", "Ptr",hUPD, "Int",0 )
}


Baconfry
Posts: 14
Joined: 07 Jan 2022, 04:07

Re: Porting SKAN's "Functions for Resource-Only DLL" to v2

Post by Baconfry » 30 Jan 2023, 05:49

Thank you very much, teadrinker! That did the trick on this function.

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

Re: Porting SKAN's "Functions for Resource-Only DLL" to v2

Post by teadrinker » 30 Jan 2023, 06:02

In v2 & character has a different meaning: ByRef

Baconfry
Posts: 14
Joined: 07 Jan 2022, 04:07

Re: Porting SKAN's "Functions for Resource-Only DLL" to v2

Post by Baconfry » 30 Jan 2023, 09:07

Thanks for the tip. Does it mean that this

Code: Select all

DllCall ... "Ptr", &Var...
is essentially equivalent to this in v2?

Code: Select all

DllCall ... "Ptr", Var...
I gave the docs a read, but I'm really not well versed in DllCalls, so this may be a silly question. On another note, I managed to port this function in v2 as well.

Code: Select all

DllCreateEmpty(NewFile)
{
    UNIXTIME := A_NowUTC
    DLLBIN := Buffer(1024, 0)
    DLLHEX := "0X5A4DY3CXC0YC0X4550YC4X1014CYD4X210E00E0YD8XA07010BYE0X200YECX1000YF0X1000YF4X10"
    . "000YF8X1000YFCX200Y100X4Y108X4Y110X2000Y114X200Y11CX4000003Y120X40000Y124X1000Y128X100000Y12C"
    . "X1000Y134X10Y148X1000Y14CX10Y1B8X7273722EY1BCX63Y1C0X10Y1C4X1000Y1C8X200Y1CCX200Y1DCX40000040"

    UNIXTIME := DateAdd(UNIXTIME, -1970, "seconds")
    Numput("UInt", UNIXTIME, DLLBIN, 200)

    Loop Parse DLLHEX, "XY" {
        if Mod(A_Index, 2)
            Off := "0x" A_LoopField
        else
            NumPut("Int", "0x" A_LoopField, DLLBIN, Off)
    }

    File := FileOpen( NewFile, "w"),  File.RawWrite( DLLBIN, 1024 ),  File.Close()
    
    return NewFile
}
It shows no error and paired with the ported DllPackFiles(), it could pack resources into Dlls. Only thing missing now is reading the resources back :)

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

Re: Porting SKAN's "Functions for Resource-Only DLL" to v2

Post by teadrinker » 30 Jan 2023, 12:00

Baconfry wrote: is essentially equivalent to this in v2?
What the FileRead() function with the RAW option returns is not a simple variable, but the Buffer object, that is some kind of a wrapper. When you need to set the 'Ptr', ... parameter in the DllCall, you can just use the Buffer variable name. In DllCall such buffers are used most often, when the pointers to data blocks are requred.

Baconfry
Posts: 14
Joined: 07 Jan 2022, 04:07

Re: Porting SKAN's "Functions for Resource-Only DLL" to v2

Post by Baconfry » 30 Jan 2023, 15:20

That's a very nice thing to note. Thanks! :)

Post Reply

Return to “Ask for Help (v2)”