Calling a C# DLL and returning a string with Unmanaged Exports from an Inno Setup Script

69 Views Asked by At

I am trying to call a C# dll to modify a json config file for my app during installation.

I have found several posts about this on StackOverflow but they relate to much older versions. Unfortunately none of them worked for me. For the DLL I used the sample C# project that comes with Inno and just added my method. I have not modified the project or solution in any other way apart from adding my method. For the Inno project I created an app from scratch.
Version numbers:
Inno Setup 6.0.3
C#: Framework 4.5

C# snippet:

[DllExport("Test", CallingConvention = CallingConvention.StdCall)]
public static int Test([MarshalAs(UnmanagedType.BStr)] out string strout)
{
      strout = "Hello World!";
      return 0; // indicates success
}

Inno project:

[Files]
Source: "MyDll.dll"; DestDir: "{tmp}"; Flags: dontcopy
Source: "example.json"; DestDir: "{app}"; AfterInstall: CallTest

[Code]
function Test(out strout: WideString): Integer; 
  external '[email protected] stdcall setuponly delayload';

procedure CallTest;
var
  retval: Integer;
  str: WideString;
begin
  MsgBox('Hello from CallTest', mbInformation, MB_OK);
  retval := Test(str);
  MsgBox('Return str is: ' + str, mbInformation, MB_OK);
end;

The Inno compiles correctly. During installation I do get the popup message "Hello from CallTest" but then get the error message "Line 32: Could not call proc". Line 32 is where we call our Test function and we set the return value to the Integer defined earlier. Although we are calling a method (not a procedure) and I looked at the return types from the DLL I cannot figure out why I am getting this error. Any help would be appreciated.

0

There are 0 best solutions below