AHK On-the-fly Compiler

Get help with using AutoHotkey and its commands and hotkeys
JFrog

AHK On-the-fly Compiler

04 Mar 2018, 18:52

Hello,
I'm working on an AHK GUI/tool that I want to be able to share with coworkers who don't have AHK installed on their PCs. The idea is to allow them to create their own basic scripts/hotkeys through a simple GUI vs coding.
I've got some of the (very) basic framework that writes a .ahk file on the fly, but I need to be able to compile it to its standalone .exe since they won't have AHK installed.
While my coworkers could install AHK on their PCs, this tool would catch on much easier if there was no install necessary. So it is possible for a .exe script to compile another .ahk into .exe without AHK installed? If so, how?
Thanks!
User avatar
Masonjar13
Posts: 1289
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: AHK On-the-fly Compiler

04 Mar 2018, 19:09

Well, few things. First, AHK doesn't have to be "installed" at all, as long as the exe is present. You can simply pass the script as a parameter to AHK, or drag-and-drop. Alternatively, you could do something like my ScriptUp script. It uses AutoHotkey.dll (which uses AHK_H syntax, mostly the same but a few differences) to run scripts as separate threads under one process, which effectively allows you to run a script/any number of scripts from one compiled script.

Having the AHK exe present would be significantly easier. What I've done to make it portable is I use FileSelectFile so they may select the script, then it uses Run,% """" . pathToAHK . """ """ . pathToScript . """".
guest3456
Posts: 2280
Joined: 09 Oct 2013, 10:31

Re: AHK On-the-fly Compiler

04 Mar 2018, 19:16

JFrog wrote:So it is possible for a .exe script to compile another .ahk into .exe without AHK installed? If so, how?


i believe AHK_H compiled exe's can be used to compile other scripts. so you could add the Ahk2Exe script as part of your main script, and then use that executable as the bin file

JFrog
Posts: 5
Joined: 04 Mar 2018, 18:55

Re: AHK On-the-fly Compiler

04 Mar 2018, 20:53

@Masonjar, thanks for the quick reply!
So I'll just repeat to make sure I understand. If I provide the AutoHotKey.exe with my tool, I can create .ahk scripts and evoke them using the AutoHotKey.exe without my coworkers needing to do anything extra, is that correct?

If this is true, could I similarly provide the Ahk2Exe.exe file and compile scripts as well? (More of a curious/educational question, even if that works it still seems more practical to evoke ahk files using a provided AHK exe).

guest3456 wrote:i believe AHK_H compiled exe's can be used to compile other scripts. so you could add the Ahk2Exe script as part of your main script, and then use that executable as the bin file


Funny that you mention that, as that's exactly where my mind was at :D However, my programming experience isn't quite so advanced, I'm not sure how I would use it as a "bin file". Wouldn't I just provide the Ahk2Exe and use it similar to Masonjar's suggestion?:

Code: [Select all] [Download] GeSHi © Codebox Plus

Run,% """" . pathToAhk2Exe . """ """ . pathToScript . """"
guest3456
Posts: 2280
Joined: 09 Oct 2013, 10:31

Re: AHK On-the-fly Compiler

04 Mar 2018, 22:57

if you are happy shipping just your exe, then with AHK_H your compiled exe can also be used to run the external .ahk scripts without requiring you to install ahk. the exe's are in fact the interpreters also

https://hotkeyit.github.io/v2/docs/AHKH_Features.htm

Compiling AutoHotkey

Original AutoHotkey is only capable to be compiled with AutoHotkeySC.bin. In AutoHotkey_H any AutoHotkey binary (AutoHotkey.dll, AutoHotkey.exe, AutoHotkeySC.bin) can be compiled.
This allows keeping full functionality of AutoHotkey including executing other scripts. Compiled AutoHotkey.exe and AutoHotkey.dll can use /E switch to execute different script than compiled one.

User avatar
Masonjar13
Posts: 1289
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: AHK On-the-fly Compiler

04 Mar 2018, 23:19

Yes, you can basically just copy and paste the install folder and have everything work. However, if you do want it "all-in-one," using either the dll as I mentioned, or _H as guest3456 has mentioned. Note that also you could FileInstall all of these files into one script, then extract and use them as needed. Many ways to accomplish what you're going for, so it'll really be up to you as to which to choose.
JFrog
Posts: 5
Joined: 04 Mar 2018, 18:55

