I'm trying to reference a C# DLL in my InnoSetup project. What I need is a simple function with one string parameter and a string return value. But even following the example and trying different kinds of marshaling I always end up in a Access Violation.
This is my C# class:
public class NKToolbox
{
[DllExport("EncryptPassword", CallingConvention.StdCall)]
static string EncryptPassword([MarshalAs(UnmanagedType.LPStr)] string password)
{
File.WriteAllText(@"C:\temp\test.txt", password);
return password.Length.ToString();
}
}
I placed the File.WriteAllText
to see if the method is even called. But no. I Use the UnmanagedExports package from Robert Giesecke.
And the Inno Setup Code:
function EncryptPassword(pw: WideString): WideString;
external 'EncryptPassword@files:nktoolbox.dll stdcall';
function InitializeSetup: Boolean;
var
str: WideString;
begin
str := EncryptPassword('sdvadfva');
log(str);
result := False;
end;
On the line str := EncryptPassword('sdvadfva')
I get a 'Access violation at address ...... Write of address .....' I'm using Inno Setup 5.5.9 Unicode.
I've tried it with different marshaling statements I've found in other threads, I've tried it with the out
keyword, with normal string
type and WideString
hopeless.