3
votes

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

Let's start from simple Hello World. Suppose, I created simple class library 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. Something like this:

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've 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

2 Answers

1
votes

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.

1
votes

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.

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

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.

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