Returning a string from a C# DLL with Unmanaged Exports to Inno Setup script

心不动则不痛 提交于 2019-11-27 09:36:29
TLama

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
}

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!