Get/Send values between AHK-programs - possible?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Get/Send values between AHK-programs - possible?

20 Apr 2020, 07:14

Hi!
Running one AHK program from another AHK program is not difficult.
(like this) Examples of programs! - ProgA.ahk / ProgB.ahk
When ProgA is running as Administrator (yes on the UAC-question) even ProgB is running as Administrator. (Great!)
It is possible to run ProgB without ProgA - No problem. (Great!)

But, is it possible to run ProgA as runs ProgB and use variable values ​​between these programs in some way?
Or is the only way to create an ini-file and manage variable values ​​through it with IniRead / IniWrite?
User avatar
boiler
Posts: 16769
Joined: 21 Dec 2014, 02:44

Re: Get/Send values between AHK-programs - possible?

20 Apr 2020, 07:24

See Example #3 and Example #4 in the documentation for OnMessage() to see how scripts can share data between them.
BNOLI
Posts: 548
Joined: 23 Mar 2020, 03:55

Re: Get/Send values between AHK-programs - possible?

20 Apr 2020, 07:32

AFAIK you could also use TCP/UDP/HTTP (sender/receiver) for communication. Shared files, of course. Calling a remote AHK script from the command line using additional cmd line parameters. OnMessage(), ...
Remember to use [code]CODE[/code]-tags for your multi-line scripts. Stay safe, stay inside, and remember washing your hands for 20 sec !
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Get/Send values between AHK-programs - possible?

20 Apr 2020, 09:22

I think it was very complicated! (even if the AHK programs are on the same computer)
I made a try with two programs and one variable (which almost succeeded)
Program examples of OnMessage
1) The result from ProgB is showed in the function Receive_WM_COPYDATA(wParam, lParam) (ProgA)
Is the only way to get the result value outside the function to make this (CopyOfData) Global?

2) But after OnMessage is introduced into ProgB, ProgB will not be able to run without ProgA (ErrorMessage)
(Does the same restriction as #Include?)
User avatar
boiler
Posts: 16769
Joined: 21 Dec 2014, 02:44

Re: Get/Send values between AHK-programs - possible?

20 Apr 2020, 09:48

Albireo wrote: 1) The result from ProgB is showed in the function Receive_WM_COPYDATA(wParam, lParam) (ProgA)
Is the only way to get the result value outside the function to make this (CopyOfData) Global?
That's a valid way to get access to that value, and it's probably easiest. It's not like you can make it the return value of the function since the function is launched by an event.
Albireo wrote: 2) But after OnMessage is introduced into ProgB, ProgB will not be able to run without ProgA (ErrorMessage)
(Does the same restriction as #Include?)
I'm not understanding the question? Can you elaborate?
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Get/Send values between AHK-programs - possible?

20 Apr 2020, 12:09

1) Receive values ProgA!
boiler wrote:
20 Apr 2020, 09:48
Albireo wrote: 1) The result from ProgB is showed in the function Receive_WM_COPYDATA(wParam, lParam) (ProgA)
Is the only way to get the result value outside the function to make this (CopyOfData) Global?
That's a valid way to get access to that value, and it's probably easiest. It's not like you can make it the return value of the function since the function is launched by an event.
Global CopyOfData is probably the only way to get the result from the function Receive_VM_COPYDATA()

Code: Select all

