Jump to content

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

[AHK_H(+dll) / AHK_L / v2] AhkDllThread+AhkExported


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

Calling a Class is the fastest way but Label should be fast enough too.

A_AhkLabel:=DllCall("GetProcAddress","PTR",DllCall("GetModuleHandle","PTR"),"AStr","ahkLabel","PTR")
DllCall(A_AhkLabel,"Str","Label","UInt",0)​
return

In AutoHotkey_H dll you can use WinAPI functions directly.

Also consider using DynaCall.

dll:=AhkThread("
(
A_AhkLabel:=GetProcAddress(GetModuleHandle(),""ahkLabel"")
DllCall(A_AhkLabel,""Str"",""Label"",""UInt"",0)​

AhkLabel:=DynaCall(A_AhkPath ""\ahkLabel"",""i==st"")
AhkLabel[""Label""]
return
)")
While dll.ahkReady()
sleep 100
ExitApp
Label:
MsgBox % A_ThisLabel
Return

Small example with calling class and label from AutoHotkey_H:

SetBatchLines,-1
CriticalFun:=CriticalObject(fun)
dll:=AhkThread("
(
exe:=AhkExported()
fun:=CriticalObject(" (&CriticalFun) ")
Loop 100{
QueryPerformanceCounter(getvar(v1:=0))
exe.ahkLabel(""Label"")
QueryPerformanceCounter(getvar(v2:=0))
fun.Call()
QueryPerformanceCounter(getvar(v3:=0))
ToolTip `% ""Label`t`t"" v2-v1 ""``nClass`t`t"" v3-v2
Sleep 200
}
)")
While dll.ahkReady()
	Sleep 100
ExitApp
Esc::ExitApp
Label:
Return
Class Fun {
	Call(){
	}
}

CriticalObject is only required if you call the function from multiple threads, otherwise you can also use (&fun) instead (&CriticalFun)!



think
  • Members
  • 119 posts
  • Last active: Nov 01 2015 11:37 PM
  • Joined: 23 May 2013

Thanks HotKeyIt, this is a lot of information :-)

 

I tried this two alternatives first and it looks both are running much faster and they work compiled too, thanks:

A_AhkLabel:=DllCall(""GetProcAddress"",""PTR"",DllCall(""GetModuleHandle"",""PTR""), ""AStr"",""ahkLabel"",""PTR"")
DllCall(A_AhkLabel,""Str"",""Label"",""UInt"",0)​

; or
                
exe:=AhkExported()
exe.ahkLabel(""Label"")

I will study the other examples too but it will take some time to understand...

 

Two more questions:

- what is the fastest way to get a variable value from dll thread to the main thread - I now use Var:=dll.ahkgetvar("Var")

​- and vice versa what is the best method to get variable value from the main to dll thread?



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

That is the best way as long as it is a string, because ahkgetvar returns strings only :)

Another method is to use Alias but be careful because when variable is resized it changes memory address and can cause crash, therefore use VarSetCapacity(var,1024) or even larger if you variable might hold longer string:

VarSetCapacity(var,1024)
AhkThread("
(
Alias(var," getvar(Var) ")
Loop
	ToolTip % var
return
)")
Loop 100000000
	var:=A_Index
ExitApp
Label:
	MsgBox % A_ThisLabel
Return


think
  • Members
  • 119 posts
  • Last active: Nov 01 2015 11:37 PM
  • Joined: 23 May 2013

Thanks, my Var is a number (for example Var:=1) - is this ok?

 

How about geting a variable value from the main to dll thread?



HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
dll:=AhkThread("
(
var:=1
ready:=1
Loop
  var:=A_Index
return
)")
While !dll.ahkgetvar("ready")
  Sleep 100
Alias(var,dll.ahkgetvar("var",1))
Loop 10000
  ToolTip % var
ExitApp

If var is a number no VarSetCapacity is required.