在Inno Setup中调用.NET DLL

use*_*428 4 .net dll inno-setup

我正在尝试将用C#编写的DLL加载到Inno Setup中。

这是代码:

function Check(version, dir: String): Integer;
external 'Check@{src}\check.dll stdcall';
Run Code Online (Sandbox Code Playgroud)

然后我这样称呼它 Check(x,y)

但是无法加载DLL。

我尝试了stdcallcdecl

check.dll文件位于setup.exe

为什么不起作用?

Mar*_*ryl 6

使用非托管导出从C#程序集导出函数,以便可以在Inno Setup中调用它。

  • 在C#中实现静态方法
  • 非托管导出 NuGet程序包添加到您的项目
  • 将项目的平台目标设置为x86
  • DllExport属性添加到您的方法
  • 如果需要,请定义函数参数的封送处理(尤其是必须定义字符串参数的封送处理)。
  • 建立
using RGiesecke.DllExport;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

namespace MyNetDll
{
    public class MyFunctions
    {
        [DllExport(CallingConvention = CallingConvention.StdCall)]
        public static bool RegexMatch(
            [MarshalAs(UnmanagedType.LPWStr)]string pattern,
            [MarshalAs(UnmanagedType.LPWStr)]string input)
        {
            return Regex.Match(input, pattern).Success;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在Inno Setup端(Unicode版本):

using RGiesecke.DllExport;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

namespace MyNetDll
{
    public class MyFunctions
    {
        [DllExport(CallingConvention = CallingConvention.StdCall)]
        public static bool RegexMatch(
            [MarshalAs(UnmanagedType.LPWStr)]string pattern,
            [MarshalAs(UnmanagedType.LPWStr)]string input)
        {
            return Regex.Match(input, pattern).Success;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用函数了:

[Files]
Source: "MyNetDll.dll"; Flags: dontcopy

[Code]
function RegexMatch(Pattern: string; Input: string): Boolean;
    external 'RegexMatch@files:MyNetDll.dll stdcall';
Run Code Online (Sandbox Code Playgroud)

也可以看看: