Bongo
Bongo

Reputation: 3153

C# string to Inno Setup

I have a C# DLL in which exposes a method which generates a string.
I want to call this Method from Inno Setup and receive the string then.

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;

Here is my simple C# part

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

My question is:
What kind of type do I have to send back to Inno Setup so that Inno Setup can handle it correctly?

Until now I get this message

enter image description here

PS: I read this post:
Returning a string from a C# DLL with Unmanaged Exports to Inno Setup script
But I want the C# code to be in charge of the string.

Upvotes: 1

Views: 367

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202474

The .NET String type definitely won't marshal to Pascal string type. The .NET does not know anything about Pascal types.

The .NET can marshal strings to character arrays (and Pascal can marshal string from character arrays). But when the string is a return type of a function, there's problem with allocation of the memory for the string (who allocates the memory, who releases it).

That's why the solution in question you pointed to suggests you to use by ref/out parameter, because this way the caller can provide a buffer the .NET can marshal the string to. So there are no problems with allocation.

Upvotes: 2

Related Questions