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

前端 未结 1 1746
傲寒
傲寒 2020-11-29 12:33

I have a C# DLL which exposes a function using Unmanaged Exports which is called directly by an Inno Setup Pascal script. This function needs to return a string to Inno Setu

相关标签:
1条回答
  • 2020-11-29 13:20

    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;
    
    0 讨论(0)
提交回复
热议问题