Calling custom C#/.Net DLL in Autohotkey
autohotkey, c#, dll
Solution
Use the NuGet package UnmanagedExports to mark the method as callable from a non-.Net external program. Use DllCall inside AHK to call the function.
Problem
I think I'm in over my head here, but wondering if anyone can point me in the right direction. I've created a C# class library (dll) in Visual Studio 2010 to interact with a MS SQL server. It works fine when I call it from another C# program. However, when I try and call it from an AHK script I get the "Error level = -4" indicating the function can't be found. Here's my C# code: ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; namespace AHK_Interface { public class AHK_Interface { public string TrackUsage() { try { SqlConnection ahk_connection = new SqlConnection("Data Source=SQLServer;Initial Catalog=AHK;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"); SqlCommand cmd; ahk_connection.Open(); cmd = new SqlCommand("INSERT INTO AHK_USAGE(username,script_version,notes) VALUES ('TEST user','1.01','TEST NOTES')",ahk_connection); cmd.Connection = ahk_connection; cmd.ExecuteNonQuery(); string success_ind = "success!"; ahk_connection.Close(); return success_ind; } catch (Exception e) { string success_ind = e.Message; return success_ind; } } } } ``` I went into regasm.exe and registered the DLL successfully. Here's my Autohotkey code where I'm trying to call it. All this method does is perform and insert statement and return a success/no success string so I didn't think I'd need to pass any parameters to it. ``` SetWorkingDir %A_ScriptDir% DllCall("LoadLibrary", "str", "AHK_Interface.dll") msgbox %ErrorLevel% ;good at this point success_ind := DllCall("AHK_Interface\TrackUsage") ;trying to call my method "TrackUsage" above MsgBox, %success_ind% %ErrorLevel% ;gives error level of -4 here ExitApp ```