…
OnMessage(0x4a, "Receive_WM_COPYDATA")  ; 0x4a is WM_COPYDATA
RunWait ProgB.ahk
vFromProgB := CopyOfData
MsgBox 64, Rad %A_LineNumber% -> %A_ScriptName%, Result .: %Result% `nvFromProgB .: %vFromProgB% `n`nProgA - Ends
…
ExitApp

Receive_WM_COPYDATA(wParam, lParam)
{	Global CopyOfData
	StringAddress := NumGet(lParam + 2*A_PtrSize)  ; Retrieves the CopyDataStruct's lpData member.
	CopyOfData := StrGet(StringAddress)  ; Copy the string out of the structure.
	; Show it with ToolTip vs. MsgBox so we can return in a timely fashion:
	ToolTip %A_ScriptName%`nReceived the following string:`n%CopyOfData%
	
	Return True  ; Returning 1 (true) is the traditional way to acknowledge this message.
}
Have not tested how more than one result are transferred from ProgB to ProgA or to send information from ProgA to ProgB


2) Function ProgB
boiler wrote:
20 Apr 2020, 09:48
Albireo wrote: 2) But after OnMessage is introduced into ProgB, ProgB will not be able to run without ProgA (ErrorMessage)
(Does the same restriction as #Include?)
I'm not understanding the question? Can you elaborate?
1) In order to send a result from ProgB to ProgA, ProgB must know that it is ProgA that will get the result.
Like this .: TargetScriptTitle := "ProgA.ahk ahk_class AutoHotkey" (in ProgB)
I don't know of any way in which ProgB knows that it is ProgA that started ProgB or if ProgB is run standalone.
ProgA allways knows ProgB (but ProgB doesn't always know ProgA)
Don't know the possibility of sending information about the file name (ProgA.ahk) from ProgA to ProgB with the instruction OnMessage

2) ProgB should be able to run in many ways eg.
  • From ProgA
  • From ProgC (another program than ProgA)
  • And ProgB (Standalone - without another program)
Summary (Possibly I misunderstood something?)
Once the functions (which OnMessage requires to work) have been installed in ProgB, I cannot run ProgB standalone.
ProgB assumes that ProgA is running. (do not think it is a good solution to erase the error handling)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Get/Send values between AHK-programs - possible?

20 Apr 2020, 12:51

how do find out whether someone's home? u ring the doorbell.

structure ur B in a way such that it isnt dependent on A having been run if noone responds to the handshake
lblb
Posts: 190
Joined: 30 Sep 2013, 11:31

Re: Get/Send values between AHK-programs - possible?

20 Apr 2020, 14:45

Here is a simple strategy from the great dmg that I have used in several of my scripts:

https://autohotkey.com/board/topic/18361-anyway-to-pass-params-to-script-while-running/page-2#entry534166

As they explained:
"It uses a hidden GUI window with G-Labels and the ControlSetText command to send information between scripts. This method is a little more complicated than using a .ini file but it is more versatile and the scripts do not have to repeatedly check a file for changes. If information is sent the receiving script will know immediately because the associated G-Label will be launched."

This and other strategies are mentioned here, along with the talk() library:
https://www.autohotkey.com/boards/viewtopic.php?t=29097
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Get/Send values between AHK-programs - possible?

20 Apr 2020, 18:07

Thanks for all the suggestions!

Some are very cool, but my wishes are quite simple. (I think) One method I got a little more stuck for was Talk().ahk,
but never found the sample file (talk_examples.7z) to which the program refers.

My program structure / wishes are as follows:

ProgB1.ahk Starts Program.exe (through the scheduler) to avoid the UAC query.
ProgB2.ahk Stops Program.exe (through the scheduler) to avoid the UAC query.
(Both of these programs have about 5-600 rows - There are some other AHK-programs that do similar things)
It may take 5-50 seconds to run ProgB1.ahk / ProgB2.ahk - as waiting for Program.exe to start or stop.

Now I was write another program (ProgA) that communicates with ProgB1 or ProgB2 (not at the same time).
But wasn't as simple as I hoped.

If an error occurs in Program.exe, ProgB? gets information about this (but never ProgA)
Although ProgB waits for Program.exe has been started / stopped (with RunWait), RunWait does not work in ProgA (ProgA quits after only a few seconds)

ProgA can do many things, one thing is the following.
  1. Run ProgB2 (Stop Program.exe - if running)
  2. Wait until Program.exe is not running
  3. Modify an ini-file to Program.exe and give the ini-file some new values.
  4. Start Program.exe again
  5. Wait until Program.exe is running (or ProgB1 is closed)
I had wished that any error messages had been transferred from ProgB1 / ProgB2 to ProgA or if there is a "program stop" in ProgB1 / ProgB2, ProgA would also be terminated.
But above all, make ProgA wait for ProgB1 / ProgB2 to be executed.
(Perhaps, this can I try to keep track of through the task manager from ProgA)
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Get/Send values between AHK-programs - possible?

22 Apr 2020, 11:16

This example with Sender / Receiver works (almost as desired) .:
Description
  • When 1_Sender.ahk is run
  • 1_Receiver.ahk is started
  • Then the message "Success!" is shown from 1_Receiver.ahk.
    (Only works if the instruction Sleep 1000 is included in the 1_Receiver.ahk - I have no idea why.)
  • "PostMessage is done!" is shown from 1_Sender.ahk.
  • If the message "Success!" is closed, a new message is appear "Receiver is done!..." from 1_Receiver.ahk
No problem to only run 1_Receiver.ahk (without 1_Sender.ahk)
Only the message "Receiver is done!..." appear (as my desire)

1_Sender.ahk

Code: Select all

#NoEnv
DetectHiddenWindows, On	; Must have to detect the second windows
#SingleInstance force

WinGet SendPID, PID, %A_ScriptName%

Run 1_Receiver.ahk,,, RecPID
WinWait ahk_pid %RecPID%

PostMessage 123456,,,, ahk_pid %RecPID%	; Message1 in the Receiver

MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, Postmessage is done!
1_Receiver.ahk

Code: Select all

#NoEnv
#Persistent
DetectHiddenWindows, On
#SingleInstance force

OnMessage(123456, "ResFunc")

Sleep 1000

MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, Receiver is done!`n`nSendPID .: %SendPID% `n`nReady to Exit?
ExitApp
Return

ResFunc()
{	MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%,  Success!
}
1) I want to know when the 1_Receiver.ahk is done.
- RunWait doesn't work.

2) My desire is to sending some text / numbers from 1_Sender.ahk to 1_Receiver.ahk (or back from 1_Receiver.ahk)
Would work with clipboard in some cases. Always if 1_Receiver.ahk always was run from 1_Sender.ahk (but it is not always in that way)
BNOLI
Posts: 548
Joined: 23 Mar 2020, 03:55

Re: Get/Send values between AHK-programs - possible?

22 Apr 2020, 12:11

talk_examples.7z (attached), taken from :arrow: here, found using Google's site:search-parameter: site:autohotkey.com talk().ahk talk_samples.7z. Well, it was the first hit :lol:
Not tested.

8-)
Attachments
talk_examples.7z
Samples to be used with AVIs talk().ahk
(1.34 KiB) Downloaded 50 times
Remember to use [code]CODE[/code]-tags for your multi-line scripts. Stay safe, stay inside, and remember washing your hands for 20 sec !

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: JoeWinograd, yabab33299 and 142 guests