在 Inno Setup 中调用 .NET DLL [英] Calling .NET DLL in Inno Setup

查看:22
本文介绍了在 Inno Setup 中调用 .NET DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to load a DLL written in C# into Inno Setup.

代码如下:

function Check(version, dir: String): Integer;
external 'Check@{src}check.dll stdcall';

然后我称之为Check(x,y)

但是无法加载 DLL.

But the DLL couldn't be loaded.

我用 stdcallcdecl 试过了.

I tried it with stdcall and cdecl.

check.dll 文件与 setup.exe 一起.

为什么它不起作用?

推荐答案

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

Use the Unmanaged Exports to export function from a C# assembly, so that it can be called in Inno Setup.

  • 在 C# 中实现静态方法
  • 非托管导出 NuGet 包添加到您的项目
  • 将项目的平台目标设置为x86
  • DllExport 属性添加到您的方法中
  • 如果需要,为函数参数定义封送处理(特别是必须定义字符串参数的封送处理).
  • 构建
  • Implement a static method in C#
  • Add the Unmanaged Exports NuGet package to your project
  • Set Platform target of your project to x86
  • Add the DllExport attribute to your method
  • If needed, define marshaling for the function arguments (particularly marshaling of string arguments has to be defined).
  • Build
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;
        }
    }
}

在 Inno Setup 端(Unicode 版本):

On Inno Setup side (Unicode version):

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

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

现在你可以使用你的函数了:

And now you can use your function:

if RegexMatch('[0-9]+', '123456789') then
begin
  Log('Matched');
end
  else
begin
  Log('Not matched');
end;

<小时>

另见:

这篇关于在 Inno Setup 中调用 .NET DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