AutoHotkey.dll StdLib Compiled Topic is solved

Ask for help, how to use AHK_H, etc.
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

AutoHotkey.dll StdLib Compiled

13 Sep 2016, 21:13

I've got a script that loads up about 9 scripts via AutoHotkey.dll (note: I only wrote this just a few minutes ago). All of these scripts reference my standard library (AHK Install Location\Lib). While in script form, all #includes and not-included function calls properly read-in from the StdLib. If I compile the script, all #includes and function calls fail. I half-expected them to still be read-in from the StdLib, as it was still accessible to them. I can only assume the StdLib path is dictated by the hosting script, and that compiled scripts either remove or alter StdLib (the one I'm using anyway). Even if the libraries or functions are included in the main script, this issue persists, meaning the thread doesn't have access to the main script's after-compilation includes.

My question is: what is happening to the StdLib path when a script is compiled? And why are new threads dictated by it?
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: AutoHotkey.dll StdLib Compiled

14 Sep 2016, 00:52

Did you compile AutoHotkey.dll or AutoHotkey.exe?
You either compile the exe including all what the dlls need and use CreateScript to include library functions in dll.
Or you compile AutoHotkey.dll and include it in your exe.

Code: Select all

dll:=AhkThread("MsgBox `% fun(100)`n" CreateScript("fun{}"))
while dll.ahkReady()
	Sleep 100

fun(var){
	return var*var
}
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: AutoHotkey.dll StdLib Compiled

14 Sep 2016, 01:45

I compiled a _L script (I do not use _H, except through the dll) with AutoHotkey.dll as a resource (FileInstall), extracting the same dll to different paths to accommodate for all scripts. It loops through a directory of scripts and creates a new thread for each of them using ahkdll(). That is, all of the threads are from external scripts not included in the compiled script. I would have included that in the first post, but I wasn't sure it was pertinent.

Code: Select all

#singleInstance force
#persistent
#include <threadMan>
onExit,cleanup

scripts:=[]

loop,files,*.ahk
    scripts.push(a_loopFileFullPath)

for i,a in scripts {
    t%i%:=new threadMan
    t%i%.newFromFile(a)
}
return

cleanup:
for i,a in scripts { 
    t%i%.quit()
    t%i%:=""
}
exitApp
The reason for saving to an array is because I'm planning to expand on it.

The threads themselves work without any error, while this script isn't compiled. If it is compiled, the threads don't have access to the standard library, thus throwing countless applicable errors.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: AutoHotkey.dll StdLib Compiled

14 Sep 2016, 14:40

The scripts don't have access to standard library because they are saved somewhere else, you will need to create a lib.lnk file pointing to your library in the same folder where you extract AutoHotkey.dll.
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: AutoHotkey.dll StdLib Compiled

14 Sep 2016, 14:51

Oh, my mistake, no they aren't FileInstall'd. They are however always copied to the same place, compiled or not. I added a "Lib.lnk" to the same folder they're extracting to (a_temp). No difference.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: AutoHotkey.dll StdLib Compiled

14 Sep 2016, 20:44

Unrelated to your actual question, but it might drive your solution... are you planning to distribute it to other workstations? I'd highly recommend using _MemoryLibrary() from HotKeyIt instead of LoadLibrary to use the dll from resource, then you never have to extract to a_temp to begin with (though your scripts running via autohotkey.dll will still have trouble finding the stdlib so you still need a solution but it won't involve a shortcut in a_temp... if it was me and I was distributing it I would probably fileinstall the needed libraries as well, read them from resource and append them to the script being ran via the .dll, but it wouldn't be very dynamic)

https://autohotkey.com/board/topic/7730 ... rylibrary/

ie:

Code: Select all

0?FileInstall,AutoHotkey.dll,-
Resource_Read(data,"\AutoHotkey.dll")
MemLib:=new _MemoryLibrary(&data)

DllCall(MemLib.GetProcAddress("ahktextdll"),"Str","MsgBox","Str","","Str","")
sleep, 5000
DllCall(MemLib.GetProcAddress("ahkterminate"))
MemLib.Free()
Last edited by gwarble on 15 Sep 2016, 19:20, edited 1 time in total.
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: AutoHotkey.dll StdLib Compiled

14 Sep 2016, 21:23

So this allows multiple threads using one .dll directly from resource? Or just one per .dll still? If it's only one per, this is quite useless for me, but if it's multiple, that would be very useful.

The main script is meant to be dynamic/work on any system for any amount of scripts, where the scripts are added by the user. Therefore, I would want the user to be able to use their scripts with their own standard/user/local libraries. What I posted was just a crude example.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: AutoHotkey.dll StdLib Compiled

15 Sep 2016, 16:55

_MemoryLibrary can be used to load same dll multiple times, same as MemoryLoadLibrary in AHK_H, you should really try using AutoHotkey_H, it will make it much easier to use mult-threading.

With regards to lib.lnk, it needs to be saved in the same location where AutoHotkey.exe is.
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: AutoHotkey.dll StdLib Compiled

15 Sep 2016, 17:38

HotKeyIt wrote:_MemoryLibrary can be used to load same dll multiple times, same as MemoryLoadLibrary in AHK_H, you should really try using AutoHotkey_H, it will make it much easier to use mult-threading.
My preference is _L for now, and for the sake of not confusing myself, I'll only be sticking to one. I'll certainly implement _MemoryLibrary, though; it's exactly what I've been looking for.
HotKeyIt wrote: With regards to lib.lnk, it needs to be saved in the same location where AutoHotkey.exe is.
This doesn't make sense to me. The Lib folder itself is already there. Do you mean the compiled script, like a redirection where the local lib would be?
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: AutoHotkey.dll StdLib Compiled

15 Sep 2016, 19:23

yup you can load the same dll for multiple threads... I also use _L with the dll in this way sometimes

use:

Code: Select all

0?FileInstall,AutoHotkey.dll,-
Resource_Read(dlldata,"AutoHotkey.dll")
MemLib1:=new _MemoryLibrary(&dlldata)
MemLib2:=new _MemoryLibrary(&dlldata)
with:

Code: Select all

Resource_Read(ByRef Var, Name, Type="#10") { 
  VarSetCapacity( Var, 128 ), VarSetCapacity( Var, 0 )
  If ! ( A_IsCompiled ) {
    FileGetSize, nSize, %Name%
    FileRead, Var, *c %Name%
    Return nSize
  }
  If hMod := DllCall( "GetModuleHandle", UInt,0 )
    If hRes := DllCall( "FindResource", UInt,hMod, Str, Name, Str, Type) ;RCDATA = #10
      If hData := DllCall( "LoadResource", UInt,hMod, UInt,hRes )
        If pData := DllCall( "LockResource", UInt,hData )
  Return VarSetCapacity( Var, nSize := DllCall( "SizeofResource", UInt,hMod, UInt,hRes ) )
      ,  DllCall( "RtlMoveMemory", Str,Var, UInt,pData, UInt,nSize )
Return 0    
}
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: AutoHotkey.dll StdLib Compiled

16 Sep 2016, 01:00

Masonjar13 wrote:This doesn't make sense to me. The Lib folder itself is already there. Do you mean the compiled script, like a redirection where the local lib would be?
In that case it should be working, is it working for non compiled scripts?
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: AutoHotkey.dll StdLib Compiled

16 Sep 2016, 12:15

All non-compiled scripts and AutoHotkey.dll threads on non-compiled scripts are able to utilize the standard library located at AutoHotkey_Installation_dir\Lib. When compiled, the compiled script just includes them, but the threads are no longer able to access the standard library.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: AutoHotkey.dll StdLib Compiled

16 Sep 2016, 12:59

Using my example from above, literally any script that contains a function from the standard library. I'm currently rewriting my threadMan object to use _MemoryLibrary, so posting it won't do any good just yet.

Edit: Note: the links to _MemoryLibrary lead to autohotkey.net (Unwanted software/Malware reported site).
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: AutoHotkey.dll StdLib Compiled

16 Sep 2016, 19:03

Masonjar13 wrote:Note: the links to _MemoryLibrary lead to autohotkey.net (Unwanted software/Malware reported site).
Link works fine for me, sounds like a false positive from your antivirus software, anti-malware software, internet browser, firewall, or ISP
Masonjar13 wrote:Using my example from above, literally any script that contains a function from the standard library. I'm currently rewriting my threadMan object to use _MemoryLibrary, so posting it won't do any good just yet.
As far as your stdlib issue, do you not know ahead of time what stdlib functions will be used? The shortcut/.lnk method suggested above does not work for me in this case (ignoring all .dll/_H stuff):

Code: Select all

- Copy my normal installation folder (c:\Programs\Autohotkey\) to somewhere (c:\Programs\AHKTest\)
- Delete "Lib" folder from new copy
- Create shortcut in new folder (c:\Programs\AHKTest\) named "Lib" (or Lib.lnk as are all shortcuts) pointing to original Lib folder (c:\Programs\AutoHotkey\Lib\)
- Tested by creating a simple script that uses a stdlib function, and dragNdropping it onto (c:\Programs\AHKTest\AutoHotkey.exe)
From what I read above it sounds like that would work, but it doesn't (and I haven't read about the shortcut trick before) but maybe HotKeyIt can see whats wrong or clarify the method... Though even if it does work like this, you may have to revert to extracting to A_Temp so you have somewhere to put a shortcut... If its loaded from memory, I'd be curious to see what A_AhkPath returns in your use case since the .dll won't really have a true "path" (or i'll try an example of what you're talking about, but you can probably tell me easier)
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: AutoHotkey.dll StdLib Compiled

16 Sep 2016, 19:14

The link issue is known to HotKeyIt. Whether it's a false positive or not, I'm unsure, but I've been there before without issues, I just click past the warning.

