Page 1 of 1

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

Posted: 23 Jun 2021, 21:54
by ntnvsl
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 3537 times

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

Posted: 24 Jun 2021, 05:07
by swagfag
is ur C# app 32bit or 64bit?

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

Posted: 24 Jun 2021, 17:31
by ntnvsl
swagfag wrote:
24 Jun 2021, 05:07
is ur C# app 32bit or 64bit?
32bit

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

Posted: 24 Jun 2021, 19:09
by HotKeyIt
Do you have any working Example? I assume you have to start a thread first (ahktextdll).
Probably something like this: https://csharp.hotexamples.com/de/examples/-/AutoHotkey.Interop.AutoHotkeyEngine/-/php-autohotkey.interop.autohotkeyengine-class-examples.html

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

Posted: 24 Jun 2021, 21:25
by swagfag
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

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

Posted: 01 Jul 2021, 07:41
by burque505
@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