3

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?

RBT
  • 24,161
  • 21
  • 159
  • 240
AndreyS
  • 365
  • 7
  • 18

2 Answers2

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.

Christopher Painter
  • 54,556
  • 6
  • 63
  • 100
  • Christopher, thanks for your answer. It can be useful. But I've already resolved my issue (without installScript) by creating New Managed Code Custom Action where I can specify path to dll and Method Signature to use. Also I can pass some params to the function and set its return value to property. It works fine for me and it is exactly what I was looking for! – AndreyS Feb 04 '14 at 13:23
  • 1
    I really don't recommend this. You tattoo the msiexec sandbox with a CLR version. This sandbox remains for 10 minutes after your installer finishes. If one MSI tattoos say 2.0 quits then you install and need say 4.0 you'll get a BadImageFormat exception. Also you don't have access to the MSI handle so it's hard to do proper logging and data driven custom actions. Don't get me wrong, it was a nice feature when they added it but much better solutions exist today. This is all covered on my blog from around 2006 - 2008 when all of these technologies were released. – Christopher Painter Feb 04 '14 at 14:51
  • @AndreyS : Can you share in details? I am facing the same problem. – Hkachhia Jul 20 '18 at 12:52
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.

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.