Search code examples
cominno-setuppascalscript

Returning BSTR from OLE to Inno Setup


In my setup, created with Inno Setup 5.x I'm using code to perform HTTP request.

This is done by WinHttpRequest COM.

While it's fairly easy to pass string arguments to functions, I'm having trouble receiving them.

For example function GetResponseHeader

HRESULT GetResponseHeader(
  [in]          BSTR Header,
  [out, retval] BSTR *Value
);

takes one IN argument and one OUT argument.

Passing empty string to Value results in Invalid Variant Operation being thrown.

What is the correct type for [out, retval] BSTR *Value on the Pascal side? How can I convert it to string? Should I manually release it?


Solution

  • Actually that kind of method signature is converted to a function. Effectively, the method has this signature:

    function GetResponseHeader(Header: string): string;
    

    So you can do:

    var
      WinHttpReq: Variant;
      ContentType: string;
    begin
      WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
      WinHttpReq.Open('GET', 'https://www.example.com/', False);
      WinHttpReq.Send();
      ContentType := WinHttpReq.GetResponseHeader('Content-Type');
      Log(ContentType);
    end;