https://autohotkey.com/board/topic/64481-include-virtually-any-file-in-a-script-exezipdlletc/page-1
I combined Bin2Hex code by Laszlo and "include-a-bitmap-in-your-uncompiled-script" by Veovis to make it easier to add images/files to your uncompiled script. Tested on image, text and exe files. It takes a very long time to write the file back to disk if the file is large.
Steps to use...
(1) Run this script
(2) Script will prompt you to choose a file
(3) Script will create FinalCode_NameOfFile_ExtensionOfFile.ahk
(4) Edit or Open FinalCode_NameOfFile_ExtensionOfFile.ahk
(5) Copy & Paste Sections 1,2 and 3 to your script
(6) Do this for as many images/files that you want to include in your script. Only Copy and Paste Section 2 one time per script.
(7) FinalCode_NameOfFile_ExtensionOfFile.ahk will tell you where to copy each section
(8) FinalCode_NameOfFile_ExtensionOfFile.ahk can be run as is to see it work before copying to your script.
Credits go to the following people.
Laszlo - "Bin2Hex functions" found here https://autohotkey.com/board/topic/4352-binary-file-io-with-binary-buffers/
Veovis - "include-a-bitmap-in-your-uncompiled-script" found here https://autohotkey.com/board/topic/9974-include-a-bitmap-in-your-uncompiled-script/
Rohwedder - "Divide String by number of characters" found here https://www.autohotkey.com/boards/viewtopic.php?f=76&t=64905&p=278527#p278527
Code: Select all
;I combined Bin2Hex code by Laszlo and "include-a-bitmap-in-your-uncompiled-script" by Veovis to make it easier to add images/files to your uncompiled script.
;Steps to use...
;(1) Run this script
;(2) Script will prompt you to choose a file
;(3) Script will create FinalCode_NameOfFile_ExtensionOfFile.ahk
;(4) Edit or Open FinalCode_NameOfFile_ExtensionOfFile.ahk
;(5) Copy & Paste Sections 1,2 and 3 to your script
;(6) Do this for as many images/files that you want to include in your script. Only Copy and Paste Section 2 one time per script.
;(7) FinalCode_NameOfFile_ExtensionOfFile.ahk will tell you where to copy each section
;(8) FinalCode_NameOfFile_ExtensionOfFile.ahk can be run as is to see it work before copying to your script.
;Credits go to the following people.
;Laszlo - "Bin2Hex functions" found here https://autohotkey.com/board/topic/4352-binary-file-io-with-binary-buffers/
;Veovis - "include-a-bitmap-in-your-uncompiled-script" found here https://autohotkey.com/board/topic/9974-include-a-bitmap-in-your-uncompiled-script/
;Rohwedder - "Divide String by number of characters" found here https://www.autohotkey.com/boards/viewtopic.php?f=76&t=64905&p=278527#p278527
SplitStringVarByThisNumber = 16300 ;After converting the file to hexcode this will be the maximum number of characters in each continuation/join variable.
;Choose file to covert to Hex
FileSelectFile,File,,,Choose a file
if File =
ExitApp
UniqueNumber = %a_now% ;this is needed to make each varible in the continuation/join section different each time this script is ran.
SplitPath,File,ScriptCreatedFile,OutDir,OutExtension,OutNameNoExt ;ScriptCreatedFile will be the same name as the file chosen by FileSelectFile without the path
FinalCode = FinalCode_%OutNameNoExt%_%OutExtension%.ahk ;This will be the name of the Output ahk file
IfExist,%FinalCode%
MsgBox,4,File already exists!,File: ( %FinalCode% )`n`nDo you want to overwrite it?`n`nSelect Yes to overwrite, select No to Exit script
IfMsgBox, no
exitapp
IfMsgBox, yes
FileDelete,%FinalCode%
BinRead(file,data) ;read binary data
Bin2Hex(h,data,res) ;convert binary to Hex
;Split Hex code in sections no larger then 16366 (SplitStringVarByThisNumber), the name of these sections will be HexData1, HexData2, etc...
While, HexDataX%A_Index% := SubStr(h,1+(A_Index-1)*SplitStringVarByThisNumber, SplitStringVarByThisNumber)
Sections++ ;Need to know how many sections of HexData is created
;create the continuation/join sections and label them var1, var2,etc....These variables will be written to FileCode_???.ahk
loop %Sections%
{
HexData := HexDataX%a_index%
Var%A_Index% =
(
%UniqueNumber%_%a_index% =
`( join
%HexData%
`)
)
len := StrLen(Var%a_index%)
if Len > 16394
{
MsgBox,4112,Error, One of the continuation sections is too large.`n`nPlease reduce ( SplitStringVarByThisNumber )`n`nThe script will now exit.
ExitApp
}
}
;The rest of this Autoexecute section creates FileCode_???.ahk
if Sections = 1
{
FileAppend,;This script can be ran as is or add the 3 sections to your script. For multiple files Section 2 can only be added to your script 1 time..`n`n;[SECTION 1]-----------------------------------------Add Lines 4-7 to your autoexecute section------------------------------`n,%FinalCode%
FileAppend,
(
Gosub, CreateHexDataContinuationSections
HexDataX := ( HexDataX %UniqueNumber%_1 )
WriteFile("%ScriptCreatedFile%",HexDataX)
HexDataX1 =
;---------------------------------------------------------------------------------------------------------------------------
Return ;only add this return if you want your Autoexec section to end here
),%FinalCode%
}
else
{
FileAppend,
(
;This script can be ran as is or add the 3 sections to your script. Section 2 only has to be added to your script 1 time.`n`n;[SECTION 1]-----------------------------------------Add Lines 4-10 to your AutoExecute section------------------------------
Gosub, CreateHexDataContinuationSections
loop %Sections%
{
HexDataX := ( HexDataX %UniqueNumber%_`%a_index`% )
HexDataX`%a_index`% =
}
WriteFile("%ScriptCreatedFile%",HexDataX)
;---------------------------------------------------------------------------------------------------------------------------
Return ;only add this return if you want your AutoExecute section to end here
),%FinalCode%
}
If Sections = 1
Lines = 12-29
else
Lines = 15-32
FileAppend,
(
;[SECTION 2]---------------------------Put the WriteFile function Lines %Lines% anywhere in your script-----------------------
WriteFile(file,data)
{
Handle := DllCall("CreateFile","str",file,"Uint",0x40000000
,"Uint",0,"UInt",0,"UInt",4,"Uint",0,"UInt",0)
Loop
{
if strlen(data) = 0
break
StringLeft, Hex, data, 2
StringTrimLeft, data, data, 2
Hex = 0x`%Hex`%
DllCall("WriteFile","UInt", Handle,"UChar *", Hex
,"UInt",1,"UInt *",UnusedVariable,"UInt",0)
}
DllCall("CloseHandle", "Uint", Handle)
return
}
),%FinalCode%
FileAppend,;[SECTION 3]---Add label CreateHexDataContinuationSections and all continuation/join sections (everything below including the Return) to anywhere in your script (other than the AutoExecute section)---,%FinalCode%
Loop, %sections%
{
if A_index = 1
FileAppend,`nCreateHexDataContinuationSections:`n,%FinalCode%
AppendVar := Var%a_index%
FileAppend,%AppendVar%`n,%FinalCode%
}
FileAppend,Return,%FinalCode%
MsgBox ( %FinalCode% ) has been created in this scripts folder....`n`n%a_scriptdir%
ExitApp
;------------------------------------------------------------------------------------------------------------------------------------------
;functions Bin2Hex and BinRead by Laszlo - https://autohotkey.com/board/topic/4352-binary-file-io-with-binary-buffers/
Bin2Hex(ByRef h, ByRef b, n=0) ; n bytes binary data -> stream of 2-digit hex
{ ; n = 0: all (SetCapacity can be larger than used!)
format = %A_FormatInteger% ; save original integer format
SetFormat Integer, Hex ; for converting bytes to hex
m := VarSetCapacity(b)
If (n < 1 or n > m)
n := m
Address := &b
h =
Loop %n%
{
x := *Address ; get byte in hex
StringTrimLeft x, x, 2 ; remove 0x
x = 0%x% ; pad left
StringRight x, x, 2 ; 2 hex digits
h = %h%%x%
Address++
}
SetFormat Integer, %format% ; restore original format
}
BinRead(file, ByRef data, n=0, offset=0)
{
h := DllCall("CreateFile","Str",file,"Uint",0x80000000,"Uint",3,"UInt",0,"UInt",3,"Uint",0,"UInt",0)
IfEqual h,-1, SetEnv, ErrorLevel, -1
IfNotEqual ErrorLevel,0,Return,0 ; couldn't open the file
m = 0 ; seek to offset
IfLess offset,0, SetEnv,m,2
r := DllCall("SetFilePointerEx","Uint",h,"Int64",offset,"UInt *",p,"Int",m)
IfEqual r,0, SetEnv, ErrorLevel, -3
IfNotEqual ErrorLevel,0, {
t = %ErrorLevel% ; save ErrorLevel to be returned
DllCall("CloseHandle", "Uint", h)
ErrorLevel = %t% ; return seek error
Return 0
}
m := DllCall("GetFileSize","UInt",h,"Int64 *",r)
If (n < 1 or n > m)
n := m
Granted := VarSetCapacity(data, n, 0)
IfLess Granted,%n%, {
ErrorLevel = Mem=%Granted%
Return 0
}
result := DllCall("ReadFile","UInt",h,"Str",data,"UInt",n,"UInt *",Read,"UInt",0)
if (!result or Read < n)
t = -3
IfNotEqual ErrorLevel,0, SetEnv,t,%ErrorLevel%
h := DllCall("CloseHandle", "Uint", h)
IfEqual h,-1, SetEnv, ErrorLevel, -2
IfNotEqual t,,SetEnv, ErrorLevel, %t%-%ErrorLevel%
Return Read
}