Calling .NET DLL in Inno Setup [duplicate]

Use the Unmanaged Exports to export function from a C# assembly, so that it can be called in Inno Setup.

  • Implement a static method in C#
  • Add the Unmanaged Exports NuGet package to your project
  • Set Platform target of your project to x86
  • Add the DllExport attribute to your method
  • If needed, define marshaling for the function arguments (particularly marshaling of string arguments has to be defined).
  • Build
using RGiesecke.DllExport;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

namespace MyNetDll
{
    public class MyFunctions
    {
        [DllExport(CallingConvention = CallingConvention.StdCall)]
        public static bool RegexMatch(
            [MarshalAs(UnmanagedType.LPWStr)]string pattern,
            [MarshalAs(UnmanagedType.LPWStr)]string input)
        {
            return Regex.Match(input, pattern).Success;
        }
    }
}

On Inno Setup side (Unicode version):

[Files]
Source: "MyNetDll.dll"; Flags: dontcopy

[Code]
function RegexMatch(Pattern: string; Input: string): Boolean;
    external 'RegexMatch@files:MyNetDll.dll stdcall';

And now you can use your function:

if RegexMatch('[0-9]+', '123456789') then
begin
  Log('Matched');
end
  else
begin
  Log('Not matched');
end;

See also:

  • Returning a string from a C# DLL with Unmanaged Exports to Inno Setup script
  • Inno Setup Calling DLL with string as parameter
  • Inno Setup - External .NET DLL with dependencies

Take a look at Unmanaged Exports from Robert Giesecke.


I don't think this is possible. Managed DLLs do not export functions directly. Calling DLLs from InnoSetup requires the function to be directly exported.

The problem is the same when trying to use managed DLLs from C++ for example. This can not be done except when using COM, as described here.

You should use a native Win32 DLL.