Getting IStream from OleVariant

I use Delphi along with WinHTTP to execute an HTTP request to download some files from the Internet, and I can fulfill the request, but I don’t know how to get IStream from OleVariant, which is returned from ResponseStream, I spent a lot of time searching the Internet, but I can’t figure out how to do this. Here is what I tried:

var
  req: IWinHTTPRequest;
  instream: IStream;
begin
  req := CoWinHTTPRequest.Create;

  req.Open('GET', 'http://google.com', false);
  req.Send('');

  if req.Status <> 200 then
  begin
    ShowMessage('failure'#10 + req.StatusText);

    FreeAndNil(req);

    Application.Terminate;
  end;

  instream := req.ResponseStream as IStream;

  ShowMessage('success');

  FreeAndNil(instream);
  FreeAndNil(req);

end;

But I get an error [DCC Error] main.pas(45): E2015 Operator not applicable to this operand type(line 45 - instream := req.ResponseStream as IStream;).

How to scare IStream from OleVariant?

+3
source share
1 answer

try it

instream := IUnknown(req.ResponseStream) as IStream;

1 FreeAndNil . FreeAndNil . . , , . , :

  FreeAndNil(instream);
  FreeAndNil(req);

Edit2: ,

, , , .

req.ResponseStream OleVariant. as QueryInterface OleVariant.

OleVariant OleVariant IUnknown, OleVariant IUnknown, as QueryInterface IStream.

a OleVariant IStream, OleVariant IStream.

+9

Source: https://habr.com/ru/post/1790824/


All Articles