从具有非托管导出的C#DLL返回字符串到Inno安装脚本

我有一个C#DLL,它使用Unmanaged Exports公开一个函数,它由Inno Setup Pascal脚本直接调用。 此函数需要将字符串返回给Inno Setup。 我的问题是如何实现这一目标?
我首选的方法是将Inno Setup中的缓冲区传递给C#函数,该函数将返回此缓冲区内的字符串。 我想出了这段代码:

C#function:

[DllExport("Test", CallingConvention = CallingConvention.StdCall)] static int Test([Out, MarshalAs(UnmanagedType.LPWStr)] out string strout) { strout = "teststr"; return strout.Length; } 

Inno安装脚本:

 function Test(var res: String):Integer; external 'Test@files:testdll.dll stdcall'; procedure test1; var Res: String; l: Integer; begin SetLength(Res,256); l := Test(Res); { Uncommenting the following line causes an exception } { SetLength(Res,l); } Log('"Res"'); end; 

当我运行此代码时, Res变量为空(我在日志中看到“”)

如何从此DLL返回一个字符串?

请注意,我使用的是Inno Setup的Unicode版本。 我也不想使用COM调用此函数,也不想在DLL中分配缓冲区并将其返回给Inno Setup。

我建议你使用BSTR类型,它曾经是用于互操作函数调用的数据类型。 在C#端,您将字符串编组为UnmanagedType.BStr类型,在Inno Setup端,您将使用与BSTR类型兼容的WideString 。 因此,您的代码将更改为此(另请参阅Unmanaged Exports文档的Marshalling sample章节):

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

在Inno Setup方面,使用WideString

 [Code] function Test(out strout: WideString): Integer; external 'Test@files:testdll.dll stdcall'; procedure CallTest; var retval: Integer; str: WideString; begin retval := Test(str); { test retval for success } Log(str); end;