Jump to content

How to use Execute() in a array?


Recommended Posts

Hello,

I will start off by saying that I'm a self taught hobbyist programmer, and new to Autoit. I apologize pretense if my technical terminology and usage is incorrect. A lot of reading and fiddling around on Google while I learn Autoit. The help file has been an outstanding resource. Anyways, I thought I should state that before asking my next question.

I have some functions that are meant to convert and store Autoit expressions in binary form in the Windows registry. When the function is needed the binary data is retrieved from the Windows registry key, converted back to a string, and passed through Execute() Everything works the way it should as long as there is only one expression on one line otherwise with multiple expressions Execute() does not behave the way I want it to. The only thing my little programming knowledge and common sense can come up with is that the Execute() must be passed through an array of sorts.

Included below is the UDF, GUI, & 2 examples. _EXAMPLES_1.au3 works as it should _EXAMPLES_2.au3 demonstrates the problem more clearly. The function is in RegApp_UDF.au3 on line 36. The GUI is meant to help automate the functions in the UDF. As always thank you for your assistance.

RegApp_UDF.au3

#include-once

#include
#include

; store the binary data in the clipboard
; $val = The name of the Autoit script to convert and store to clipboard
Func _StoreRegApp($val)
Local $file = FileOpen($val)
While 1
Local $str = FileRead($file)
If $str = '' Then ExitLoop
Return ClipPut(_StringToHex($str))
WEnd
FileClose($file)
EndFunc

