Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Include virtually any file in a script (exe/zip/dll/etc....)


  • Please log in to reply
49 replies to this topic
Rseding91
  • Members
  • 703 posts
  • Last active: Apr 02 2016 05:05 AM
  • Joined: 07 Jun 2010

I currently use a temporary workaround. The extract functions empty the static variables once they are complete. This can be done as I use the functions only once in my script.

 

The functions already automatically do that the first time you call them. Further if you want to dump the binary version of the file it stores after it compiles it from the static variables you can call the function like this: Extract_Name(*Path*, 1) ;the 1 saying erase the data once you write it.



Rijul Ahuja
  • Members
  • 763 posts
  • Last active: Nov 25 2013 10:16 AM
  • Joined: 14 Mar 2012

The functions already automatically do that the first time you call them. Further if you want to dump the binary version of the file it stores after it compiles it from the static variables you can call the function like this: Extract_Name(*Path*, 1) ;the 1 saying erase the data once you write it.

 

Thank you! Can I include this in a program that I wish to distribute? I have already added this line to the top of my code.

;Thanks to Rseding91's function to "Include virtually any file in a script"
;                 www.autohotkey.com/board/topic/64481-include-virtually-any-file-in-a-script-exezipdlletc/

Abandon the forum. The community has decided in a democratic vote to leave this website because of inactive and perverse administration.

Very few of the contributing members remain here.


Rseding91
  • Members
  • 703 posts
  • Last active: Apr 02 2016 05:05 AM
  • Joined: 07 Jun 2010

 

Thank you! Can I include this in a program that I wish to distribute? I have already added this line to the top of my code.

;Thanks to Rseding91's function to "Include virtually any file in a script"
;                 www.autohotkey.com/board/topic/64481-include-virtually-any-file-in-a-script-exezipdlletc/

 

Yeah that's fine.



Cei
  • Members
  • 23 posts
  • Last active: Oct 30 2014 03:37 PM
  • Joined: 26 Apr 2013

Hey Rseding91, just coming to say thank you :)

I made a GUI out of your script in order to accelerate the workflow. Hope you don't mind.

33LKd.JPG



CodeEater
  • Members
  • 83 posts
  • Last active: Jun 06 2013 03:42 PM
  • Joined: 17 Jun 2012

Hello Rseding91,

Could you add a function that return the extract data to a variable but the file on the disk.

Here is the reson. :)



servo386
  • Members
  • 2 posts
  • Last active: Mar 07 2015 08:09 PM
  • Joined: 13 Aug 2013

I hate to ressurect this old thread but this seems like a great tool that i'd like to deploy but am having trouble. I guess its with the syntax.

 

 

I copy pasted the entire output of the script (long wall of gibberish which is the hash or whatever)

 

Then after i put this:

 

Extract_acdcapp(%A_WorkingDir%/acdc.exe)

 

where acdcapp is the name of the function and acdc is the name of the exe.

 

It gives me:  Duplicate function definition.

 

and exits

 

What is the proper syntax for calling the function to extract the file?

 



Rijul Ahuja
  • Members
  • 763 posts
  • Last active: Nov 25 2013 10:16 AM
  • Joined: 14 Mar 2012
