IStream.Read() [英] IStream.Read()

查看:104
本文介绍了IStream.Read()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在使用System.Runtime.InteropServices.ComTypes.IStream时遇到问题。


使用Read()的示例方法:


if(IStreamObject!= null)

{

IntPtr readBytes = IntPtr.Zero;

IStreamObject.Read(buffer,size,readBytes);

Result = readBytes.ToInt32();

}


上面的代码将数据从流读入缓冲区。这样做 -

缓冲区充满了有效数据。

问题是读取的字节数(以readBytes为单位)始终为零。


如果我没有读取

字节数,我怎么想知道通话是否失败?


BR

彼得

Hi,

I have a problem using System.Runtime.InteropServices.ComTypes.IStream.

Sample using the Read() method:

if (IStreamObject != null)
{
IntPtr readBytes = IntPtr.Zero;
IStreamObject.Read(buffer, size, readBytes);
Result = readBytes.ToInt32();
}

The above code read data from a stream into a buffer. This works - the
buffer is filled with valid data.
The problem is that the number of bytes read (in readBytes) is always zero.

How am i suppose to know whether the call failed or not, if i dont have the
number of bytes read ?

BR
Peter

推荐答案

彼得,


如果没有返回字节(readBytes参数为零)然后

这很可能意味着你读过流的末尾。在那个

点,你应该停止。


如果由于某种原因调用失败,将抛出一个COMException。如果

除了S_OK之外的任何东西在调用Read时返回为HRESULT,(或实际上是通过COM互操作实现任何方法的
),那么抛出一个COMException

,在ErrorCode属性中返回HRESULT。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Peter Larsen []" < Pe ********* @ newsgroup.nospamwrote in message

news:uj **************** @ TK2MSFTNGP03.phx。 gbl ...
Peter,

If there are no bytes returned (the readBytes parameter is zero) then
that most likely means that you read past the end of the stream. At that
point, you should stop.

If the call fails for some reason, a COMException will be thrown. If
anything other than S_OK is returned as an HRESULT in a call to Read, (or
any method through COM interop, actually), then a COMException is thrown
with the HRESULT returned in the ErrorCode property.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Larsen []" <Pe*********@newsgroup.nospamwrote in message
news:uj****************@TK2MSFTNGP03.phx.gbl...




我在使用System.Runtime.InteropServices.ComTypes.IStream时遇到问题。 br />

使用Read()方法的示例:


if(IStreamObject!= null)

{

IntPtr readBytes = IntPtr.Zero;

IStreamObject.Read(buffer,size,readBytes);

Result = readBytes.ToInt32();

}


上面的代码将数据从流读入缓冲区。这工作 -

缓冲区充满了有效数据。

问题是读取的字节数(以readBytes为单位)总是

零。


我怎么想知道呼叫是否失败,如果我没有

读取的字节数?


BR

彼得
Hi,

I have a problem using System.Runtime.InteropServices.ComTypes.IStream.

Sample using the Read() method:

if (IStreamObject != null)
{
IntPtr readBytes = IntPtr.Zero;
IStreamObject.Read(buffer, size, readBytes);
Result = readBytes.ToInt32();
}

The above code read data from a stream into a buffer. This works - the
buffer is filled with valid data.
The problem is that the number of bytes read (in readBytes) is always
zero.

How am i suppose to know whether the call failed or not, if i dont have
the number of bytes read ?

BR
Peter



5月9日下午2:40, ; Peter Larsen [] < PeterLar ... @ newsgroup.nospam>

写道:
On May 9, 2:40 pm, "Peter Larsen []" <PeterLar...@newsgroup.nospam>
wrote:

我在使用System.Runtime.InteropServices.ComTypes时遇到问题。 IStream。


使用Read()方法的示例:


if(IStreamObject!= null)

{

IntPtr readBytes = IntPtr.Zero;

IStreamObject.Read(buffer,size,readBytes);

Result = readBytes.ToInt32();

}


上面的代码将数据从流读入缓冲区。这样做 -

缓冲区充满了有效数据。

问题是读取的字节数(以readBytes为单位)始终为零。
I have a problem using System.Runtime.InteropServices.ComTypes.IStream.

Sample using the Read() method:

if (IStreamObject != null)
{
IntPtr readBytes = IntPtr.Zero;
IStreamObject.Read(buffer, size, readBytes);
Result = readBytes.ToInt32();
}

The above code read data from a stream into a buffer. This works - the
buffer is filled with valid data.
The problem is that the number of bytes read (in readBytes) is always zero.



我不记得自己和IntPtr做过互操作,但想法是

你把*指针*传给了一个int和该指针内的数据是由Read方法设置的
。你正在IntPtr.Zero中传递,即我不知道
关心读取了多少字节。


试试这个:


不安全

{

int readBytes = 0;

IStreamObject.Read(缓冲区,大小,新的IntPtr(& readBytes));

结果= readBytes;

}


Jon

I don''t recall doing interop with IntPtr myself, but the idea is that
you pass in a *pointer* to an int and the data within that pointer is
set by the Read method. You''re passing in IntPtr.Zero, i.e. "I don''t
care about how many bytes were read".

Try this:

unsafe
{
int readBytes=0;
IStreamObject.Read(buffer, size, new IntPtr(&readBytes));
Result = readBytes;
}

Jon


" Peter Larsen []" < Pe ********* @ newsgroup.nospamwrote in message

news:uj **************** @ TK2MSFTNGP03.phx。 gbl ...
"Peter Larsen []" <Pe*********@newsgroup.nospamwrote in message
news:uj****************@TK2MSFTNGP03.phx.gbl...




我在使用System.Runtime.InteropServices.ComTypes.IStream时遇到问题。 br />

使用Read()方法的示例:


if(IStreamObject!= null)

{

IntPtr readBytes = IntPtr.Zero;

IStreamObject.Read(buffer,size,readBytes);

Result = readBytes.ToInt32();

}


上面的代码将数据从流读入缓冲区。这工作 -

缓冲区充满了有效数据。

问题是读取的字节数(以readBytes为单位)总是

零。


我怎么想知道呼叫是否失败,如果我没有

读取的字节数?


BR

彼得
Hi,

I have a problem using System.Runtime.InteropServices.ComTypes.IStream.

Sample using the Read() method:

if (IStreamObject != null)
{
IntPtr readBytes = IntPtr.Zero;
IStreamObject.Read(buffer, size, readBytes);
Result = readBytes.ToInt32();
}

The above code read data from a stream into a buffer. This works - the
buffer is filled with valid data.
The problem is that the number of bytes read (in readBytes) is always
zero.

How am i suppose to know whether the call failed or not, if i dont have
the number of bytes read ?

BR
Peter



你传递的第三个参数错误,你传递的是null />
指针,而函数需要指向int的指针。

我希望这会引发异常,因为底层的COM方法

应该检查关于有效性的所有指针,当其中一个为空时,返回一个HRESULT =

STG_E_INVALIDPOINTER。

您实际在读的是什么流?


WIlly。

What you are passing as third argument is wrong, you are passing a null
pointer, while the function expects a pointer to an int.
I would expect this to throw an exception, as the underlying COM method
should check all pointers on validity and return an HRESULT =
STG_E_INVALIDPOINTER when one of them is null.
From what stream are you actually reading?

WIlly.


这篇关于IStream.Read()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