AutoHotkey / Ahk2Exe

Official AutoHotkey script compiler - written itself in AutoHotkey
https://autohotkey.com/
Do What The F*ck You Want To Public License
599 stars 113 forks source link

Compatibility issue with long continuation sections in AHKv2 #74

Closed Nick768 closed 3 years ago

Nick768 commented 3 years ago

Hello,

i've written a script to write a hex string copied from HxD to a file. That means i can copy a hex pattern from a .jpg file (for example) and paste it into a continuation section in my script. The script then writes every decimal equivalent of a hex number with WriteChar() to my file. But if i want to compile my script (i use AHK v2.0-a134-d3d43350 Unicode x64 and compile it with the current Ahk2Exe version) and there is a long continuation section, Ahk2Exe returns "Error: The script contains syntax errors. [...] Continuation section too long.". When i use resource hacker to put my script into the AHK v2 Unicode x64 .bin file, it works perfectly. What i've tried:

My script:

SetWorkingDir A_ScriptDir ; Ensures a consistent starting directory.

hxStr := "
(
89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52 00 00 03 ; ... lots more here
)"
f := FileOpen("new.jpg", "w")
writeHexToFile(f, hxStr)
f.Close()

return

writeHexToFile(file, hexString)
{
    hex := {}
    i := 0
    while i <= 255
    {
        j := Format("{:#x}", i)
        if (j = 0)
        {
            j := "00"
        }
        else if (j < 16)
        {
            j := StrReplace(j, "0x", "0")
        }
        else
        {
            j := StrReplace(j, "0x", "")
        }

        hex.%j% := i
        i++
    }

    if (SubStr(hexString, 3, 1) = " ") ; Mode 1
    {
        chars := StrSplit(hexString, " ")
        for _, char in chars
        {
            char := StrLower(char)
            file.WriteChar(hex.%char%)
        }
    }
    else ; Mode 2
    {
        i := 1
        while i < StrLen(hexString)
        {
            char := StrLower(SubStr(hexString, i, 2))
            file.WriteChar(hex.%char%)
            i += 2
        }
    }
    return
}

Is it possible to port Ahk2Exe to AHKv2? This compiler could work, but it renames the script and then the generated .exe is not able to find and start this script...

Best regards, Nick768

TAC109 commented 3 years ago

Can you post the exact text of the error message, please

TAC109 commented 3 years ago

Also check that your AutoHotkey directory structure is correct. After unzipping the v2 download you should place a very recent version of Ahk2Exe into the Compiler directory that was just created, and run that copy of Ahk2Exe to create your executable. (Get the copy of Ahk2Exe from a v1.1.33.09 download.)

The _H version of Ahk2Exe is not compatible with the standard _L versions of AutoHotkey.

Nick768 commented 3 years ago

My settings and the message:

