从OleVariant获取IStream [英] Getting an IStream from an OleVariant
问题描述
ResponseStream
。我花了很多时间去搜索,但是我不知道该怎么做。这是我试过的: var
req:IWinHTTPRequest;
instream:IStream;
begin
req:= CoWinHTTPRequest.Create;
req.Open('GET','http://google.com',false);
req.Send('');
如果req.Status<> 200然后
begin
ShowMessage('failure'#10 + req.StatusText);
FreeAndNil(req);
Application.Terminate;
结束
instream:= req.ResponseStream as IStream;
ShowMessage('success');
FreeAndNil(instream);
FreeAndNil(req);
end;
但是我收到错误 [DCC Error] main.pas(45) :E2015操作符不适用于此操作数类型
(第45行是 instream:= req.ResponseStream as IStream;
)。
如何从OleVariant中剔除IStream?
尝试这个
instream:= IUnknown(req.ResponseStream)as IStream;
编辑1
您不得在界面上调用FreeAndNil 。 FreeAndNil只能传递一个对象实例。否则会导致异常。由于界面是引用计数,所以你可以简单地让它们超出范围,它们将被清理。所以,你需要删除:
FreeAndNil(intream);
FreeAndNil(req);
编辑2:尝试解释发生了什么
如果您认为这不准确或可以更好地解释,请随时进行修改或补充。
req.ResponseStream
是一个 OleVariant
。 as
关键字正在调用 QueryInterface
,而不是由 OleVariant
。
OleVariant
内置类型转换从 OleVariant
到 IUnknown
,所以你需要先将 OleVariant
转换为 IUnknown
,然后使用作为
运算符来执行 QueryInterface
,以获得 IStream
界面。
您不能直接将 OleVariant
一个 IStream
,因为没有从 OleVariant
到 IStream
。
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 the OleVariant that is returned from ResponseStream
. I have spent a lot of time googling but I can't figure out how to do it. Here is what I have 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 the error [DCC Error] main.pas(45): E2015 Operator not applicable to this operand type
(line 45 is instream := req.ResponseStream as IStream;
).
How do I scare the IStream out of an OleVariant?
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
.
这篇关于从OleVariant获取IStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!