4

I'm trying to load a DLL written in C# into Inno Setup.

Here is the code:

function Check(version, dir: String): Integer;
external 'Check@{src}\check.dll stdcall';

Then I call it like Check(x,y)

But the DLL couldn't be loaded.

I tried it with stdcall and cdecl.

The check.dll file is along to setup.exe.

Why isn't it working?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user1095428
  • 71
  • 1
  • 4
  • I simply use a batch file (which fires commands in the CMD console) to register my DLLs as I would one by one: @echo off echo Registering DevExpress DLLs %~dp0gacutil.exe /i %~dp0DevExpress.BonusSkins.v12.1.dll %~dp0gacutil.exe /i %~dp0DevExpress.Charts.v12.1.Core.dll So, I place this in the RUN section of the iss script: [Run] Filename:C:\myFolder\RegisterDevExpress.bat" Hope this helps. – Chagbert Jun 09 '15 at 07:31

3 Answers3

6

Use the Unmanaged Exports library 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:

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
4

Take a look at Unmanaged Exports from Robert Giesecke.

1

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.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139