Jump to content
Sign in to follow this  
Mat

extra functions in an exe

Recommended Posts

Mat

Basically, I have an exe, but I need the ability to include a set of user defined functions, that can be changed, and might not even exist!!

All I need to do is before #includes are rgistered, run a simple function that checks to see if a file exists, and If it doesnt, write it.

The best I got was:

#OnAutoItStartRegister "_Check"

#include "test2.au3"

_Msg ()

Func _Check ()
   If Not FileExists (@ScriptDir & "\test2.au3") Then FileWrite (@ScriptDir & "\test2.au3", "Func _Msg ()" & @CRLF & _
      "MsgBox (0, ""Message"", ""success"")" & @CRLF & "EndFunc; ==> _Msg")
EndFunc

with no success.

I also tried reading the file, and doing something with that possibly, but I can't seem to be able to execute the functions there. I can if I use execute, but these functions are being potentially called very often.

solution? a preprocessor maybe? hacking the exe and injecting somehting in there? I am hoping to only have to have the single exe, and any files needed may be generated from there... If that makes a difference.

MDiesel

Share this post


Link to post
Share on other sites
BrettF
Lars

Hi MDiesel,

What I should do is let the app test if the include is present, if not write the include and restart the app.

The next time the app start the include is there and it can continue normally.

Share this post


Link to post
Share on other sites
Mat

Brett,

Its my mini calculator script (see sig), I am expanding it to include custom functions as well as variables. Variables are stored in the ini, and thats working np. Its the functions I have a problem with. I can write the file fine, and I have all the logic for adding them done, but then when I tried running it standalone, as I want it to be, I got the error that the file could not be found.

lars,

yes, but its an exe, so I can't edit it. I could have a single exe that that runs it, and acts as Autoit3.exe in the directory, and links all the scripts, but as said, I'm hoping for it to be a simple standalone, run the exe and your away program.

I'm having a look at a preprocessor for autoit, project manager, and trying to work out if it can be done like that.

Can I inject functions into an exe? I don't know... here?

Edited by mdiesel

Share this post


Link to post
Share on other sites
BrettF
Mat

What about interpreting it?

?? explain on, I am all ears. By interpreting what do you mean reading the file and parsing the result as autoit does?

MDiesel

Edited by mdiesel

Share this post


Link to post
Share on other sites
Lars

Wow this reminds me of a university price question, -> Write an exe that shows it's own source without including it's source and echo it.... :D

Do you really need to end up with one exe file?? A simple DLL with exported functions can do the trick..

Share this post


Link to post
Share on other sites
Mat

Thanks a lot, I'm looking through that now.

There are no restrictions on what It has to be, so I suppose it doesn't really matter, But I was hoping to get it to the point where I only need 1 file...

Unfortunately my knowledge on how exe's and dll's work is rather basic, I can understand how to manipulate them, but the inner working remain a mystery to me.

MDiesel

Share this post


Link to post
Share on other sites
martin

Basically, I have an exe, but I need the ability to include a set of user defined functions, that can be changed, and might not even exist!!

All I need to do is before #includes are rgistered, run a simple function that checks to see if a file exists, and If it doesnt, write it.

The best I got was:

#OnAutoItStartRegister "_Check"
 
 #include "test2.au3"
 
 _Msg ()
 
 Func _Check ()
    If Not FileExists (@ScriptDir & "\test2.au3") Then FileWrite (@ScriptDir & "\test2.au3", "Func _Msg ()" & @CRLF & _
       "MsgBox (0, ""Message"", ""success"")" & @CRLF & "EndFunc; ==> _Msg")
 EndFunc

with no success.

I also tried reading the file, and doing something with that possibly, but I can't seem to be able to execute the functions there. I can if I use execute, but these functions are being potentially called very often.

solution? a preprocessor maybe? hacking the exe and injecting somehting in there? I am hoping to only have to have the single exe, and any files needed may be generated from there... If that makes a difference.

MDiesel

I think all you need to do is to have your example script like this

#AutoIt3Wrapper_Run_Before=settest.exe
#include "test2.au3"

_Msg ()

and have already compiled this to settest.exe for example

If Not FileExists (@ScriptDir & "\test2.au3") Then FileWrite (@ScriptDir & "\test2.au3", "Func _Msg ()" & @CRLF & _
"MsgBox (0, ""Message"", ""success"")" & @CRLF & "EndFunc; ==> _Msg")

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
PsaltyDS

I think all you need to do is to have your example script like this

#AutoIt3Wrapper_Run_Before=settest.exe
#include "test2.au3"

_Msg ()

and have already compiled this to settest.exe for example

