gpt4 book ai didi

c# - 在 Autohotkey 中调用自定义 C#/.Net DLL

转载 作者:行者123 更新时间:2023-11-30 13:46:36 25 4
gpt4 key购买 nike

我想我在这里不知所措,但想知道是否有人可以指出我正确的方向。我在 Visual Studio 2010 中创建了一个 C# 类库 (dll) 来与 MS SQL 服务器交互。当我从另一个 C# 程序调用它时,它工作正常。但是,当我尝试从 AHK 脚本调用它时,我得到“错误级别 = -4”,指示找不到该函数。

这是我的 C# 代码:

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;
}
}
}
}

我进入 regasm.exe 并成功注册了 DLL。

这是我尝试调用的 Autohotkey 代码。这个方法所做的只是执行和插入语句并返回一个成功/不成功的字符串,所以我认为我不需要向它传递任何参数。

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

最佳答案

使用 NuGet 包 UnmanagedExports将该方法标记为可从非 .Net 外部程序调用。使用 AHK 内部的 DllCall 调用函数。

关于c# - 在 Autohotkey 中调用自定义 C#/.Net DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20380739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com