Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[AHK/v2] DynaRun() - Run AutoHotkey Process Dynamically


  • Please log in to reply
33 replies to this topic
HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

Thanks Lexikos for How to: Run Dynamic Script... Through a Pipe!
This is generally not different to Lexikos example, just little more compact and user friendly.
I release it especially to support CreateScript for non dll AutoHotkey scripts.

Requires Win2000++

Download DynaRun.ahk - or - DynaRun.ahk (v2 version)
 

 

DynaRun(Script,name,parameters)

Example:

DynaRun("MsgBox Pipe","Title from pipe")

To use DynaRun() in compiled script you will need to compile AutoHotkey.exe (not AutoHotkeySC.bin) from AutoHotkey_H pack with compiler included in the pack.



berban
  • Members
  • 202 posts
  • Last active: Jun 07 2016 02:58 AM
  • Joined: 30 Dec 2009
Hey,
Have you noticed with this script that it doesn't support comments? (Delineated with a ";" only). As far as I have noticed.

I just do
text := RegExReplace(text, "m)\s+;.*$")
Apart from that, really handy though, thanks!

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

This is because you have to escape those.

DynaRun("MsgBox test `;this is a test")


berban
  • Members
  • 202 posts
  • Last active: Jun 07 2016 02:58 AM
  • Joined: 30 Dec 2009
That's not exactly it, I wasn't using the function directly like that but rather from clipboard text.
DynaRun("MsgBox test ;this is a test")
DynaRun(ClipBoard)
When I put that text into the clipboard and then run it as in the second line, it creates a new process alright, it just refuses to launch the code, saying
Error at line 4.

Line Text: "MsgBox test
Error: Missing close-quote

The program will exit.
Whereas if I execute the line literally as in the first line, of course all that is compiled is DynaRun("MsgBox test, which doesnt even launch a new process.
I'm kind of confused by why this is happening but it's really not important so I'll let it be

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

As said before, you have to use escape character ( '`;' )

Clipboard:="MsgBox test `;this is a test"
DynaRun(Clipboard)


  • Guests
  • Last active:
  • Joined: --
Hi HotKeyIt, nice function.

I just realized that this does not work for compiled scripts run on a machine that AutoHotkey is not installed. It's because the function just simply creates a non-compiled script and run it. But I'm wondering if you happen to know a way of doing it. I mean a way to run a dynamic AHK code even from a compiled script where AHK is not installed.

Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Thanks for this great function!

 

I have a script I've created where I use DynaRun to evaluate variable expression on-the-go (posted at the bottom). The only problem with it is, if this function is called within a dll thread, the thread crashes. So, the solution, in principle, is simple: if the current AHK running is a thread, then call DynaRun from the executable --  am I right?

 

So my question is: is there a way to tell whether the currently running script is ran from the exe or from the dll?

 

Spoiler

Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

A_IsDll will contain 1 if it is an AutoHotkey.dll thread.

 

DynaRun returns the PID of new process, instead Sleep you can use Process,WaitClose. 

 

Also this works fine for me:

ahk:=AhkDllThread()
ahk.ahktextdll("MsgBox % DynaExpr_EvalToVar(""5+5"")`N" CreateScript("DynaExpr_EvalToVar{}"))
MsgBox end

DynaExpr_EvalToVar(sExpr)
{
	sTmpFile := A_Temp "\temp.ahk"

	sScript:="
	(
		#NoTrayIcon
		FileDelete " sTmpFile "

		val := " sExpr "
		FileAppend %val%, " sTmpFile "
	)"

	PID:=DynaRun(sScript)

	Process,WaitClose,%PID%
	FileRead sResult, %sTmpFile%

	return sResult
}


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Thanks! Wow, that was...woefully obvious :(.

 

What I meant to say about DynaRun crashing was that a threaded DynaRun would crash if the thread-caller is a compiled script. You can see this exact crash happen if you compile and run your example script. The the reason I am trying to use the exe for DynaRun is for when A_IsDll is true.


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

To DynaRun from compiled script you will need to compile from AutoHotkey.exe instead of AutoHotkeySC.bin using this compiler.

In compiled script you need to use /E switch to execute other than internal script.

If A_IsCompiled
  Run, %A_AhkPath% /E "\\.\pipe\...


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

To DynaRun from compiled script you will need to compile from AutoHotkey.exe instead of AutoHotkeySC.bin using this compiler.

 

Great! I have switched to using this compiler since you've helped me in the AutohotKey.dll thread. So how exactly do I apply what you are saying? Here's a screenshot of my best guess, but, alas, it still crashes.

 

Spoiler


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

AhkDllThread assumes that AutoHotkey.dll is installed in resources.

Add following at the top of your script:

If 0
  FileInstall,AutoHotkey.dll,Will be never extracted.


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

*Slaps forehead* Sorry! You have already told me that before :\.

 

Anyway, so I placed that at the top of my script. An odd thing happens, now. When I launch the exe, for some reason, it gets caught in an infinite loop and launches the executable, over and over again. Are you not getting this on your side?


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

Can you post the full script you try to compile?



Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Sure!

 

Spoiler

Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.