Re: AHK On-the-fly Compiler

05 Mar 2018, 17:49

@guest3456, thanks for your suggestions. I spent some time playing around with AHK_H, but so far I'm unsuccessful in getting a AHK_H compiled exe to execute a another script. When I try to drag/drop an ahk onto my exe, it just crashes. Any idea how this is supposed to work?

@Masonjar, if I use the dll as you mentioned, don't I still have to include the dll itself since it is now a dependency?

I'm sorry if these are silly questions, much of my coding experience can be considered fairly basic (I've never messed with dll's much, among other things). I would really love to be able to only provide a single exe and not depend on AutoHotKey.exe or Ahk2Exe.exe, if possible though it seems the FileInstall command may be the simplest thing for me to do :?
burque505
Posts: 343
Joined: 22 Jan 2017, 19:37

Re: AHK On-the-fly Compiler

05 Mar 2018, 18:09

@JFrog, maybe you'll have some luck with Enigma Virtual Box, it's free. Probably a teaser for their full-blown Enigma Protector.
Enigma Protector download page.
Regards,
burque505
User avatar
Masonjar13
Posts: 1289
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: AHK On-the-fly Compiler

05 Mar 2018, 18:16

No problem! With the dll method, yes you do have to include it. You can use my class here, to which you'll also need MemoryLibrary, Struct, and ReadResource. They way I have it setup, if you use FileInstall, you can pass A_IsCompiled to the threadMan object when you create it and it will read the dll from resource. Typically, you'll need a separate dll file for each script you want to run, but in this case, MemoryLibrary will load the dll into memory and use it from there instead, so one file is all you need. You can download the dll's from the _H package. Be sure to match the correct bit-length (32-bit for x86/32-bit compiled scripts, 64-bit for x64/64-bit scripts) or an error will occur and prevent it from running.

That might seem like a lot, but this example should get you started.

Code: [Select all] [Download] GeSHi © Codebox Plus

#include <threadMan> ; be sure to include this file so you can access the class
pathToDll:=AutoHotkey.dll ; put the path to the dll here

thread1:=new threadMan(pathToDll,a_isCompiled) ; will work if script is compiled, and uncompiled if the dll is in the same folder as the script
thread1.newFromText("msgbox Hello from first thread!")
msgbox Hello from main thread!
exitApp

fileInstall,AutoHotkey.dll,0 ; we want this inaccessable so it doesn't actually extract the dll.

As I was testing this though.. it seems that readResource() isn't working.. not sure why. Compression doesn't seem to make a difference, either. So, this will work if the dll is accessible at the path, but not otherwise. I'll look into that.
guest3456
Posts: 2280
Joined: 09 Oct 2013, 10:31

Re: AHK On-the-fly Compiler

06 Mar 2018, 08:32

JFrog wrote:@guest3456, thanks for your suggestions. I spent some time playing around with AHK_H, but so far I'm unsuccessful in getting a AHK_H compiled exe to execute a another script. When I try to drag/drop an ahk onto my exe, it just crashes. Any idea how this is supposed to work?


you don't drag drop it, you pass the /E switch at the command line or from a Run command, like mentioned in what i quoted

i just tested it, and both worked for me

Code: [Select all] [Download] GeSHi © Codebox Plus

Run, compiled.exe /E script.ahk

JFrog
Posts: 5
Joined: 04 Mar 2018, 18:55

Re: AHK On-the-fly Compiler

06 Mar 2018, 17:15

@guest3456, does the compiled.exe need to have any special code to handle the /E switch? I've got script.ahk with only "msgbox hello" to test this and see if it runs, but when I try both methods you suggested nothing happens. Clearly, I'm still doing something wrong...

@MasonJar, I tried copy/pasting your code just to make sure I could make it work and unfortunately I've not been successful on that front either. I made sure to have your Lib folder and AutoHotkey.dll all in the same directory as the compiled (and uncompiled) script but I got some errors. I ended up having to copy the files from the Required-Libraries folder into the Lib folder, along with a _struct.dll from the _H lib folder. Then I started getting a warning message that said the keyboard & mouse were disabled and asked if I wanted to continue. Clicking "Yes" ran the script but I only got the msgbox from the main thread and not the "first" thread. :crazy:

As clean and eloquent as these solutions appears to be, I think I will end up using FileInstall to bring the Ahk2Exe.exe and AutoHotkeySC.bin files, compile an ahk and then delete the files that were "FileInstall"ed. I know it's not the cleanest or most efficient, but it minimizes the number of loose/floating files and makes it easier for others to simply double-click an exe (as opposed to figuring out how to handle an ahk). Sadly, this is the only thing I've gotten to work so far.
User avatar
Masonjar13
Posts: 1289
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: AHK On-the-fly Compiler

06 Mar 2018, 20:28

AHK has Standard Libraries. If the functions/classes aren't either in the script, or included with an actual path, they must be in a standard library for the script to find them. No other dll's are necessary other than AutoHotkey (x86/x64). The only thing that isn't working with mine is readResource(). To get around that, you could extract the dll to the temp folder (or wherever) and reference it that way, then delete it when you're done.

For sake of simplicity, I'd recommend just including all the required files. Alternatively, you could use ScriptUp as-is; it should work for your purpose. You will still need to include the dll somehow, though.
JFrog
Posts: 5
Joined: 04 Mar 2018, 18:55

Re: AHK On-the-fly Compiler

07 Mar 2018, 09:02

Thanks so much MasonJar and guest3456, I appreciate the time you've both taken to help :D
guest3456
Posts: 2280
Joined: 09 Oct 2013, 10:31

Re: AHK On-the-fly Compiler

07 Mar 2018, 14:41

JFrog wrote:@guest3456, does the compiled.exe need to have any special code to handle the /E switch? I've got script.ahk with only "msgbox hello" to test this and see if it runs, but when I try both methods you suggested nothing happens. Clearly, I'm still doing something wrong...


are you using an exe compiled with AHK_H or with regular AHK? only AHK_H exe's will respond to the /E switch

JFrog
Posts: 5
Joined: 04 Mar 2018, 18:55

Re: AHK On-the-fly Compiler

08 Mar 2018, 16:40

guest3456 wrote:are you using an exe compiled with AHK_H or with regular AHK? only AHK_H exe's will respond to the /E switch

yes, I made sure to compile it with AHK_H
guest3456
Posts: 2280
Joined: 09 Oct 2013, 10:31

Re: AHK On-the-fly Compiler

08 Mar 2018, 20:57

JFrog wrote:
guest3456 wrote:are you using an exe compiled with AHK_H or with regular AHK? only AHK_H exe's will respond to the /E switch

yes, I made sure to compile it with AHK_H


I think there are some bugs with the current AHK_H. When I tested it earlier, I used an older version of AHK_H. Once HotKeyIt fixes it, it should work again. I'll let you know. Its very simple and works well.

User avatar
Masonjar13
Posts: 1289
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: AHK On-the-fly Compiler

08 Mar 2018, 21:08

guest3456 wrote:Its very simple and works well.

Seems like it would be exceptionally simple (and useful)! But I'm curious: if you add a custom password and then build _H, will this password be passed on to any scripts compiled this way? Since there's a default password, I guess this would have been in his mind while he was making it, but I've never used _H besides in dll form.
guest3456
Posts: 2280
Joined: 09 Oct 2013, 10:31

Re: AHK On-the-fly Compiler

08 Mar 2018, 21:35

Masonjar13 wrote:But I'm curious: if you add a custom password and then build _H, will this password be passed on to any scripts compiled this way? Since there's a default password, I guess this would have been in his mind while he was making it, but I've never used _H besides in dll form.


Yes, I've done this. So if you change the pw within the AHK_H source, then you have to use Visual Studio to build your own AHK_H interpreter and .bin files. Then when you use the Ahk2Exe, you choose your custom built .bin and there is a box for the password that you have to input to match the pw from the source

guest3456
Posts: 2280
Joined: 09 Oct 2013, 10:31

Re: AHK On-the-fly Compiler

08 Mar 2018, 22:37

JFrog wrote:@guest3456, does the compiled.exe need to have any special code to handle the /E switch? I've got script.ahk with only "msgbox hello" to test this and see if it runs, but when I try both methods you suggested nothing happens. Clearly, I'm still doing something wrong...


ok so make sure you download the latest ahk_h-v1-release. then when you compile with Akh2Exe, you need to choose the AHK_H.exe as the 'base file', not the .bin. Apparently that's the only way the /E switch will work.

here's a gif

https://i.imgur.com/OpfEQws.gifv


Return to “Ask For Help”

Who is online

Users browsing this forum: Flipeador, Telog and 84 guests