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

Oha*_*had 4 c# dll inno-setup unmanagedexports

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

C#功能:

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

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;
Run Code Online (Sandbox Code Playgroud)

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

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

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

TLa*_*ama 7

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

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

在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;
Run Code Online (Sandbox Code Playgroud)