Help with AutoHotkey.DLL and StrReplace or StringReplace

Ask for help, how to use AHK_H, etc.
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Help with AutoHotkey.DLL and StrReplace or StringReplace

Post by burque505 » 19 Jul 2021, 14:25

Hello all,
I cannot get either StrReplace() or StringReplace to work with AutoHotkey.dll. The code below doesn't throw an error for the ahktextdll call, but yields "292" instead of the desired result. (I've tried lots of other syntax - suffice it to say I can't get anything to work.)

Code: Select all

#Persistent
ahk := ComObjCreate("AutoHotkey.Script")

haystack := "Hello World"
needle := "World"

code = 
(
global out
out := StrReplace("You are a primate", " a ", " an insignificant ")
)

out := ahk.ahktextdll("strreplace(""You are a primate"", ""primate"", ""fat monkey"")")
while (ahk.ahkready())
	sleep 100
ahk.ahkterminate()
msgbox %out% ; Shows "292"


out := StrReplace("Fat monkeys are not everywhere", " not", " most certainly")
msgbox %out% ; Shows "Fat monkeys are most certainly everywhere"

StringReplace, outagain, haystack, %needle%
msgbox %outagain% ; Shows "Hello"
ExitApp
Thanks in advance,
burque505

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

Re: Help with AutoHotkey.DLL and StrReplace or StringReplace

Post by HotKeyIt » 19 Jul 2021, 17:30

You need #Persistent.
Also ahktextdll returns thread handle!

Code: Select all

hThread := ahk.ahktextdll("#Persistent`nout:=strreplace(""You are a primate"", ""primate"", ""fat monkey"")")
While !out:=ahk.ahkgetvar("out")
  Sleep 100
ahk.ahkterminate()
MsgBox %out%

burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Help with AutoHotkey.DLL and StrReplace or StringReplace

Post by burque505 » 19 Jul 2021, 18:06

Thank you very much, @HotKeyIt, that clears up two questions for me. Extremely helpful.
Regards,
burque505

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

Re: Help with AutoHotkey.DLL and StrReplace or StringReplace

Post by HotKeyIt » 19 Jul 2021, 18:27

Np, btw. you can do also this:

Code: Select all

ahk:=AhkThread()
hThread := ahk.ahktextdll("Alias(out," getvar(out) ")`nout:=strreplace(""You are a primate"", ""primate"", ""fat monkey"")")
While ahk.AhkReady()
  Sleep 100
MsgBox %out%

burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Help with AutoHotkey.DLL and StrReplace or StringReplace

Post by burque505 » 19 Jul 2021, 18:39

Thanks again, @HotKeyIt, I'll give it a try.
Edit: That code is working for me if I call it with AutoHotkey_H directly, i.e. not loading the DLL.
Regards,
burque505

Post Reply

Return to “Ask for Help”