If Not FileExists (@ScriptDir & "\test2.au3") Then FileWrite (@ScriptDir & "\test2.au3", "Func _Msg ()" & @CRLF & _
"MsgBox (0, ""Message"", ""success"")" & @CRLF & "EndFunc; ==> _Msg")
That will get the function done, but I think he has deluded himself that the changed contents of test2.au3 will be included in the current running script because he thinks it somehow gets done before the #include happens.

@mdiesel: #include is a COMPILE TIME directive. The code from an included file is already in the EXE at RUN TIME (when you run the compiled script) it's too late to change it. Any changes you make to test2.au3 will not show up in the EXE until you recompile it. The #include files are not accessed at RUN TIME!

:D


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Mat

Another solution, thanks martin, Still Means I have to have 2 exes... which in many ways is the worst solution as they are chunky enough as it is. also, thats sets it to run before compilation. all I need is a simple fileInstall function to add test2.au3 to the script before running

@PSaltyDS: Yes, I know that, I'm looking for an alternative.

MDiesel

Edited by mdiesel

Share this post


Link to post
Share on other sites
martin

That will get the function done, but I think he has deluded himself that the changed contents of test2.au3 will be included in the current running script because he thinks it somehow gets done before the #include happens.

:D

It does so he is not deluded.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
Mat

kk. It appears the explanation of what I want is not clear.

at RUNTIME, an au3 file in the directory containing functions needs to be accessible by commands within the exe. Injecting the exe is not going to work, neither is include, and neither is running another process before compilation.

so far the only positive lead is the idea of an interpreter. If someone could explain how to possibly go about that, I would be much abliged.

MDiesel

Share this post


Link to post
Share on other sites
martin

kk. It appears the explanation of what I want is not clear.

at RUNTIME, an au3 file in the directory containing functions needs to be accessible by commands within the exe. Injecting the exe is not going to work, neither is include, and neither is running another process before compilation.

so far the only positive lead is the idea of an interpreter. If someone could explain how to possibly go about that, I would be much abliged.

MDiesel

Ok, I probably didn't understand, but I'm not sure I do yet. So do you mean

1 you have an exe which you can't change or don't want to

2 the exe calls an au3 script, with a parameter say, so that the au3 script can execute a command for the exe. You want the au3 script to be able to give a default answer if I doesn't know how to deal with the parameter, and the default answer can be found in another script like test2.au3.

If I've got that right then it doesn't sound too difficult.

Does the au3 script have to pass a result back to the exe or just execute some function and just say it's been done?


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
Tvern

kk. It appears the explanation of what I want is not clear.

at RUNTIME, an au3 file in the directory containing functions needs to be accessible by commands within the exe. Injecting the exe is not going to work, neither is include, and neither is running another process before compilation.

so far the only positive lead is the idea of an interpreter. If someone could explain how to possibly go about that, I would be much abliged.

MDiesel

I achieved something that might come close to what you are looking for using:

Run('"' & @ScriptFullPath & '"' & ' /AutoIt3ExecuteScript  func1.au3')

I believe this is probably the interpreter approach. When the exe is clicked it will use itself to run the au3 file and exit. The au3 file will still execute normally. For more advanced integration into your program you might be able to use StdoutRead() and StdinWrite().

edit: afaik you can also pass arguments to func1.au3, but only as a string.

Edited by Tvern

Share this post


Link to post
Share on other sites
Mat

You're both wrong again. but closer anyhow.

the exe uses the functions in the script.

Full explanation:

Welcome to my mini calculator. nice little script, just needs a bit of "wrapping up" to make it perfect. 2 of the things I have outlined for this expansion is the ability to add and save variables and functions. Now the variables are a peice of cake. They're stored alonside the settings in the programs ini. The functions, however, are much trickier, as the exe needs to actually run the functions, if the custom function is called. Luckily the way I have handled the calculations mean all that has to be done is for the name to be added to an array, so it can run that function. excerpt from script:

For $i = 0 to UBound ($aOps) - 1
         If StringRegExp ($sCalc, $aOps[$i]) <> 0 Then
            $n = StringRegExp ($sCalc, $aOps[$i] & "\(([^\)]*)\)", 3)
            For $x = 0 to UBound ($n, 1) - 1
               If StringInStr ($n[$x], ",") Then
                  $nSplit = StringSplit ($n[$x], ",")
                  For $s = 1 to $nSplit[0]
                     $n[$x] &= Execute ($nSplit[$s])
                  Next
               Else
                  $n[$x] = Execute ($n[$x])
               EndIf
               $sCalc = StringReplace ($sCalc, $aOps[$i] & "(" & $n[$x] & ")", Execute ($aOps[$i] & "(" & $n[$x] & ")"))
            Next
            ExitLoop
         EndIf
      Next

