Inno Setup-具有依赖项的外部.NET DLL [英] Inno Setup - External .NET DLL with dependencies

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

问题描述

我正在尝试在安装过程中在Inno Setup脚本中使用自定义DLL。我编写了一个非常简单的函数,该函数基本上使用MySQL .NET连接器检查MySQL数据库的连接字符串(目标服务器上没有MySQL客户端)。此导出函数的代码为:

I am trying to use a custom DLL in a Inno Setup script during installation. I wrote a very simple function that basically checks a connection string for a MySQL database using MySQL .NET connector (there is no MySQL client on the target server). The code of this exported function is:

public class DbChecker
{
    [DllExport("CheckConnexion", CallingConvention.StdCall)]
    public static int CheckConnexion([MarshalAs(UnmanagedType.LPStr)] string connexionString)
    {
        int success;
        try
        {
            MySqlConnection connection = new MySqlConnection(connexionString);
            connection.Open();
            connection.Close();
            success = 0;
        }
        catch (Exception)
        {
            success = 1;
        }
        return success;
    }
}

此函数在Inno Setup中以这种方式导入:

The function is imported this way in Inno Setup :

[Files]
Source: "..\..\MyDll\bin\x86\Release\*"; Flags: dontcopy;

[Code]
function CheckConnexion(connexionString: AnsiString): Integer;
external 'CheckConnexion@files:MyDll.dll,MySql.Data.dll stdcall setuponly loadwithalteredsearchpath';`

问题是安装程序在运行时引发异常:

The problem is that the setup throws an exception at runtime:


运行时错误(于53:207):

Runtime Error (at 53:207):

外部异常E0434352。

External exception E0434352.

我想我必须使用 files 前缀,因为在将文件复制到 {app}之前,该函数在 NextButtonClick 事件处理程序中调用目录。

I think I have to use the files prefix because the function is called in the NextButtonClick event handler, before files are copied to the {app} directory.

MyDll.dll MySql都可以。在运行时将Data.dll 正确提取到 {tmp} 目录。

我尝试使用和不使用 loadwithalteredsearchpath 标志,结果相同。

I tried both with and without the loadwithalteredsearchpath flag with the same result.

我发现这是错误代码是通用的.NET运行时错误代码。

What I found is that this error code is a generic .NET runtime error code.

如果我使用 MySql.Data 删除了该部件,它就可以很好地工作(除了它什么都不做... )

If I remove the part using MySql.Data it works perfectly fine (except that it does nothing...)

如在其他线程上所建议的那样,我一直尝试使用 EventLog 在.NET代码中记录错误和 UnhandledException ,但是即使没有MySQL部分,无论如何(也没有创建日志源),我都有相同的例外。我检查了计算机上的EventLog权限。

As advised on other threads I've been trying to log the error in my .NET code using EventLog and UnhandledException but I have the same exception no matter what (and no log source is created), even without the MySQL part. I checked EventLog permissions on my computer.

似乎当我使用任何其他基本 C#代码时,都会引发异常(无论何时尝试加载其他C#代码) DLL)。

It seems that the exception is thrown as soon as I use anything else that "basic" C# code (whenever I try to load another DLL).

推荐答案

也许有更好的方法,但是这样做可以。

There is probably a better way, but this will do.

实施初始化功能(此处为 Init ),该功能可设置 AppDomain.AssemblyResolve 处理程序,用于在主路径中查找程序集(正在执行)程序集:

Implement an initialization function (Init here) that sets up AppDomain.AssemblyResolve handler that looks for an assembly in the path of the main (executing) assembly:

[DllExport("Init", CallingConvention.StdCall)]
public static void Init()
{
    AppDomain currentDomain = AppDomain.CurrentDomain;
    currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
}

private static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
{
    string location = Assembly.GetExecutingAssembly().Location;
    AssemblyName name = new AssemblyName(args.Name);
    string path = Path.Combine(Path.GetDirectoryName(location), name.Name + ".dll");
    if (File.Exists(path))
    {
        return Assembly.LoadFrom(path);
    }
    return null;
}

将其导入Inno设置:

Import it to the Inno Setup:

procedure Init(); external 'Init@files:MyDll.dll stdcall setuponly';

然后在调用需要依赖项的函数( CheckConnexion )。

And call it before calling the function that needs the dependency (CheckConnexion).

另一个解决方案可能是:

在已编译的可执行文件中嵌入DLL

Another solution might be this:
Embedding DLLs in a compiled executable

顺便说一句,不需要 loadwithalteredsearchpath 标志。它对.NET程序集imo无效。

Btw, no need for the loadwithalteredsearchpath flag. It has no effect on .NET assemblies imo.

这篇关于Inno Setup-具有依赖项的外部.NET DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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