Using a compiled EXE to call function of other compiled EXE?

Get help with using AutoHotkey and its commands and hotkeys
Joshua W

Using a compiled EXE to call function of other compiled EXE?

07 Jan 2014, 12:09

Hi, I was wondering if there's any way to have write a script so that it can call functions of a separate compiled script, and receive the return value (i.e. to a variable in the first script).

Something like...
---
BrotherPath := 'C:\MyApplicationParts\BrotherScript.exe'
ReturnVar := BrotherPath.MyFunction(Param1, Param2)
---

Is there any way to do what I'm describing? Maybe it can be done with the "Run" command, somehow?
Zelio
Posts: 275
Joined: 30 Sep 2013, 00:45
Location: France

Re: Using a compiled EXE to call function of other compiled

18 Jan 2014, 03:03

There are many ways... and can be complicated, it is not simple as a DLL function...

Maybe the simple way is to call your "BrotherScript.exe" with extra arguments (scriptoption + functionname + param1 + param2 +...) and write your result to a file (written by BrotherScript and will be read by MainScript).
Go to http://ahkscript.org/docs/Scripts.htm (or your manual, into script section) and search something about how Passing Command Line Parameters to a Script... you will call like this CompiledScript.exe [Switches] [Script Parameters] or as an normal application with Run command Run %comspec% /c ""C:\My Utility.exe" "param 1" "second param" >"C:\My File.txt""and you will receive like this

Code: Select all

Loop, %0%  ; For each parameter:
{
    param := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
    MsgBox, 4,, Parameter number %A_Index% is %param%.  Continue?
    IfMsgBox, No
        break
}
Also scripts can speack eachother when they run (both at same time, don't need to call the other script multiple or time, don't need to write on a disk), you can use this example of the manual (WM_COPYDATA, OnMessage section) for have an idea

Code: Select all

; Example: Send a string of any length from one script to another.  This is a working example.
; To use it, save and run both of the following scripts then press Win+Space to show an
; InputBox that will prompt you to type in a string.

; Save the following script as "Receiver.ahk" then launch it:
#SingleInstance
OnMessage(0x4a, "Receive_WM_COPYDATA")  ; 0x4a is WM_COPYDATA
return

Receive_WM_COPYDATA(wParam, lParam)
{
    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.
}

; Save the following script as "Sender.ahk" then launch it.  After that, press the Win+Space hotkey.
TargetScriptTitle = Receiver.ahk ahk_class AutoHotkey

#space::  ; Win+Space hotkey. Press it to show an InputBox for entry of a message string.
InputBox, StringToSend, Send text via WM_COPYDATA, Enter some text to Send:
if ErrorLevel  ; User pressed the Cancel button.
    return
result := Send_WM_COPYDATA(StringToSend, TargetScriptTitle)
if result = FAIL
    MsgBox SendMessage failed. Does the following WinTitle exist?:`n%TargetScriptTitle%
else if result = 0
    MsgBox Message sent but the target window responded with 0, which may mean it ignored it.
return

Send_WM_COPYDATA(ByRef StringToSend, ByRef TargetScriptTitle)  ; ByRef saves a little memory in this case.
; This function sends the specified string to the specified window and returns the reply.
; The reply is 1 if the target window processed the message, or 0 if it ignored it.
{
    VarSetCapacity(CopyDataStruct, 3*A_PtrSize, 0)  ; Set up the structure's memory area.
    ; First set the structure's cbData member to the size of the string, including its zero terminator:
    SizeInBytes := (StrLen(StringToSend) + 1) * (A_IsUnicode ? 2 : 1)
    NumPut(SizeInBytes, CopyDataStruct, A_PtrSize)  ; OS requires that this be done.
    NumPut(&StringToSend, CopyDataStruct, 2*A_PtrSize)  ; Set lpData to point to the string itself.
    Prev_DetectHiddenWindows := A_DetectHiddenWindows
    Prev_TitleMatchMode := A_TitleMatchMode
    DetectHiddenWindows On
    SetTitleMatchMode 2
    SendMessage, 0x4a, 0, &CopyDataStruct,, %TargetScriptTitle%  ; 0x4a is WM_COPYDATA. Must use Send not Post.
    DetectHiddenWindows %Prev_DetectHiddenWindows%  ; Restore original setting for the caller.
    SetTitleMatchMode %Prev_TitleMatchMode%         ; Same.
    return ErrorLevel  ; Return SendMessage's reply back to our caller.
}
Good luck !

Return to “Ask For Help”

Who is online

Users browsing this forum: doncole, HotKeyIt, JohnyWalter, Scryptic, TAC109, Zoanthario and 61 guests