C#字符串到Inno安装程序

时间:2015-02-12 15:07:25

标签: c# interop marshalling inno-setup

我有一个C#DLL,其中公开了一个生成字符串的方法 我想从Inno Setup调用此方法然后接收字符串。

function GetInformationEx():String;
external 'GetInformationEx@{src}\data\tools\ZipLib.dll stdcall loadwithalteredsearchpath';

procedure ShowProgress(progress:Integer);
var 
    information : String;
begin
    WriteDebugString('ShowProgress called');
    if(progress > pbStateZip.position) then 
    begin
        pbStateZip.position := progress;
        lblState2.Caption  := IntToStr(progress)+' %';
        try
            information := GetInformationEx();
        except
            ShowExceptionMessage;
        end;
        //Do something with the information
    end
    if(progress >= 100)then 
    begin
        KillTimer(0,m_timer_ID);
        //Inform that the extraction is done
    end
    WriteDebugString('ShowProgress leave');
end;

这是我简单的C#部分

[DllExport("GetInformationEx", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
public static String GetInformationEx()
{
    return "Some simple text message || or heavy information"; 
}

我的问题是:
我必须将哪种类型发送回Inno Setup,以便Inno Setup能够正确处理它?

直到现在我收到此消息

enter image description here

PS:我读过这篇文章:
Returning a string from a C# DLL with Unmanaged Exports to Inno Setup script
但我希望C#代码负责字符串。

1 个答案:

答案 0 :(得分:2)

.NET String类型肯定不会封送到Pascal string类型。 .NET对Pascal类型一无所知。

.NET可以将字符串编组为字符数组(而Pascal可以编组字符数组中的字符串)。但是当字符串是函数的返回类型时,为字符串分配内存存在问题(谁分配内存,谁释放它)。

这就是the solution in question you pointed to建议你使用ref / out参数的原因,因为这样调用者可以提供一个缓冲区,.NET可以将字符串封送到。所以分配没有问题。