Local $sAxName Local $oMSComm $sAxName = "MSCOMMLib.MSComm.1" $oMSComm = ObjCreate($sAxName) MsgBox(0, Default, StringFormat("Name: %s, Obj %d, Err %d", $sAxName, IsObj($oMSComm), @error)) I'm talking to serial ports (for Arduino) using the MSComm object. It all runs fine from SciTE or .exe. If I compile to .a3x the object is not created. I could manage without .a3x but I like it because it compiles faster.
A3X Script Extract
With newer versions of AutoIT (v22.214.171.124+), the compiled script is no longer appended to files as an overlay, and instead is embedded as a binary resource. This leads my previous AutoIt-script detection UDF lacking. However, since that UDF (>PE File Overlay Extraction) was targeted towards overlays in general, its still a worthwhile tool to have.
This UDF on the other hand is pretty specific - it lets you detect and optionally extract A3X scripts from a compiled executable. The method is rather straightforward - it looks for a resource type of RT_RCDATA with a resource name of "SCRIPT", and then extracts the binary, testing for the A3X signature along the way.
The main UDF has two functions:
_FileContainsScriptResource() _FileContainsA3XScript() The first of these functions is there only for checking if a resource with "SCRIPT" exists. Its main purpose is to report on possible embedded scripts. I made this a separate function primarily because of issues with compressed executables. Tools like UPX and MPRESS can compress the resources as well as the rest of the executable, so any detection of the A3X signature will fail in those circumstances. (Note that UPX and MPRESS don't compress overlay data, which is why the older Overlay-Extraction A3X-detection worked regardless)
The second function will both check for and optionally return the A3X script resource as binary. It also does a signature check for verification.
Anyway, here's an example of its usage. The main UDF is attached below.
#include <_FileContainsA3XScript.au3> ; ======================================================================================================== ; <A3XScriptExtract.au3> ; ; Example of detecting AutoIt Scripts embedded as binary resources (in AutoIt v126.96.36.199+ exe's), and ; extracting them to .A3X files. ; ; This script can be invoked in interactive or command-line mode. ; Passing an executable as a parameter will extract an A3X resource, writing it out to ; an A3X file with the same base name as the executable. ; ; Uses <_FileContainsA3XScript.au3> functions ; ; Author: Ascend4nt ; ======================================================================================================== ; ---------------------- MAIN CODE ------------------------------- Local $sFile, $sLastDir, $sLastFile, $binA3X, $iErr, $nA3XSize ; Command-line parameter received? Simply do a direct A3X extraction If $CmdLine Then $sFile = $CmdLine If Not FileExists($sFile) Then Exit 1 $binA3X = _FileContainsA3XScript($sFile, True) If @error Then Exit @error $nA3XSize = @extended Local $nExt = StringInStr($sFile, '.', 0, -1) If $nExt Then $sFile = StringLeft($sFile, $nExt - 1) EndIf $sFile &= '.a3x' Exit Not FileWrite($sFile, $binA3X) EndIf ; No command-line parameters, query the user in interactive mode $sLastDir = @ScriptDir While 1 $sFile=FileOpenDialog("Select PE File To Look for Embedded A3X scripts In",$sLastDir,"PE Files (*.exe;*.dll;*.scr)|All Files (*.*)",3,$sLastFile) If @error Or $sFile="" Then Exit $sLastFile = StringMid($sFile, StringInStr($sFile, '\', 1, -1) + 1) $sLastDir = StringLeft($sFile, StringInStr($sFile, '\', 1, -1) - 1) ; Resource 'Script' check $bScriptResourceFound = _FileContainsScriptResource($sFile) ConsoleWrite("_FileContainsScriptResource() return: " & $bScriptResourceFound & ", @error = " & @error & ", @extended = " & @extended & @CRLF) ; Actual A3X script resource check. True to return the A3X script as binary $binA3X = _FileContainsA3XScript($sFile, True) $iErr = @error $nA3XSize = @extended ; No A3X script found? If $iErr Or $nA3XSize = 0 Then ConsoleWrite("Failed Return from _FileContainsA3XScript(), @error = " & $iErr & ", @extended = " & $nA3XSize & @CRLF) MsgBox(64, "No AutoIt A3X script resource Found", "AutoIt A3X script resource not Found!" & @CRLF & _ ($bScriptResourceFound ? "However, *A* script resource was found" : "No 'Script' resource found either") & @CRLF & _ "for '" & $sLastFile & "'") ContinueLoop EndIf ConsoleWrite("_FileContainsA3XScript() return type: " & VarGetType($binA3X) & ", value = " & (IsBinary($binA3X) ? "[A3X_Binary]" : $binA3X) & @CRLF) ConsoleWrite(@TAB & "@error = " & $iErr & ", @extended = " & $nA3XSize & @CRLF) Local $hFileOut = -1, $sOutFile, $iMsgBox, $bSuccess = 0 $iMsgBox = MsgBox(35, "A3X script resource found in " & $sLastFile, "A3X script resource found. File size: " & FileGetSize($sLastFile) & _ ", A3X script size: " & $nA3XSize & @CRLF & @CRLF & _ "Would you like to Extract and save A3X file?") If $iMsgBox = 6 Then ;~ If $nA3XSize > 134217728 Then ;~ MsgBox(48, "A3X script is too huge", "A3X script size is > 128MB, skipping..") ;~ ContinueLoop ;~ EndIf $sOutFile = FileSaveDialog("A3X script - SAVE: Choose a file to write A3X script data to (from " & $sLastFile&")", _ $sLastDir, "AutoIt Compiled Sript (*.a3x)|All (*.*)", 2 + 16) If @error Then ContinueLoop ; Simple check for extension - if none, add .a3x If StringInStr($sOutFile, '.') = 0 Then $sOutFile &= ".a3x" ; Else $hFileOut = FileOpen($sOutFile, 16 + 2) If $hFileOut <> - 1 Then $bSuccess = FileWrite($hFileOut, $binA3X) FileClose($hFileOut) EndIf Else ContinueLoop EndIf If $bSuccess Then ShellExecute(StringLeft($sOutFile,StringInStr($sOutFile,'\',1,-1)-1)) Else MsgBox(64, "Error Opening or writing to file", "Error opening, reading, or saving A3X file") EndIf WEnd Exit _FileContainsA3XScript.au3
*edit: Modified example: Now alternatively accepts a command-line parameter for automated script extraction