Getting an IStream from an OleVariant

后端 未结 1 1581
暗喜
暗喜 2021-01-20 15:09

I am using Delphi along with WinHTTP to do an HTTP request to download some files from the internet, and I can do the request but I don\'t know how to get the IStream from t

相关标签:
1条回答
  • 2021-01-20 15:36

    Try this

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

    Edit 1 You must not call FreeAndNil on an interface. FreeAndNil can only be passed an object instance. Failure to do so results in an exception. Since interfaces are reference counted anyway you can simply let them go out of scope and they will be cleaned up. So, you need to remove:

      FreeAndNil(instream);
      FreeAndNil(req);
    

    Edit2: A try to explain what is going on

    Please feel free to edit or complement if you think this is not accurate or if it can be explained better.

    req.ResponseStream is an OleVariant. The as keyword is doing a call to QueryInterface and that is not implemented by OleVariant.

    OleVariant has a built in type conversion from OleVariant to IUnknown so you need to first cast the OleVariant to IUnknown and then use the as operator to do a QueryInterface in order to get the IStream interface.

    You can not cast a OleVariant directly to a IStream because there is no built in type conversion from OleVariant to IStream.

    0 讨论(0)
提交回复
热议问题