Inno Setup – 具有依赖项的外部.NET DLL

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

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; } } 

该function在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';` 

问题是安装程序在运行时抛出exception:

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

外部exceptionE0434352。

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

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

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

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

如果我使用MySql.Data删除该部分它完全正常(除了它什么也没做……)

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

一旦我使用“基本”C#代码(每当我尝试加载另一个DLL)时,似乎抛出exception。

可能有更好的方法,但这样做。

实现一个初始化函数(这里是Init ),它设置AppDomain.AssemblyResolve处理程序,在主(执行)程序集的路径中查找程序集:

 [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设置:

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

并在调用需要依赖的函数( CheckConnexion )之前调用它。


另一种解决方案可能是:
将DLL嵌入已编译的可执行文件中


顺便说一句,不需要loadwithalteredsearchpath标志。 它对.NET程序集imo没有影响。