; write binary data to windows registry
; $val = File name or embeded string | $key = Registry key name | $ref = Value name | $mod = 0(File) 1(Embeded String) - optional
Func _WriteRegApp($val, $key, $ref, $mod = 0)
If $mod = 1 then
Return RegWrite('HKEY_CURRENT_USER\Software\RBApplications\'&$key, $ref, 'REG_BINARY', $val)
Else
Local $file = FileOpen($val)
While 1
Local $str = FileRead($file)
If $str = '' Then ExitLoop
Return RegWrite('HKEY_CURRENT_USER\Software\RBApplications\'&$key, $ref, 'REG_BINARY', _StringToHex($str))
WEnd
FileClose($file)
EndIf
EndFunc

; convert and execute the specified registry binary data
; $key = Registry key name | $val = Registry string value
Func _ExecuteRegApp($key, $val)
$ret = RegRead('HKEY_CURRENT_USER\Software\RBApplications\'&$key, $val)
Execute(_HexToString(BinaryToString($ret)))
EndFunc

RegApp_GUI.au3

#include
#include "RegApp_UDF.au3"

; a simple gui to help automate the udf

Global $width = 320, $height = 40

$gui = GUICreate("Registry Applications", $width, $height, -1, -1)

$button1 = GUICtrlCreateButton ("Store", 5, 5, 100, 30)
$button2 = GUICtrlCreateButton ("Write", 110, 5, 100, 30)
$button3 = GUICtrlCreateButton ("Execute", 215, 5, 100, 30)

GUISetState(@SW_SHOW)

While 1
Switch GUIGetMsg()

Case $GUI_EVENT_CLOSE
Exit

Case $button1
Local $ret = FileOpenDialog('', @ScriptDir & "\", "All Files (*.*)", 1)
_StoreRegApp($ret)
If $ret = '' then
; do nothing
Else
MsgBox(64, 'Return Message', 'The contents of '&$ret&' has been stored to the clipboard. Use ctrl+v to paste the data where needed')
EndIf

Case $button2
Local $ret = FileOpenDialog('', @ScriptDir & "\", "All Files (*.*)", 1)
Local $key = InputBox('Key Name', 'Specify the key name.'&@LF&'Example: MyKeyName', '', '')
Local $ref = InputBox('Value Name', 'Specify the value name.'&@LF&'Example: MyScriptFunction', '', '')
If $ret = '' or $ref = '' or $key = '' then
; do nothing
Else
_WriteRegApp($ret, $key, $ref)
MsgBox(64, 'Return Message', $ret&' is stored in key '&$key&' under value '&$ref)
EndIf

Case $button3
Local $key = InputBox('Key Name', 'Specify the key name.'&@LF&'Example: MyKeyName', '', '')
Local $ref = InputBox('Value Name', 'Specify the value name.'&@LF&'Example: MyScriptFunction', '', '')
If $ret = '' or $key = '' then
; do nothing
Else
_ExecuteRegApp($key, $ref)
EndIf

EndSwitch
WEnd

_EXAMPLES_1.au3

#include
#include
#include
#include
#include "RegApp_UDF.au3"

; a few examples

Global $iMemo
Global $BinMsgBox = '4D7367426F7828302C20275265676973747279' & _
'204175746F69742042696E617279204170706C' & _
'69636174696F6E204578616D706C65272C2027' & _
'5468697320697320616E206578616D706C6520' & _
'6F662072756E6E696E67206120636F6E766572' & _
'7465642062696E617279204175746F69742073' & _
'63726970742066756E6374696F6E2066726F6D' & _
'2057696E646F77732052656769737472792E2729'

$gui = GUICreate("Examples", 320, 40, -1, -1)
$button1 = GUICtrlCreateButton ("Example 1", 5, 5, 100, 30)
$button2 = GUICtrlCreateButton ("Example 2", 110, 5, 100, 30)
$button3 = GUICtrlCreateButton ("Example 3", 215, 5, 100, 30)

GUISetState(@SW_SHOW)

While 1
$msg = GUIGetMsg()
Switch $msg

Case $GUI_EVENT_CLOSE
Exit

Case $button1
MsgBox(64, 'Example 1 - Store Application', 'Convert this script into binary data and store its contents in the clipboard.')
_StoreRegApp('_EXAMPLES.au3')
Local $ret = ClipGet()
MsgBox(64, 'Converted Return Data', $ret)

Case $button2
MsgBox(64, 'Example 2 - Write Application', 'Creates a registry application key with binary data.')
_WriteRegApp($BinMsgBox, 'MsgBox_Example', 'MsgBox', 1)
_RegJump('HKCU\Software\RBApplications\MsgBox_Example')

Case $button3
MsgBox(64, 'Example 3 - Execute Application', 'Executes registry Autoit binary data that was set in Example 2.')
_ExecuteRegApp('MsgBox_Example', 'MsgBox')
RegDelete('HKCU\Software\RBApplications')

EndSwitch
WEnd

; credit to Yashied for the _RegJump function (http://www.autoitscript.com/forum/topic/117907-regjump/#entry820919)
Func _RegJump($sKey)

Local $Root, $Text = StringSplit($sKey, '\', 2)

If IsArray($Text) Then
$Text = $Text[0]
Else
$Text = $sKey
EndIf
Switch $Text
Case 'HKEY_CLASSES_ROOT', 'HKEY_CURRENT_USER', 'HKEY_LOCAL_MACHINE', 'HKEY_USERS', 'HKEY_CURRENT_CONFIG'
$Root = $Text
Case 'HKCR'
$Root = 'HKEY_CLASSES_ROOT'
Case 'HKCU'
$Root = 'HKEY_CURRENT_USER'
Case 'HKLM'
$Root = 'HKEY_LOCAL_MACHINE'
Case 'HKU'
$Root = 'HKEY_USERS'
Case 'HKCC'
$Root = 'HKEY_CURRENT_CONFIG'
Case Else
Return 0
EndSwitch

Local $Class = '[CLASS:RegEdit_RegEdit]', $Delay = Opt('WinWaitDelay', 0)
Local $Prev, $Result = 1

If WinExists($Class) Then
WinClose($Class)
If Not WinWaitClose($Class, '', 5) Then
$Result = 0
EndIf
EndIf
If $Result Then
$Prev = RegRead('HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit', 'Lastkey')
If @error Then
$Prev = 0
EndIf
If Not RegWrite('HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit', 'Lastkey', 'REG_SZ', StringReplace($sKey, $Text, $Root, 1)) Then
$Result = 0
Else
If Not Run('regedit.exe') Then
$Result = 0
If IsString($Prev) Then
RegWrite('HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit', 'Lastkey', 'REG_SZ', $Prev)
EndIf
EndIf
EndIf
EndIf
Opt('WinWaitDelay', $Delay)
Return $Result
EndFunc ;==>_RegJump

_EXAMPLES_2.au3

#include "RegApp_UDF.au3"

#cs $embededData =
msgbox(0, '', 'Hello')
msgbox(0, '', 'World')
#ce
Global $embededData = '6D7367626F7828302C2027272C202748656C6C6F27290D0A6D7367626F7828302C2027272C2027576F726C642729'

_WriteRegApp($embededData, 'ExampleApplication', 'MsgBox_1', 1)
_ExecuteRegApp('ExampleApplication', 'MsgBox_1')

Download:

RegApps.zip

Link to comment
Share on other sites

Thank you for the reply JohnOne,

Is there a way to make happen what I am looking for, and if so which direction should I be looking? In a literal sense writing out the script in whole would be sarcastic common sense. If by writing you mean dumping to a temp file, that is what I am trying to avoid.

This is more a novelty project than anything. I found it interesting that I could store these values in this way, and if there would be a way to retrieve them, I started thinking I could do away with the physical files and just store functions in the registry grabbing them when needed with no need for multitudes of endless UDF library files. Then I started thinking this might be an interesting approach to a tag based filing system... maybe a custom filedialog of some sort with no physical files but rather stored registry values that can be converted and interpreted.

There again was no reason to do the hex to binary conversion like any of the rest of it, it just looked neater and cleaner in the registry to me. It would be an interesting idea of paranoia though to encrypt the output of the conversion but again no practical reason for it. How do we go about doing executing this function?

Thank you again JohnOne

Link to comment
Share on other sites

I'm a bit confused as to how to implement "AutoIt3ExecuteScript". After searching the "Help File" & internet I turned up unsuccessful. I tried using the execute line switch but the variables are undefined so they are not recognized. I imagine this what you were referencing to using "AutoIt3ExecuteScript" passing the vars as params or something. Could you be so kind as to providing a example? i am a bit confused. Thank you.

Link to comment
Share on other sites

This is complicated. I have done exactly this, but my code is a first attempt and needs to be rewritten. I am using Assign to declare variables and executing lines of code in sequence. I wouldn't try to make too much sense of the code, because it's really a mess, but if you look at the code which begins with the line => 'For $foo = 1 To $_aScriptArr_[0] ; Parse each line sequence' ... it might give you some ideas.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...