where $aOps is the array of functions.

now the problem comes when it attampts to execute the custom function. as it does not exist within the exe. so i need to "include" the script containing all the custom functions definitions. Without changing the exe. This is turning out to be a pain. the only method I know of is to read the script and find the function and execute it. several problems there: This is happening as the user types, so it needs to be very quick, but also variables. at the moment the structure is simple:

Func custom($x)
Return 2*$x
EndFunc

If you want a good example have a look at the script, treating deg and rad functions as custom, and you will see what I mean.

MDiesel

Share this post


Link to post
Share on other sites
PsaltyDS

You're both wrong again. but closer anyhow.

the exe uses the functions in the script.

Full explanation:

Welcome to my mini calculator. nice little script, just needs a bit of "wrapping up" to make it perfect. 2 of the things I have outlined for this expansion is the ability to add and save variables and functions. Now the variables are a peice of cake. They're stored alonside the settings in the programs ini. The functions, however, are much trickier, as the exe needs to actually run the functions, if the custom function is called. Luckily the way I have handled the calculations mean all that has to be done is for the name to be added to an array, so it can run that function. excerpt from script:

For $i = 0 to UBound ($aOps) - 1
         If StringRegExp ($sCalc, $aOps[$i]) <> 0 Then
            $n = StringRegExp ($sCalc, $aOps[$i] & "\(([^\)]*)\)", 3)
            For $x = 0 to UBound ($n, 1) - 1
               If StringInStr ($n[$x], ",") Then
                  $nSplit = StringSplit ($n[$x], ",")
                  For $s = 1 to $nSplit[0]
                     $n[$x] &= Execute ($nSplit[$s])
                  Next
               Else
                  $n[$x] = Execute ($n[$x])
               EndIf
               $sCalc = StringReplace ($sCalc, $aOps[$i] & "(" & $n[$x] & ")", Execute ($aOps[$i] & "(" & $n[$x] & ")"))
            Next
            ExitLoop
         EndIf
      Next

where $aOps is the array of functions.

now the problem comes when it attampts to execute the custom function. as it does not exist within the exe. so i need to "include" the script containing all the custom functions definitions. Without changing the exe. This is turning out to be a pain. the only method I know of is to read the script and find the function and execute it. several problems there: This is happening as the user types, so it needs to be very quick, but also variables. at the moment the structure is simple:

Func custom($x)
Return 2*$x
EndFunc

If you want a good example have a look at the script, treating deg and rad functions as custom, and you will see what I mean.

MDiesel

You want your calculator to accept adhoc scripts, written in AutoIt syntax. A neat idea, but you are bumping into big problems:

You can't declare new functions at run time. If a function wasn't there at compile time, it simply isn't available at run time. And editing an #include file at run time will not bring new functions into the executable. The only functions available at run time were the ones present in that #include at compile time.

Your only option within the same process of your script is Execute(), but that will not take loops, no multi-line expressions, requires Assign/Eval to work with variables, etc., etc. Writing the interpreter will be possible but hugely complicated.

With an external process you are just back to Tvern's use of an external script file and /AutoIt3ExecuteScript.

:D


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Mat

...

With an external process you are just back to Tvern's use of an external script file and /AutoIt3ExecuteScript.

:D

Yep, got there eventually didn't we! Unfortunately just executing isn't enough.

Alternatively I could let autoit do the work, by writing a script, and then using stdin/out to read results... Is there a way that could work?

MDiesel

Reason for edit: typo

Edited by mdiesel

Share this post


Link to post
Share on other sites
PsaltyDS

Yep, got there eventually didn't we! Unfortunately just executing isn't enough.

Alternatively I could let autoit do the work, by writing a script, and then using stdin/out to read results... Is there a way that could work?

MDiesel

Reason for edit: typo

As long as you are OK with external execution, there is a ray of hope. The adhoc text .AU3 file that you execute can already have some helper functions in it. You can require the adhoc scripter to use certain pre-defined variable names, like $iRET. Then you tell them whatever their adhoc does must return its result in $iRET. That gives you a known point to interface back to the parent script. For example the template might look like:
Global $iRET; Contents of this variable will be returned to parent script
Global $hParent = HWND(0x00012345); updated by parent script when creating.
_Main()

; Return $iRET to parent script with callback, WM_COPYDATA message, by writing to a GUI ctrl, etc., etc.

Func _Main()

    ; Adhoc code pasted here and must be suitable for complete execution inside this one function

EndFunc

:D


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
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
Sign in to follow this  

×