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:

Leave a Comment