Extract_acdcapp( A_WorkingDir . (SubStr(A_WorkingDir)="\") ? "acdc.exe" : "\acdc.exe")

Read on A_WorkingDir, and expressions. Parameters passed to a function are expressions.


Abandon the forum. The community has decided in a democratic vote to leave this website because of inactive and perverse administration.

Very few of the contributing members remain here.


servo386
  • Members
  • 2 posts
  • Last active: Mar 07 2015 08:09 PM
  • Joined: 13 Aug 2013
Extract_acdcapp( A_WorkingDir . (SubStr(A_WorkingDir)="\") ? "acdc.exe" : "\acdc.exe")

Read on A_WorkingDir, and expressions. Parameters passed to a function are expressions.

 

 

I appreciate the response but I don't quite understand. I guess my fundamental problem is lack of knowledge on how to work with functions.
 
I put your code into my script and got "too few parameters" error.
 
I'll read up on your suggestions.


Rijul Ahuja
  • Members
  • 763 posts
  • Last active: Nov 25 2013 10:16 AM
  • Joined: 14 Mar 2012

Apologies, my mind was not working.

Extract_acdcapp( A_WorkingDir . (SubStr(A_WorkingDir,1,0)="\") ? "acdc.exe" : "\acdc.exe")

If you would like, I can try and explain how this works, PM me.


Abandon the forum. The community has decided in a democratic vote to leave this website because of inactive and perverse administration.

Very few of the contributing members remain here.


kArThIcK
  • Members
  • 142 posts
  • Last active: Aug 31 2017 03:40 AM
  • Joined: 13 May 2013

Wow, thanks for the script i was just looking for a way to embed files into my scriptsgrin.png


This Is An Awesome Compilation Of Help Materials http://www.autohotkey.net/~faqbot/
;) :) B)

agaxent
  • Members
  • 23 posts
  • Last active: Aug 08 2019 07:14 PM
  • Joined: 07 Jan 2014

this is what i been looking for to make a single exe but , because im just newbie in ahk i dont get it how exactly the code to extract the file that have already been pack?

 

for example i pack the main.png to main.ahk my problem is how to unpack using the code extract?? please someone can enlighten  me  really appreciate it .. and sorry for my bad english.



Skrell
  • Members
  • 384 posts
  • Last active: Jul 07 2016 05:03 PM
  • Joined: 23 Aug 2011

Yes please this definitely needs some documentation and examples! 



dustind900
  • Members
  • 43 posts
  • Last active: Nov 03 2017 01:11 PM
  • Joined: 09 Dec 2012

I'll try to help...

I'm going to assume that you put all files in the same folder. Recommended until you get things figured out. 

 

First the file to embed - HelloWorld.ahk

MsgBox Hello World!
ExitApp

.

.

Second we run Rseding91's script

  1. Select HelloWorld.ahk
  2. In the InputBox enter "HelloWorld_Embedded" ( without quotes )
  3. Select YES to save code as HelloWorld_Embedded.ahk in scripts current directory
  4. Select NO when it asks if you want to copy code to clipboard ( you can figure that out later )

 

HelloWorld_Embedded.ahk

Extract_HelloWorld_Embedded_Get(What)
{
	Static Size = 31, Name = "HelloWorld.ahk", Extension = "ahk", Directory = "C:\Users\US\Desktop"
	;This function returns the size(in bytes), name, filename, extension or directory of the file stored depending on what you ask for.
	If What = Size
		Return Size
	Else If What = Name
		Return Name
	Else If What = Extension
		Return Extension
	Else If What = Directory
		Return Directory
}

Extract_HelloWorld_Embedded(Filename,DumpData = 0)
{
	;This function "extracts" the file to the location+name you pass to it.
	Static HasData = 1, Base64Decode, Out_Data, Hex_Mcodex86 = "558bec518365fc00568b75088a1684d20f86ac000000578b7d0c5333db33c084d2764d32c984c975318aca80e92b4680f94f770c0fb6ca8b55108a4c11d5eb02b1240fb6d180ea3d80f9240f94c1fec923ca8a1684d277cd84c9760943fec9884c0508eb05c6440508004083f8047caf83fb027c4b8a45098a4d08c0e102c0e8040ac18a4d0a88074783fb027e108a55098ac1c0e802c0e2040ac288074783fb037e09c0e1060a4d0b880f478b45fc8a1684d28d4418ff8945fc0f875bffffff5b5f8b45fc5ec9c3"
	Static Hex_Mcodex64 = "48895C241048897C2418440FB60933DB488BFA4C8BD14584C90F84BC000000904533DB33C94584C9744F32C00F1F400084C0752C418D41D549FFC23C4F7712410FB6C1420FB64400D53C24740404C3EB0232C0450FB60A4584C975D484C0740B41FFC3FEC888440C08EB05C6440C080048FFC14883F9047CAC4183FB027C5C0FB65424090FB644240848FFC7C0E0020FB6CAC0E9040AC8884FFF0FB64C240A4183FB027E11C0E2040FB6C1C0E8020AC248FFC78847FF4183FB037E0DC0E1060A4C240B48FFC7884FFF450FB60A428D5C1BFF4584C90F8545FFFFFF488B7C24188BC3488B5C2410C3"
	Static CD = "|$$$}rstuvwxyz{$$$$$$$>[email protected]$$$$$$XYZ[\]^_``abcdefghijklmnopq"
	Static 1="77u/TXNnQm94IEhlbGxvIFdvcmxkIQ0KRXhpdEFwcA=="
	
	If (!HasData)
		Return -1
	
	If (CD){
		VarSetCapacity(TD,43 * (A_IsUnicode ? 2 : 1))
		
		Loop,% 1
			TD .= %A_Index%, %A_Index% := ""
		
		Hex_Mcode := A_PtrSize = 8 ? Hex_Mcodex64 : Hex_Mcodex86
		VarSetCapacity(Base64Decode,StrLen(Hex_Mcode)//2)
		Loop % StrLen(Hex_Mcode)//2
			NumPut("0x" . SubStr(Hex_Mcode,2*A_Index-1,2),Base64Decode,A_Index-1,"Char")
		DllCall("VirtualProtect",A_IsUnicode ? "Ptr" : "UInt",&Base64Decode,A_IsUnicode ? "Ptr" : "UInt",VarSetCapacity(Base64Decode),"uint",0x40,"uint*",0)
		
		VarSetCapacity(Out_Data,31,0)
		, DllCall(&Base64Decode,A_IsUnicode ? "AStr" : "Str",TD,A_IsUnicode ? "Ptr" : "UInt",&Out_Data,A_IsUnicode ? "AStr" : "Str",CD,"CDECL UINT")
		, Base64Decode := ""
		, TD := ""
		, CD := ""
		, Hex_Mcodex86 := ""
		, Hex_Mcodex64 := ""
		, Hex_Mcode := ""
	}
	
	IfExist,%Filename%
		FileDelete,%Filename%
	
	h := DllCall("CreateFile","str",Filename,"Uint",0x40000000,"Uint",0,"UInt",0,"UInt",4,"Uint",0,"UInt",0)
	DllCall("WriteFile","UInt",h,A_IsUnicode ? "Ptr" : "UInt",&Out_Data,"UInt",31,"UInt",0,"UInt",0)
	DllCall("CloseHandle", "Uint", h)
	
	If (DumpData)
		VarSetCapacity(Out_Data,31,0)
		, VarSetCapacity(Out_Data,0)
		, HasData := 0
}

.

.

Third we need a script to extract the embedded code ( save as Extract.ahk in the same dir as HelloWorld_Embedded.ahk )

#Include HelloWorld_Embedded.ahk

SplashTextOn 200, 200, Extracting, Please Wait...
Extract_HelloWorld_Embedded( A_ScriptDir "\HelloWorld_Extracted.ahk" )
while !FileExist( A_ScriptDir "\HelloWorld_Extracted.ahk" )
    Sleep 100
Sleep 1000
ControlSetText Static1, Done!, Extracting
Sleep 1000
SplashTextOff
ExitApp

.

.

Run Extract.ahk and if all went well you should now have HelloWorld_Extracted.ahk in your script dir. 

 



Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

 

I'll try to help...

I'm going to assume that you put all files in the same folder. Recommended until you get things figured out. 

 

First the file to embed - HelloWorld.ahk

MsgBox Hello World!
ExitApp

.

.

Second we run Rseding91's script

  1. Select HelloWorld.ahk
  2. In the InputBox enter "HelloWorld_Embedded" ( without quotes )
  3. Select YES to save code as HelloWorld_Embedded.ahk in scripts current directory
  4. Select NO when it asks if you want to copy code to clipboard ( you can figure that out later )

 

HelloWorld_Embedded.ahk

Extract_HelloWorld_Embedded_Get(What)
{
	Static Size = 31, Name = "HelloWorld.ahk", Extension = "ahk", Directory = "C:\Users\US\Desktop"
	;This function returns the size(in bytes), name, filename, extension or directory of the file stored depending on what you ask for.
	If What = Size
		Return Size
	Else If What = Name
		Return Name
	Else If What = Extension
		Return Extension
	Else If What = Directory
		Return Directory
}

Extract_HelloWorld_Embedded(Filename,DumpData = 0)
{
	;This function "extracts" the file to the location+name you pass to it.
	Static HasData = 1, Base64Decode, Out_Data, Hex_Mcodex86 = "558bec518365fc00568b75088a1684d20f86ac000000578b7d0c5333db33c084d2764d32c984c975318aca80e92b4680f94f770c0fb6ca8b55108a4c11d5eb02b1240fb6d180ea3d80f9240f94c1fec923ca8a1684d277cd84c9760943fec9884c0508eb05c6440508004083f8047caf83fb027c4b8a45098a4d08c0e102c0e8040ac18a4d0a88074783fb027e108a55098ac1c0e802c0e2040ac288074783fb037e09c0e1060a4d0b880f478b45fc8a1684d28d4418ff8945fc0f875bffffff5b5f8b45fc5ec9c3"
	Static Hex_Mcodex64 = "48895C241048897C2418440FB60933DB488BFA4C8BD14584C90F84BC000000904533DB33C94584C9744F32C00F1F400084C0752C418D41D549FFC23C4F7712410FB6C1420FB64400D53C24740404C3EB0232C0450FB60A4584C975D484C0740B41FFC3FEC888440C08EB05C6440C080048FFC14883F9047CAC4183FB027C5C0FB65424090FB644240848FFC7C0E0020FB6CAC0E9040AC8884FFF0FB64C240A4183FB027E11C0E2040FB6C1C0E8020AC248FFC78847FF4183FB037E0DC0E1060A4C240B48FFC7884FFF450FB60A428D5C1BFF4584C90F8545FFFFFF488B7C24188BC3488B5C2410C3"
	Static CD = "|$$$}rstuvwxyz{$$$$$$$>[email protected]$$$$$$XYZ[\]^_``abcdefghijklmnopq"
	Static 1="77u/TXNnQm94IEhlbGxvIFdvcmxkIQ0KRXhpdEFwcA=="
	
	If (!HasData)
		Return -1
	
	If (CD){
		VarSetCapacity(TD,43 * (A_IsUnicode ? 2 : 1))
		
		Loop,% 1
			TD .= %A_Index%, %A_Index% := ""
		
		Hex_Mcode := A_PtrSize = 8 ? Hex_Mcodex64 : Hex_Mcodex86
		VarSetCapacity(Base64Decode,StrLen(Hex_Mcode)//2)
		Loop % StrLen(Hex_Mcode)//2
			NumPut("0x" . SubStr(Hex_Mcode,2*A_Index-1,2),Base64Decode,A_Index-1,"Char")
		DllCall("VirtualProtect",A_IsUnicode ? "Ptr" : "UInt",&Base64Decode,A_IsUnicode ? "Ptr" : "UInt",VarSetCapacity(Base64Decode),"uint",0x40,"uint*",0)
		
		VarSetCapacity(Out_Data,31,0)
		, DllCall(&Base64Decode,A_IsUnicode ? "AStr" : "Str",TD,A_IsUnicode ? "Ptr" : "UInt",&Out_Data,A_IsUnicode ? "AStr" : "Str",CD,"CDECL UINT")
		, Base64Decode := ""
		, TD := ""
		, CD := ""
		, Hex_Mcodex86 := ""
		, Hex_Mcodex64 := ""
		, Hex_Mcode := ""
	}
	
	IfExist,%Filename%
		FileDelete,%Filename%
	
	h := DllCall("CreateFile","str",Filename,"Uint",0x40000000,"Uint",0,"UInt",0,"UInt",4,"Uint",0,"UInt",0)
	DllCall("WriteFile","UInt",h,A_IsUnicode ? "Ptr" : "UInt",&Out_Data,"UInt",31,"UInt",0,"UInt",0)
	DllCall("CloseHandle", "Uint", h)
	
	If (DumpData)
		VarSetCapacity(Out_Data,31,0)
		, VarSetCapacity(Out_Data,0)
		, HasData := 0
}

.

.

Third we need a script to extract the embedded code ( save as Extract.ahk in the same dir as HelloWorld_Embedded.ahk )

#Include HelloWorld_Embedded.ahk

SplashTextOn 200, 200, Extracting, Please Wait...
Extract_HelloWorld_Embedded( A_ScriptDir "\HelloWorld_Extracted.ahk" )
while !FileExist( A_ScriptDir "\HelloWorld_Extracted.ahk" )
    Sleep 100
Sleep 1000
ControlSetText Static1, Done!, Extracting
Sleep 1000
SplashTextOff
ExitApp

.

.

Run Extract.ahk and if all went well you should now have HelloWorld_Extracted.ahk in your script dir. 

 

i would have been totally lost without this example. i tested with a Word file (.doc) and it performed FLAWLESSLY!



AHK_Lee
  • Members
  • 31 posts
  • Last active: Aug 17 2014 02:16 PM
  • Joined: 13 Apr 2010

I have always wanted a way to pack files into a script for portability but I have never been able to find one.

That came to this:

Store virtually any file in a .ahk script (or compiled .exe)
Download: Here
Current version: 2.5
Compatible with: AHK Basic, AHK L ANSI, AHK L Unicode (X86/X64)


File types i've tested and know work: .zip, .exe, .txt, .ahk, ...anything?

The .ahk size of the file is rougly 1.33x the normal filesize(s). The compiled .exe (without compression) is rougly 1-1.05x the included filesize(s).

If you find any bugs or have anything to suggest please feel free to post.




How to use!:
1. Download and save the .ahk to a directory of your choosing.
2. Run the script
3. Select a file you want to include with your script(s)
4. Enter a name for the function you call to recreate the file (valid function names only)
5. Wait for it to do its stuff
6. Answer the questions on how you want to save the script
7. Simply include the code via #include or pasting it into your script
8. Call the function with Extract_*name* and the path/name of the file you want it to save as and it will recreate the file

 

Hi Rseding91,

 

First of all thank you very much for this fine piece of coding, just awesome thanks !!!

 

I wonder if you could help me I am stuck on a problem.

 

When I extract the first time the file compared to the orginal its exactly the same.

But when repeating or using the same function again the new file created is corrupt.

 

 

example script;

#persistent

~f5::
IfNotExist, dummyfile_org.dat
Extract_dummyfile("dummyfile_org.dat")

Extract_dummyfile("dummyfile_corrupt.dat")
return

Extract_dummyfile(_Filename, _DumpData = 0)
{
	;This function "extracts" the file to the location+name you pass to it.
	Static HasData = 1, Out_Data, Ptr
	Static 1 = "AAAAAAAAAAAAAPA/AAAAAAEAAAABAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJAAAACQAAAAkAAAAJAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAABAAAAAAAAAAAAgD8CAAAAAQAAAAEAAAABAAAAAQAAABdERV9NUBpJX0gXFkkFT1MgVAoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABAAAAAQAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAADIQgAAyEIAAMhCAADIQgVUYQAAAAAAAAAAAQAAAAAAAAAAAIA/AgAAAAEAAAABAAAAAQAAAAEAAAAZSU8USR1IAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAIA/AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMhCAADIQgAAyEIAAMhC"
	
	If (!HasData)
		Return -1
	
	If (!Out_Data){
		Ptr := A_IsUnicode ? "Ptr" : "UInt"
		, VarSetCapacity(TD, 1768 * (A_IsUnicode ? 2 : 1))
		
		Loop, 1
			TD .= %A_Index%, %A_Index% := ""
		
		VarSetCapacity(Out_Data, Bytes := 1290, 0)
		, DllCall("Crypt32.dll\CryptStringToBinary" (A_IsUnicode ? "W" : "A"), Ptr, &TD, "UInt", 0, "UInt", 1, Ptr, &Out_Data, A_IsUnicode ? "UIntP" : "UInt*", Bytes, "Int", 0, "Int", 0, "CDECL Int")
		, TD := ""
	}
	
	IfExist, %_Filename%
		FileDelete, %_Filename%
	
	h := DllCall("CreateFile", Ptr, &_Filename, "Uint", 0x40000000, "Uint", 0, "UInt", 0, "UInt", 4, "Uint", 0, "UInt", 0)
	, DllCall("WriteFile", Ptr, h, Ptr, &Out_Data, "UInt", 1290, "UInt", 0, "UInt", 0)
	, DllCall("CloseHandle", Ptr, h)
	
	If (_DumpData)
		VarSetCapacity(Out_Data, 1290, 0)
		, VarSetCapacity(Out_Data, 0)
		, HasData := 0
}

So in other words if you put the above into a .ahk file and run it for the firsttime the second file is corrupt.

If the script is reloaded only the first time the function is used the first extraction is only okay :(

 

Note: deleting both testfiles while test script is already running and function is already used also results in the first file to be corrupt so compared to the second corrupt they are both equal and corrupt. also when the org file(extracted) exist and script skips to the corrupt file extraction its good only for the first time the function is used.

 

I.o.w. using the function more then once without reloading the script results in a corrupt extraction.

 

Using: Version v1.1.14.01
 

What can I do without reloading every time I use the function?

 

Greetz,

Lee


[quote name="Confucius ]And remember, no matter where you go, there you are.[/quote]