在 Inno Setup 中调用 .NET DLL

编程入门 行业动态 更新时间:2024-10-26 04:20:46
本文介绍了在 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 包添加到您的项目将项目的平台目标设置为x86DllExport 属性添加到您的方法中如果需要,为函数参数定义封送处理(特别是必须定义字符串参数的封送处理).构建 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;

<小时>

另见:

使用非托管导出从 C# DLL 返回字符串到 Inno 安装脚本Inno Setup 使用字符串作为参数调用 DLLInno Setup - 具有依赖项的外部 .NET DLL

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

更多推荐

[db:关键词]

本文发布于:2023-03-31 21:24:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/813741.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Setup   Inno   DLL   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!