How to comunicate ahk.dll thread with main script?

Ask for help, how to use AHK_H, etc.
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

How to comunicate ahk.dll thread with main script?

01 Dec 2016, 09:21

Using AHK_L v2 and AHK.dll v2 I managed to create a thread this way:

Code: Select all

threadCode := "
(com % `
	;thread code here
	
)"

ahkDll := "AutoHotkey.dll"
DllCall("LoadLibrary", str,ahkDll)
DllCall(ahkDll '\ahktextdll', str,threadCode , str,'', 'CDecl')
But now I need a way of sending data from the thread to the main script.
I can't use AHK_H functions like AhkExported and others.
For reason explained in a previous post, I decided to use AHK_L + AHK.dll. But the documentation is not quite clear as to how you communicate threads in this scenario.

Anybody knows?
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: How to comunicate ahk.dll thread with main script?

01 Dec 2016, 11:21

What are you trying to say? That I should copy the code from https://github.com/HotKeyIt/ahkdll/blob ... Thread.ahk and use it in AHK_L?
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: How to comunicate ahk.dll thread with main script?

01 Dec 2016, 11:42

Why not just use AHK_H for the main script, and use the functions within AHK_H to pass data to the child thread?

See this post for an easy way to safely share objects between threads without getting access violation errors.
Bear in mind that if you pass an associative array, you will not be able to enumerate it. You will, however, be able to enumerate indexed arrays that you pass. So if you need to pass an associative array, you can pack it into an indexed array, then pass it to the other thread, then unpack it to an associative array.
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: How to comunicate ahk.dll thread with main script?

01 Dec 2016, 12:08

evilC wrote:See this post for an easy way to safely share objects between threads without getting access violation errors.
I was the OP of that thread.
evilC wrote:Why not just use AHK_H for the main script, and use the functions within AHK_H to pass data to the child thread?
The reason I don't want to use AHK_H is in this thread: https://autohotkey.com/boards/viewtopic ... 03#p118603
Basically, I want a single .exe that does the job.
No installation, no unzipping, no nothing. One double-click and that's it.
That's what I'm used to with AutoHotKey. I expect no less this time.
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: How to comunicate ahk.dll thread with main script?

01 Dec 2016, 12:16

Getfree wrote: Basically, I want a single .exe that does the job.
No installation, no unzipping, no nothing. One double-click and that's it.
That's what I'm used to with AutoHotKey. I expect no less this time.
but if you're using AutoHotkey.dll, then you don't have a single .exe
you now need that .dll file as well
so whats the difference between including the mscvrt100.dll?

Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: How to comunicate ahk.dll thread with main script?

01 Dec 2016, 12:22

I can put this at the top of my file and problem solved.

Code: Select all

FileInstall AutoHotkey.dll, AutoHotkey.dll
FileInstall mscvrt100.dll, mscvrt100.dll
Whereas if I use AHK_H I need either an auxiliary instalation program or a zip package.
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: How to comunicate ahk.dll thread with main script?

01 Dec 2016, 12:58

Getfree wrote:I can put this at the top of my file and problem solved.

Code: Select all

FileInstall AutoHotkey.dll, AutoHotkey.dll
FileInstall mscvrt100.dll, mscvrt100.dll
Whereas if I use AHK_H I need either an auxiliary instalation program or a zip package.
gotcha

Getfree wrote:What are you trying to say? That I should copy the code from https://github.com/HotKeyIt/ahkdll/blob ... Thread.ahk and use it in AHK_L?
yes exactly, then follow the AhkThread() example. did you try it?

if it doesn't work, there is also AhkDllThread() which is a bit older:
https://autohotkey.com/board/topic/5614 ... kexported/

maybe one of them will get you going

HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to comunicate ahk.dll thread with main script?

01 Dec 2016, 18:16

You can also extract AutoHotkey_H exe and run it, that will make it all easier:

Code: Select all

FileInstall AutoHotkey.exe, AutoHotkey.exe
FileInstall mscvrt100.dll, mscvrt100.dll
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: How to comunicate ahk.dll thread with main script?

02 Dec 2016, 11:38

HotKeyIt wrote:You can also extract AutoHotkey_H exe and run it, that will make it all easier:

Code: Select all

FileInstall AutoHotkey.exe, AutoHotkey.exe
FileInstall mscvrt100.dll, mscvrt100.dll
I thought of that as well.
Unfortunately for my specific situation it wouldn't help because the program needs to be part of an I/O pipeline. So I can't just run AHK_H and then close AHK_L (that would break the pipeline).

I only see two possible solutions:
1. Multiple AHK_L processes and inter-process communication
2. AHK_L + AHK.dll and inter-thread communication
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to comunicate ahk.dll thread with main script?

02 Dec 2016, 13:07

Getfree wrote:So I can't just run AHK_H and then close AHK_L (that would break the pipeline).
So don't close ahk-L until you are done :crazy:
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: How to comunicate ahk.dll thread with main script?

02 Dec 2016, 14:32

Helgef wrote:
Getfree wrote:So I can't just run AHK_H and then close AHK_L (that would break the pipeline).
So don't close ahk-L until you are done :crazy:
My project requires two threads of execution. If I do what you suggest I would already have those two threads (the two AHK processes), which defeats the whole purpose of using AHK_H in the first place. It's simpler to have two AHK_L processes (solution number 1 in my previous post)
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: How to comunicate ahk.dll thread with main script?

02 Dec 2016, 19:06

HotKeyIt wrote:Try ObjShare then, you can get the ahk code from here.
Thanks. This was helpful.

I managed to assemble this minimal working example in AHK_L v2:

Code: Select all

#persistent

LresultFromObject(obj){
	static IDispatch
	, set := VarSetCapacity(IDispatch, 16)
	, init := NumPut(0x46000000000000c0, NumPut(0x20400, IDispatch, "int64"), "int64")
	
	return DllCall('Oleacc.dll\LresultFromObject', ptr,&IDispatch, ptr,0, ptr,&obj, ptr)
}

class Test{
	Call(msg){
		tooltip % msg
	}
}

obj := new Test
lresult := LresultFromObject(obj)

threadCode := "
(
	ObjectFromLresult(lresult){
		static IDispatch
		, set := VarSetCapacity(IDispatch, 16)
		, init := NumPut(0x46000000000000c0, NumPut(0x20400, IDispatch, "int64"), "int64")
		
		if DllCall('Oleacc.dll\ObjectFromLresult', ptr,lresult, ptr,&IDispatch, ptr,0, ptr,getvar(com:=0))
			throw "LResult Object could not be created"
		return ComObject(9,com,1)
	}

	obj := ObjectFromLresult(%lresult%)
	loop 5 {
		obj.call(A_index ' Hi' )
		sleep 1000
	}
)"

ahkDll := "AutoHotkey.dll"
DllCall("LoadLibrary", str,ahkDll)
DllCall(ahkDll '\ahktextdll', str,threadCode, str,'', 'CDecl')
But I don't know if this is how it should be done.

Your code on Github calls those DLL functions like they were AHK functions.
I did a search on the repository but there's no mention of ObjectFromLresult or LresultFromObject. :wtf:

Also, I was surprised to find out that the function ObjShare does not exist in AHK.dll, but only in AHK_H, so I had to implement the 'ObjectFromLresult' part inside the thread code.

Anyways, I'm happy to have this working, but still kind of confused by your code on Github.
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: How to comunicate ahk.dll thread with main script?

02 Dec 2016, 19:13

Getfree wrote: Your code on Github calls those DLL functions like they were AHK functions.
I did a search on the repository but there's no mention of ObjectFromLresult or LresultFromObject. :wtf:
another feature of AHK_H is the ability to call Win32 Api calls bulit-in

https://hotkeyit.github.io/v2/docs/commands/WinApi.htm
Getfree wrote: Also, I was surprised to find out that the function ObjShare does not exist in AHK.dll, but only in AHK_H, so I had to implement the 'ObjectFromLresult' part inside the thread code.
no idea

HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to comunicate ahk.dll thread with main script?

02 Dec 2016, 19:38

AHK_H includes ahk functions in resources of exe, those can be also loaded by dll.
You can also include those functions in AutoHotkey.dll, those must be put into resources of dll in LIB, have a look into the exe using ResourceHacker.
You can also amend and use CleanUpAndPack.ahk to include the functions in the dll.

Return to “Ask for Help”

Who is online

Users browsing this forum: No registered users and 21 guests