且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用非托管导出从 C# DLL 返回字符串到 Inno Setup 脚本

更新时间:2023-02-12 16:47:17

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

I would suggest you to use the BSTR type, which is used to be a data type for interop function calls. On your C# side you'd marshall your string as the UnmanagedType.BStr type and on the Inno Setup side you'd use the WideString, which is compatible with the BSTR type. So your code would then change to this (see also the Marshalling sample chapter of the Unmanaged Exports docs):

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

在 Inno Setup 方面,使用 WideString 来实现:

And on the Inno Setup side with the use of WideString to this:

[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;