Images ![Compilation_Settings](https://user-images.githubusercontent.com/52794307/118789664-4a3bc100-b895-11eb-86f8-c3f2466e8d66.png) ![ErrorMsg](https://user-images.githubusercontent.com/52794307/118789666-4ad45780-b895-11eb-9b6a-9c00ff894e8a.png)

I've tested the Ahk2Exe version bundled with the installer (AHK_L 1.1.33.09 ; installed with setup.exe). To use AHKv2 i've downloaded the zip file (v2.0-a134-d3d43350) and extracted it to "C:\Program Files\AutoHotkey\v2-alpha" and created subfolders for x32 and x64 version. My directory structure:

unix tree command output ```dos C:\Program Files\AutoHotkey>tree . ├── AutoHotkey Website.url ├── AutoHotkey.chm ├── AutoHotkey.exe ├── AutoHotkeyA32.exe ├── AutoHotkeyA32_UIA.exe ├── AutoHotkeyU32.exe ├── AutoHotkeyU32_UIA.exe ├── AutoHotkeyU64.exe ├── AutoHotkeyU64_UIA.exe ├── Compiler │   ├── ANSI 32-bit.bin │   ├── Ahk2Exe.exe │   ├── AutoHotkeySC.bin │   ├── Unicode 32-bit.bin │   ├── Unicode 64-bit.bin │   ├── V2 Unicode 32-bit.bin │   ├── V2 Unicode 64-bit.bin │   └── readme.txt ├── Installer.ahk ├── SciTE │   ├── ├── WindowSpy.ahk ├── license.txt └── v2-alpha ├── x32 │   ├── AutoHotkey.chm │   └── AutoHotkey.exe └── x64 ├── AutoHotkey.chm └── AutoHotkey.exe 19 directories, 143 files ```

But i think i've found the issue: I made a simple test script which is compatible with v1.1 and v2:

MsgBox "Hello World"

I've cloned the AutoHotkey/Ahk2Exe git and debugged the whole compilation with the v1.1 .bin file. Everything works great. Then i've debugged the compilation with v2 .bin file. The result: If i compile an AHKv2 script, the compiler runs RunWait, "%comspec%" /c ""%AhkPath%" /iLib "%ilibfile%" /ErrorStdOut "%AhkScript%" 2>"%tmpErrorLog%"", %FirstScriptDir%, UseErrorLevel Hide but %AhkPath% points to C:\Program Files\AutoHotkey\AutoHotkey.exe, which is a v1.1 version of AHK. When i replace this file with a v2 executable, i can compile everything i want.

How i debug the compiler ```diff diff --git "a/C:\\Users\\***\\Desktop\\Ahk2Exe-master\\ScriptParser.ahk" "b/C:\\Users\\***\\Desktop\\debugging\\ScriptParser.ahk" index bf898f5..17649ab 100644 --- "a/C:\\Users\\***\\Desktop\\Ahk2Exe-master\\ScriptParser.ahk" +++ "b/C:\\Users\\***\\Desktop\\debugging\\ScriptParser.ahk" @@ -172,6 +172,7 @@ PreprocessScript(ByRef ScriptText, AhkScript, ExtraFiles, FileList := "", FirstS Loop, % !!IsFirstScript ; Like "if IsFirstScript" but can "break" from block { global AhkPath + MsgBox, % AhkPath . "`n" . DerefIncludeVars.A_AhkVersion IfNotExist, %AhkPath% { Util_Error("Warning: AutoHotkey.exe could not be located!`n`nAuto-include" . "s from Function Libraries, and 'Obey' directives will not be processed.",0) ```

Then i've added some code and copied a file from HotKeyIt/Ahk2Exe:

Code changes ```diff diff --git "a/C:\\Users\\***\\Desktop\\patched/Lib/FileGetInfo.ahk" "b/C:\\Users\\***\\Desktop\\patched/Lib/FileGetInfo.ahk" new file mode 100644 index 0000000..e977052 --- /dev/null +++ "b/C:\\Users\\***\\Desktop\\patched/Lib/FileGetInfo.ahk" @@ -0,0 +1,20 @@ +;~ MsgBox % FileGetInfo(A_AhkPath,"FileDescription") +FileGetInfo( peFile:="", p*) { ; Written by SKAN, modified by HotKeyIt + ; www.autohotkey.com/forum/viewtopic.php?p=233188#233188 CD:24-Nov-2008 / LM:27-Oct-2010 + static DLL:="Version\GetFileVersion" + If ! FSz := DllCall( DLL "InfoSize" (A_IsUnicode ? "W" : "A"), "Str",peFile, "UInt",0 ) + Return DllCall( "SetLastError", UInt,1 ),"" + VarSetCapacity( FVI, FSz, 0 ),DllCall( DLL "Info" ( A_IsUnicode ? "W" : "A"), "Str",peFile, "UInt",0, "UInt",FSz, "PTR",&FVI ) + If !DllCall( "Version\VerQueryValue" (A_IsUnicode ? "W" : "A"), "PTR",&FVI, "Str","\VarFileInfo\Translation", "PTR*",Transl, "PTR",0 ) + Return DllCall( "SetLastError", UInt,2 ),"" + If !Trans:=format("{1:.8X}",NumGet(Transl+0,"UInt")) + Return DllCall( "SetLastError", UInt,3),"" + for k,v in p + { subBlock := "\StringFileInfo\" SubStr(Trans,-3) SubStr(Trans,1,4) "\" v + If ! DllCall( "Version\VerQueryValue" ( A_IsUnicode ? "W" : "A"), "PTR",&FVI, "Str",SubBlock, "PTR*",InfoPtr, "UInt",0 ) + continue + If Value := StrGet( InfoPtr ) + Info .= p.MaxIndex()=1?Value:SubStr( v " ",1,24 ) . A_Tab . Value . "`n" + } Info:=RTrim(Info,"`n") +Return Info +} \ No newline at end of file diff --git "a/C:\\Users\\***\\Desktop\\Ahk2Exe-master/ScriptParser.ahk" "b/C:\\Users\\***\\Desktop\\patched/ScriptParser.ahk" index bf898f5..bf9c068 100644 --- "a/C:\\Users\\***\\Desktop\\Ahk2Exe-master/ScriptParser.ahk" +++ "b/C:\\Users\\***\\Desktop\\patched/ScriptParser.ahk" @@ -2,6 +2,8 @@ ; File encoding: UTF-8 with BOM ; +#include %A_ScriptDir%\lib\FileGetInfo.ahk + PreprocessScript(ByRef ScriptText, AhkScript, ExtraFiles, FileList := "", FirstScriptDir := "", Options := "", iOption := 0) { global DirDone SplitPath, AhkScript, ScriptName, ScriptDir @@ -172,6 +174,18 @@ PreprocessScript(ByRef ScriptText, AhkScript, ExtraFiles, FileList := "", FirstS Loop, % !!IsFirstScript ; Like "if IsFirstScript" but can "break" from block { global AhkPath + if (SubStr(DerefIncludeVars.A_AhkVersion,1,1)=2) + { Loop, %AhkPath%\..\AutoHotKey*.exe,0,1 + { FileGetVersion, ver, %A_LoopFileFullPath% + if (InStr(ver,"2.") and InStr(FileGetInfo(A_LoopFileFullPath,"FileDescription"),"AutoHotkey")) + { AhkPath = %A_LoopFileFullPath% + break + } + } + } IfNotExist, %AhkPath% { Util_Error("Warning: AutoHotkey.exe could not be located!`n`nAuto-include" . "s from Function Libraries, and 'Obey' directives will not be processed.",0) ```

I don't know if my code is good enough to fix the problem permanently, but it works perfectly for me. Perhaps it would make more sense to copy a v2 AutoHotkey.exe to the compiler directory (should also compile v1.1 scripts) and change %AhkPath% like this:

AhkPath = %A_ScriptDir%\AutoHotkeyV2.exe

I hope this helps everybody to find a way to compile V2 scripts with long continuation sections. The issue could be closed if nobody wants to get more information...

TAC109 commented 3 years ago

Thanks. Also the directions I gave in my post would have fixed the problem for you as well.

There are changes planed for Ahk2Exe which will avoid this in the future.