Setting and using AutoHotkey DLL variables in Java

Ask for help, how to use AHK_H, etc.
ORYLY
Posts: 2
Joined: 11 May 2021, 03:14

Setting and using AutoHotkey DLL variables in Java

Post by ORYLY » 11 May 2021, 03:35

I'm using Java JNI to interface with AutoHotkey V2 DLL and I'm having trouble initializing, reading, and using variables as output variables in functions. Basically:
  • ahkassign is not returning a -1 or 0 status code to indicate success/failure.
  • I can't tell if ahkgetvar is reading from an initialized variable or hitting an error if the variable I'm trying to read does not exist.
To keep the demo short, I didn't bother setting the other output variables yet. If variable initialization was working, I'd expect to get new warnings on the other output variables and not just the first one.

Code: Select all

  public interface AutoHotkeyDllLibrary extends Library {
    public Pointer ahktextdll(WString script, WString parameters, WString title);
    public Pointer ahkassign(WString varname, WString value);
    public Pointer ahkgetvar(WString varname, WString getpointer);
    public Pointer ahkFunction(WString f, WString p1, WString p2, WString p3, WString p4, WString p5, WString p6, WString p7, WString p8, WString p9, WString p10);

    public Pointer ahkExec(WString s);
  }

  public static void main(final String[] args) {
    // ### Init ###
    final AutoHotkeyDllLibrary ahkLib = Native.load("AutoHotkey.dll", AutoHotkeyDllLibrary.class);
    final Pointer newAhkThread = ahkLib.ahktextdll(null, null, null);
    Objects.requireNonNull(newAhkThread);

    // ### Try to initialize and get a variable using the dll's methods ###
    final Pointer assignResult = ahkLib.ahkassign(new WString("varX"), new WString("123")); // also tried "\"123\"" and got the same results
    // commented out because the returned pointer is null.
    // but the documentation of ahkassign says it should return -1 on failure and 0 on success.
//    Objects.requireNonNull(assignResult);

    final Pointer getResult1 = ahkLib.ahkgetvar(new WString("varX"), new WString("1"));
    // this returns some random number. there is no warning or error
    System.out.println(getResult1.getWideString(0));

    final Pointer getResult0 = ahkLib.ahkgetvar(new WString("varX"), new WString("0"));
    // this returns some random number. there is no warning or error
    System.out.println(getResult0.getWideString(0));

    // ### Try to initialize and get a variable using ahkExec ###
    // commented out because this stops the succeeding ahkFunction call from running
//    ahkLib.ahkExec(new WString("varX := \"123\""));

    // ### Hit an error when trying to use an output variable in a function call ###
    // the function call emits an AutoHotkey warning: "Parameter#1 of WinGetClientPos must be a variable."
    ahkLib.ahkFunction(
      new WString("WinGetClientPos"),
      new WString("varX"), new WString("varY"), new WString("varW"), new WString("varH"), new WString("ahk_exe notepad.exe"),
      null, null, null, null, null
    );
  }

ORYLY
Posts: 2
Joined: 11 May 2021, 03:14

Re: Setting and using AutoHotkey DLL variables in Java

Post by ORYLY » 24 May 2021, 21:34

Okay, I think I've worked out what's happening:
  • ahkassign really does work and is returning a success code of zero. JNA returns a null Pointer because a literal zero is the null pointer.
  • Output variables (and AHK variables in general) cannot be passed to function calls via ahkFunction. This is because the function signature of ahkFunction expects only string literals as parameter values. To reference variables in AHK, there's no choice but to use ahkExec.
  • The AutoHotkey V2 DLL is crashy (on my machine) after calling ahkExec (for example, calling two MsgBox in a row crashes the program). I don't encounter this in the V1 DLL.

jimsweb
Posts: 4
Joined: 11 May 2022, 01:00

Re: Setting and using AutoHotkey DLL variables in Java

Post by jimsweb » 11 May 2022, 11:52

i'm trying to implement a similar functionality via delphi - would you mind posting your code?

Post Reply

Return to “Ask for Help”