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

I have uploaded a fixed version that checks if an executable is Compiled, even when executed from AutoHotkey.dll ;)



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

Great. Thanks! Infinite recursion of launching the executable has been fixed; however, both in the script and compiled versions, I receive this message:

 

Error:

Spoiler

 

Script:

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

Have you go the latest AHK_H?

Also where does the AutoHotkey.dll get script from, try this. (CreateScript() can be found here.)

If 0
	FileInstall,AutoHotkey.dll,-

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

Have you go the latest AHK_H?

Also where does the AutoHotkey.dll get script from, try this. (CreateScript() can be found here.)

 

Yes, I just updated to the merged version before my last post. (and I made sure to update my lib folder, as well. FYI, the download for AHK_H includes the old version of DynaRun, so I had to re-replace DynaRun.ahk with the one from this topic)

 

"Where does AutoHotkey.dll get script from?" Not sure what you mean. My best guess says, in your above example, it gets the script from CreateScript(). If that is the case, then my AutoHotkey.dll does not get the script from anywhere.

 

Using your example produces the same "out of memory" error. In fact, for every time where you've replied with a script that uses CreateScript(), I've not been able to tell that CreateScript() is doing anything, at all. I have looked over the CreateScript file multiple times, and I still haven't a clue what it can be used for :\

 

In all of your scripts you've posted here, I've tested your script first, and then I removed the CreateScript(). However, functionally, the modified script operated the same way as the original -- no change at all.


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

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


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

Okay. I'm trying to add what I can here.

 

(a.) The problem is not my install. I started a new _H from scratch, ran our script, and the out of memory error still appeared.

(b.) I think I understand part of CreateScript now. To summarize, CreateScript allows smooth interaction between two scripts. In your example, you show how to create a GUI in one script and wrap the labels within CreateScript to labels within the calling script. But I still don't understand it's use here, especially since it appears that no parameters are getting passed to the function. But my lack of knowledge isn't the real issue here.

 

Given the error, it looks like it might really be in AhkDLLThread and your change to DynaRun just exposed this bug? I'm not entirely certain of what I'm looking for, but, if I can be of any help tracking down this problem, I will.


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

CreateScript allows to reuse various parts of the script inside AutoHotkey.dll (since that is not compiled).

I have tested on XP and Win 7 32 bit and all works as expected, do you have another computer to try on?

 

Try to compile below script, here AutoHotkey.dll will use included DynaRun function too.

If 0
	FileInstall,AutoHotkey.dll,-

ahk:=AhkDllThread()
ahk.ahktextdll("MsgBox % DynaExpr_EvalToVar(""5+5"")`n" CreateScript("DynaExpr_EvalToVar{}`nDynaRun{}"))
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
}
DynaRun(TempScript, pipename="", params="")
{
   static _:="uint",@:="Ptr"
   If pipename =
      name := "AHK" A_TickCount
   Else
      name := pipename
   __PIPE_GA_ := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0)
   __PIPE_    := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0)
   if (__PIPE_=-1 or __PIPE_GA_=-1)
      Return 0
	 If A_IsCompiled || (A_IsDll && DllCall(A_AhkPath "\ahkgetvar","Str","A_IsCompiled")) ; allow compiled executable to execute dynamic scripts. Requires AHK_H
		Run, % """" A_AhkPath """" (params?" ":"") params " /E ""\\.\pipe\" name """",,UseErrorLevel HIDE, PID
	 else
		Run, % """" A_AhkPath """" (params?" ":"") params " ""\\.\pipe\" name """",,UseErrorLevel HIDE, PID
   If ErrorLevel
      MsgBox, 262144, ERROR,% "Could not open file:`n" __AHK_EXE_ """\\.\pipe\" name """"
   DllCall("ConnectNamedPipe",@,__PIPE_GA_,@,0)
   DllCall("CloseHandle",@,__PIPE_GA_)
   DllCall("ConnectNamedPipe",@,__PIPE_,@,0)
   script := (A_IsUnicode ? chr(0xfeff) : (chr(239) . chr(187) . chr(191))) TempScript
   if !DllCall("WriteFile",@,__PIPE_,"str",script,_,(StrLen(script)+1)*(A_IsUnicode ? 2 : 1),_ "*",0,@,0)
      Return A_LastError,DllCall("CloseHandle",@,__PIPE_)
   DllCall("CloseHandle",@,__PIPE_)
   Return PID
}


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

Ah! Great information! Thank you so much for explaining DynaRun. I just took the time to read about pipes and that greatly helped me understand what is going on and why CreateScript can be of help with compiled scripts.

 

In answer to your question, your script works without a problem in 32w version. The problem is specific to 64w version. I apologize for not specifying that before (I updated my signature to specify this). So the problem is likely due to pointer size?


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 try using this AhkDllThreadAhkDllThread_IsH() was not 64-bit compatible. (I have also updated AhkDllThread thread)

#include <_MemoryLibrary>
AhkDllThread_IsH(){ ; FileGetVersionInfo Written by SKAN modified by HotKeyIt www.autohotkey.com/forum/viewtopic.php?p=233188#233188
 Static HexVal:="msvcrt\s" (A_IsUnicode?"w":"") "printf",AHK:=A_AhkPath?A_AhkPath:A_ScriptFullPath
 If ((FSz:=DllCall("Version\GetFileVersionInfoSize","Str",AHK,"UInt",0)) && VarSetCapacity(FVI,FSz,0) && VarSetCapacity(Trans,8*(A_IsUnicode?2:1)))
  && DllCall("Version\GetFileVersionInfo","Str",AHK,"Int",0,"UInt",FSz,"PTR",&FVI) 
  && DllCall("Version\VerQueryValue","PTR",&FVI,"Str","\VarFileInfo\Translation","PTR*",Translation,"UInt",0)
  && DllCall(HexVal,"Str",Trans,"Str","`%08X","UInt",NumGet(Translation+0))
  && DllCall("Version\VerQueryValue","PTR",&FVI,"Str","\StringFileInfo\" SubStr(Trans,-3) SubStr(Trans,1,4) "\InternalName","PTR*",InfoPtr,"UInt",0)
    Return StrGet(InfoPtr+0,DllCall("lstrlen" (A_IsUnicode?"W":"A"),"PTR",InfoPtr)) = "AutoHotkey_H"
				|| StrGet(InfoPtr+0,DllCall("lstrlen" (A_IsUnicode?"W":"A"),"PTR",InfoPtr)) = " "
}
AhkDllThread(dll="AutoHotkey.dll",obj=0){
  static init,ahkfunction,hLib,dllptr,libScript,ahkexec,DynaCall:="DynaCall", MemoryLoadLibrary:="MemoryLoadLibrary",MemoryFreeLibrary:="MemoryFreeLibrary"
      ,ResourceLoadLibrary:="ResourceLoadLibrary", MemoryGetProcAddress:="MemoryGetProcAddress"
  static AHK_H:=AhkDllThread_IsH()
  static base:={__Delete:"AhkDllThread"}
  static functions :="
(Join
ahkFunction:s=ssssssssss|ahkPostFunction:i=ssssssssss|
ahkdll:ut=sss|ahktextdll:ut=sss|ahkReady:|ahkReload:i|
ahkTerminate:i|addFile:ut=sucuc|addScript:ut=si|ahkExec:ui=s|
ahkassign:ui=ss|ahkExecuteLine:ut=utuiui|ahkFindFunc:ut=s|
ahkFindLabel:ut=s|ahkgetvar:s=sui|ahkLabel:ui=sui|ahkPause:s|ahkIsUnicode:
)"
  static AhkDllThreadfunc :="
(Join`r`n
" (!A_IsCompiled ? "#include <_MemoryLibrary>" : "") "
#Persistent
SetBatchLines,-1
#NoTrayIcon
Return
AhkDllThreadDLL(dll=""AutoHotkey.dll"",obj=0){
  static functions := ""ahkFunction:s=ssssssssss|ahkPostFunction:i=ssssssssss|""
              . ""ahkdll:ut=sss|ahktextdll:ut=sss|ahkReady:|ahkReload:i|""
              . ""ahkTerminate:i|addFile:ut=sucuc|addScript:ut=si|ahkExec:ui=s|""
              . ""ahkassign:ui=ss|ahkExecuteLine:ut=utuiui|ahkFindFunc:ui=s|""
              . ""ahkFindLabel:ui=s|ahkgetvar:s=sui|ahkLabel:ui=sui|ahkPause:s""
  static object
  If (dll=""0""){
    object:=""""
    return
  } else If (!FileExist(dll) && obj=0){
    MsgBox File: `%dll`% does not exist`, provide correct path for AutoHotkey.dll
    ExitApp
  }
  object:={"""":new _MemoryLibrary(obj?obj:dll)}
  Loop,Parse,functions,|
  {
    StringSplit,v,A_LoopField,:
    object[map="""" ? v1 : !InStr(map,v1) ? v1 : SubStr(map,InStr(map,v1)+StrLen(v1)+1,InStr(map,A_Space,0,InStr(map,v1)))]:=DynaCall(object[""""].GetProcAddress(v1),v2)
  }
  object.base:=Object(" (&base) ")
  return &object
}
)"
  If IsObject(dll){
    dll.ahkterminate()
    If !AHK_H {
      dll[""].Free(),dll:=""
    } else %MemoryFreeLibrary%(dll[""])
    return
  } else if (!FileExist(dll) && !A_IsCompiled){
    MsgBox File: %dll%`ndoes not exist`, provide correct path for AutoHotkey.dll
    ExitApp
  }
  If !AHK_H{
    If (init || ((hLib:=new _MemoryLibrary(A_IsCompiled?(dllptr:=DllCall("LockResource","ptr",DllCall("LoadResource","ptr",lib,"ptr",DllCall("FindResource","ptr",lib:=DllCall("GetModuleHandle","ptr",0,"ptr"),"str",dll,"ptr",10,"ptr"),"ptr"),"ptr")):dll)) && (Init:=hLib.GetProcAddress("ahktextdll")) && (!A_IsCompiled||LibScript:=(!(res:=DllCall("FindResource","ptr",lib:=DllCall("GetModuleHandle","ptr",0,"ptr"),"str",">AUTOHOTKEY SCRIPT<","ptr",10,"ptr"))?(res:=DllCall("FindResource","ptr",lib,"str",">AHK WITH ICON<","ptr",10,"ptr")):res)?StrGet(DllCall("LockResource","ptr",DllCall("LoadResource","ptr",lib,"ptr",res,"ptr"),"ptr"),DllCall("SizeofResource","ptr",lib,"ptr",res,"uint"),"UTF-8"):""))){
      If (ahkfunction || (DllCall(init,"Str",AhkDllThreadfunc "`n" LibScript,"Str","","Str","","Cdecl UInt") && (ahkfunction:=hLib.GetProcAddress("ahkFunction")) && (ahkExec:=hLib.GetProcAddress("ahkExec")))){
        return Object(0+DllCall(ahkfunction,"Str","AhkDllThreadDLL","Str",dll,"Str",A_IsCompiled?dllptr:"","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str"))
        ;reset internal return memory in autoHotkey.dll and release object
        ,DllCall(ahkfunction,"Str","AhkDllThreadDLL","Str","0","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str")
      } else {
        MsgBox Could not load script in %dll%
        Return 0
      }
    } else {
      MsgBox Could not load %dll%
      Return 0
    }
  }
  object:=IsObject(obj)?obj:{},object[""]:= A_IsCompiled ? %ResourceLoadLibrary%(dll) : %MemoryLoadLibrary%(dll)
  Loop,Parse,functions,|
  {
    StringSplit,v,A_LoopField,:
    object[map="" ? v1 : !InStr(map,v1) ? v1 : SubStr(map,InStr(map,v1)+StrLen(v1)+1,InStr(map,A_Space,0,InStr(map,v1)))]:=%DynaCall%(%MemoryGetProcAddress%(object[""],v1),v2)
  }
  return object,object.base:=base
}


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

Okay. I pasted that script into AhkDllThead.ahk. I tested again, and I am still receiving the error.


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

Really odd, can you upload your stuff as it is for testing since I can't reproduce it.



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

I'll PM you a DL to my stuff

 

Really odd, can you upload your stuff as it is for testing since I can't reproduce it.


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

...FYI, the download for AHK_H includes the old version of DynaRun, so I had to re-replace DynaRun.ahk with the one from this topic

I have now updated all lib functions, thanks for letting me know ;)



gummby8
  • Members
  • 120 posts
  • Last active: Nov 30 2018 06:35 PM
  • Joined: 05 Jul 2009

Is there any way possible to have this script work without having AHK pre installed on a PC?

 

I have a script that I keep on my webserver and I updated it constantly. Normally the users just open the launcher for the script which uses URLdownloadtovar first and then uses that variable to load the script.

 

if the user does not have AHK already installed it fails to run at all.

 

Is there any way to grab the raw text data of a script from a URL and launch it as a script without having AHK pre-installed?



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

Save this file (include DynaRun) as Launcher.ahk, then put AutoHotkey.exe in same folder and rename it to Launcher.exe, then double click Launcher.exe.

UrlDownloadToVar(URL) {
 WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
 WebRequest.Open("GET", URL)
 WebRequest.Send()
 Return WebRequest.ResponseText
}
DynaRun(UrlDownloadToVar("http://..."),"MyScript.ahk")


guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
or just compile the Launcher script into exe and give that to the users?