【问题标题】:Calling .NET DLL in Inno Setup [duplicate]在 Inno Setup 中调用 .NET DLL [重复]
【发布时间】:2019-08-29 13:47:20
【问题描述】:

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

代码如下:

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

那我就叫它Check(x,y)

但无法加载 DLL。

我用stdcallcdecl 尝试过。

check.dll 文件位于setup.exe

为什么它不起作用?

【问题讨论】:

  • 我只是使用一个批处理文件(它会在 CMD 控制台中触发命令)像我一样一个一个地注册我的 DLL:@echo off echo Registering DevExpress DLLs %~dp0gacutil.exe /i %~ dp0DevExpress.BonusSkins.v12.1.dll %~dp0gacutil.exe /i %~dp0DevExpress.Charts.v12.1.Core.dll 所以,我把它放在 iss 脚本的 RUN 部分: [Run] Filename:C: \myFolder\RegisterDevExpress.bat" 希望这会有所帮助。

标签: .net dll inno-setup


【解决方案1】:

使用Unmanaged Exports 从 C# 程序集中导出函数,以便在 Inno Setup 中调用它。

  • 在 C# 中实现静态方法
  • Unmanaged Exports 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;
        }
    }
}

在 Inno Setup 端(Unicode 版本):

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

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

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

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

另见:

【讨论】:

    【解决方案2】:

    看看 Robert Giesecke 的 Unmanaged Exports

    【讨论】:

      【解决方案3】:

      我认为这是不可能的。托管 DLL 不直接导出函数。从 InnoSetup 调用 DLL 需要直接导出函数。

      例如,在尝试使用 C++ 中的托管 DLL 时,问题也是一样的。除非使用 COM,否则无法做到这一点,如 here 所述。

      您应该使用本机 Win32 DLL。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-16
        • 1970-01-01
        相关资源
        最近更新 更多