The point is for the user to use this main script/compiled script to run their own scripts all under one process, to reduce clutter in the task manager. This pertains to programs that will always be running at the same time, such as start-up scripts. So, no, it won't know unless it goes through and parses all of the functions in all of the scripts (probably only in memory so it doesn't mess with their scripts), which isn't particularly something I want to do..

Whatever I need to do in terms of linking or anything that isn't crazy (like a parsing system) is alright, as long as it doesn't create too much clutter. As it is, as I've mentioned, it works while not being compiled, which I could deal with, because this system is designed to be for AHK users with AHK installed and their own standard/user libraries. I'm just curious as to why it doesn't work while compiled.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: AutoHotkey.dll StdLib Compiled  Topic is solved

16 Sep 2016, 20:04

if you want to know the reason why its not working i believe its because the script will look in these three locations for a function not defined, but you dont have your stdlib in any of them:
Help File wrote:%A_ScriptDir%\Lib\ ; Local library - requires v1.0.90+.
%A_MyDocuments%\AutoHotkey\Lib\ ; User library.
path-to-the-currently-running-AutoHotkey.exe\Lib\ ; Standard library.
(from one of your script threads, where you send the content of the script to the dll (ie so no file location) what are the values of A_ScriptDir? A_AhkPath?)

I assume your stdlib files are in AutoHotkey-Install-Folder\Lib\, right? When running compiled that location isn't really known to the script
Did you try copying your stdlib to the "User library" %A_MyDocuments%\AutoHotkey\Lib\?
Did you try copying your stdlib to %A_Temp%\Lib\ while you were still extracting there?

If these work then we need clarification from HotKeyIt what the "Lib.lnk" method is to get it working


Also note, I'm not sure if ahk uses A_AhkPath to get the path-to-running-AutoHotkey.exe or not, but:
A_AhkPath
For non-compiled scripts: The full path and name of the EXE file that is actually running the current script. For example: C:\Program Files\AutoHotkey\AutoHotkey.exe
For compiled scripts: The same as the above except the AutoHotkey directory is discovered via the registry entry HKEY_LOCAL_MACHINE\SOFTWARE\AutoHotkey\InstallDir. If there is no such entry, A_AhkPath is blank.
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: AutoHotkey.dll StdLib Compiled

16 Sep 2016, 22:11

gwarble wrote:(from one of your script threads, where you send the content of the script to the dll (ie so no file location) what are the values of A_ScriptDir? A_AhkPath?)
A_ScriptDir is respective to the thread's script, while A_AhkPath is respective to the installed AHK path when in script form. When compiled, A_ScriptDir is the same, and A_AhkPath is that of the compiled main script.
gwarble wrote: I assume your stdlib files are in AutoHotkey-Install-Folder\Lib\, right? When running compiled that location isn't really known to the script
Did you try copying your stdlib to %A_Temp%\Lib\ while you were still extracting there?
As I believe I mentioned, yes, to both of these.
gwarble wrote: Did you try copying your stdlib to the "User library" %A_MyDocuments%\AutoHotkey\Lib\?
I tried this after your mention of it. It has no effect.
gwarble wrote: Also note, I'm not sure if ahk uses A_AhkPath to get the path-to-running-AutoHotkey.exe or not, but:
A_AhkPath
For non-compiled scripts: The full path and name of the EXE file that is actually running the current script. For example: C:\Program Files\AutoHotkey\AutoHotkey.exe
For compiled scripts: The same as the above except the AutoHotkey directory is discovered via the registry entry HKEY_LOCAL_MACHINE\SOFTWARE\AutoHotkey\InstallDir. If there is no such entry, A_AhkPath is blank.
I believe it does.

This is what I've found: While compiled, A_AhkPath in the main compiled script is the path to the installed AutoHotkey.exe. Meanwhile, A_AhkPath in the threads of the main script leads to the path of the main script. That is, putting Lib.lnk in the same directory as the main compiled script, the threads are able to access the standard library. I can have the main script create the link, no big deal. So, mystery solved! It doesn't answer what happens to A_MyDocuments "\AutoHotkey\Lib", but I don't use this and I'm just assuming that many others don't. Local libraries for the threaded scripts also work, which is a big plus as well, although not pertinent to what I'm doing.

Thanks for asking the A_AhkPath question, gwarble, that was quite literally the answer I was looking for. Rather obvious, too. :roll:
And thank you, HotKeyIt, for a lot of stuff, but this time, _MemoryLibrary. It's drastically better than what I was doing previously.

I'll post my threadMan object on my github if anyone is interested.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: AutoHotkey.dll StdLib Compiled

18 Sep 2016, 03:11

%A_MyDocuments%\AutoHotkey\lib works for me.
Lib.lnk must be present in path-to-running-AutoHotkey.exe\lib.lnk, same for Lib folder.
%A_ScriptDir%\Lib\ will work only when using ahkdll, not ahktextdll! For ahktextdll it will be the exe A_ScriptDir, so when compiled it is path-to-running-AutoHotkey.exe.

Return to “Ask for Help”

Who is online

Users browsing this forum: No registered users and 23 guests