如何在InstallScript中从.NET dll调用方法

时间:2021-11-28 22:39:27

I need to call function from .NET dll in InstallScript. How can I do it?

我需要在InstallScript中调用.NET dll中的函数。我该怎么做?

Let's start from simple HelloWorld. Suppose, I created simple class library TestLibrary.dll

让我们从简单的HelloWorld开始吧。假设,我创建了简单的类库TestLibrary.dll

using System;
using System.Windows.Forms;

namespace TestLibrary
{
    public static class TestClass
    {
        public static void TestFunction()
        {
            MessageBox.Show("Hello!");
        }
    }
 }

I don't want to install this dll on a target box, I only want to run TestFunction() during installation process, so I just added TestLibrary.dll in SupportFiles view (I use InstallShield 2013 Professional, Basic MSI Project Type). Then in InstallScript I'm writing prototype for it, loading TestLibrary.dll and trying to call TestFunction from it. Smth like this:

我不想在目标盒上安装这个dll,我只想在安装过程中运行TestFunction(),所以我只是在SupportFiles视图中添加了TestLibrary.dll(我使用的是InstallShield 2013 Professional,Basic MSI Project Type)。然后在InstallScript中我正在为它编写原型,加载TestLibrary.dll并尝试从中调用TestFunction。 Smth是这样的:

export prototype TestDllFunction(HWND); //call in Custom Action
prototype TestLibrary.TestFunction(); 
.......

function TestDllFunction(hMSI)
    NUMBER Result;
begin
    Result = UseDLL(SUPPORTDIR ^ "TestLibrary.dll");
    TestLibrary.TestFunction();
    Result = UnUseDLL("TestLibrary.dll");
end;

I have 2 problems here: UseDLL returns 0 (0 means that dll was successfully loaded) only if I invoke UseDLL with hardcoded absolute path to TestLibrary.dll. And the second problem - suppose, I successfully loaded dll. How can I invoke my TestFunction and see a "Hello" messagebox then?

我有2个问题:只有当我使用硬编码的绝对路径TestLibrary.dll调用UseDLL时,UseDLL才返回0(0表示dll已成功加载)。第二个问题 - 假设,我成功加载了DLL。如何调用我的TestFunction并查看“Hello”消息框呢?

2 个解决方案

#1


1  

UseDLL only works for unmanaged code. For .NET use DotNetCoCreateObject. But to be honest, for MSI projects I'd skip InstallScript completely and use C# directly. Windows Installer XML (WiX) has a feature called Deployment Tools Foundation (DTF) which makes it possible to build a Windows Installer compatible managed custom action. The output DLL looks like a traditional Win32 DLL to Windows Installer and is compatible with InstallShield.

UseDLL仅适用于非托管代码。对于.NET,请使用DotNetCoCreateObject。但说实话,对于MSI项目,我会完全跳过InstallScript并直接使用C#。 Windows Installer XML(WiX)具有一个名为Deployment Tools Foundation(DTF)的功能,可以构建与Windows Installer兼容的托管自定义操作。输出DLL看起来像Windows Installer的传统Win32 DLL,并与InstallShield兼容。

#2


1  

If you have written a C# code for your .dll, it is not recommended to use install script, instead create a "new managed code" with stored in binary table by right click on Custom Actions.

如果您为.dll编写了C#代码,则不建议使用安装脚本,而是通过右键单击“自定义操作”创建存储在二进制表中的“新托管代码”。

In Assembly File - specify the .dll file or.exe which you want to use in this CA.

在程序集文件中 - 指定要在此CA中使用的.dll文件或.exe。

In Method Signature - Click on ellipses button and select the class name, method name and parameter used in your dll. In value of the parameter you can also select your property name.And in return property you can specify a property which stores and displays the return value from your .dll. Click ok.

在方法签名中 - 单击省略号按钮,然后选择您的DLL中使用的类名,方法名和参数。在参数值中,您还可以选择属性名称。在返回属性中,您可以指定一个属性,用于存储和显示.dll的返回值。点击确定。

Now you can call this custom action wherever required. This will invoke the function of your .dll and solve your problem.

现在,您可以在需要时调用此自定义操作。这将调用.dll的功能并解决您的问题。

#1


1  

UseDLL only works for unmanaged code. For .NET use DotNetCoCreateObject. But to be honest, for MSI projects I'd skip InstallScript completely and use C# directly. Windows Installer XML (WiX) has a feature called Deployment Tools Foundation (DTF) which makes it possible to build a Windows Installer compatible managed custom action. The output DLL looks like a traditional Win32 DLL to Windows Installer and is compatible with InstallShield.

UseDLL仅适用于非托管代码。对于.NET,请使用DotNetCoCreateObject。但说实话,对于MSI项目,我会完全跳过InstallScript并直接使用C#。 Windows Installer XML(WiX)具有一个名为Deployment Tools Foundation(DTF)的功能,可以构建与Windows Installer兼容的托管自定义操作。输出DLL看起来像Windows Installer的传统Win32 DLL,并与InstallShield兼容。

#2


1  

If you have written a C# code for your .dll, it is not recommended to use install script, instead create a "new managed code" with stored in binary table by right click on Custom Actions.

如果您为.dll编写了C#代码,则不建议使用安装脚本,而是通过右键单击“自定义操作”创建存储在二进制表中的“新托管代码”。

In Assembly File - specify the .dll file or.exe which you want to use in this CA.

在程序集文件中 - 指定要在此CA中使用的.dll文件或.exe。

In Method Signature - Click on ellipses button and select the class name, method name and parameter used in your dll. In value of the parameter you can also select your property name.And in return property you can specify a property which stores and displays the return value from your .dll. Click ok.

在方法签名中 - 单击省略号按钮,然后选择您的DLL中使用的类名,方法名和参数。在参数值中,您还可以选择属性名称。在返回属性中,您可以指定一个属性,用于存储和显示.dll的返回值。点击确定。

Now you can call this custom action wherever required. This will invoke the function of your .dll and solve your problem.

现在,您可以在需要时调用此自定义操作。这将调用.dll的功能并解决您的问题。