How to make AutoHotkey.Interop call a C# function right?

Ask for help, how to use AHK_H, etc.
ntnvsl
Posts: 2
Joined: 23 Jun 2021, 21:33

How to make AutoHotkey.Interop call a C# function right?

23 Jun 2021, 21:54

What's wrong with this code? It actually works, but crashes fatally after several keypresses.

Code: Select all

using System;
using System.Linq;
using System.Windows;
using System.Diagnostics;
using System.Runtime.InteropServices;
using FlaUI.UIA3;
using Application = FlaUI.Core.Application;
using AutoHotkey.Interop;

namespace random
{
  public partial class MainWindow : Window
  {
    delegate void Test();

    public MainWindow()
    {
      var program = Process.GetProcessesByName("notepad").First();
      var app = Application.Attach(program.Id);
      var mainWindow = app.GetMainWindow(new UIA3Automation());

      var ahk = AutoHotkeyEngine.Instance;

      Test rand = () => ahk.ExecRaw($"send {(new Random()).Next(100, 200)}");

      ahk.ExecRaw("#IfWinActive ahk_exe notepad.exe");
      ahk.ExecRaw($"RCtrl::DllCall({Marshal.GetFunctionPointerForDelegate(rand)})");

      InitializeComponent();
    }
  }
}
Untitled.png
Untitled.png (5.09 KiB) Viewed 3785 times
ntnvsl
Posts: 2
Joined: 23 Jun 2021, 21:33

Re: How to make AutoHotkey.Interop call a C# function right?

24 Jun 2021, 17:31

swagfag wrote:
24 Jun 2021, 05:07
is ur C# app 32bit or 64bit?
32bit
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to make AutoHotkey.Interop call a C# function right?

24 Jun 2021, 21:25

it does start a thread https://github.com/amazing-andrew/AutoHotkey.Interop/blob/master/src/AutoHotkey.Interop/AutoHotkeyEngine.cs#L17-L24
this uses a very old binary 1.1.13
it could be a problem with the C# wrapper, or there could be a bug in that ahk version or it could be something else
i suggest trying to load the dll(LoadLibrary, GetProcAddress) and calling the functions manually without the wrapper to see if it crashes. if it does, its probably an ahk bug - try updating to whatever the currently available latest AHK_H v1 version is
burque505
Posts: 1748
Joined: 22 Jan 2017, 19:37

Re: How to make AutoHotkey.Interop call a C# function right?

01 Jul 2021, 07:41

@ntnvsl, the original code on GitHub for AutoHotkey.Interop is missing a reference to the .ahkready method. That'll kill you. I've been there. You can recompile the DLL and get around this. Here's the modified code for AutoHotkeyEngine.cs:
Spoiler
Once you've done this, and assuming your AutoHotkeyEngine.Instance is named 'AHK', you now have Ahk.IsReady() available. You'll still probably need to get rid of the created AutoHotkeyEngine.Instance when finished or it WILL hang around and crash unpredictably.
My mods to your code (can't test):
Spoiler
I hope this helps, I know it's like vanishing down a rabbit hole. I've cobbled together plugins for several RPA platforms using AutoHotkey.Interop in one form or another, and I always get bit by where and when I need to call .IsReady(), and by instances of the engine hanging around. (You can also try sharpAHK which exists in various forms on the web. You could also download my mod of it from nuget here to see if that works.)

Hope this is more good than harm :headwall:
Regards,
burque505

Return to “Ask for Help”

Who is online

Users browsing this forum: No registered users and 6 guests