473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

IStream.Read()

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
May 9 '07 #1
21 6857
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...
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

May 9 '07 #2
On May 9, 2:40 pm, "Peter Larsen []" <PeterLar...@newsgroup.nospam>
wrote:
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.
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

May 9 '07 #3
"Peter Larsen []" <Pe*********@newsgroup.nospamwrote in message
news:uj****************@TK2MSFTNGP03.phx.gbl...
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
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.

May 9 '07 #4
Read allows the last pointer passed in to be null. From the
documentation for IStream from the Platform SDK:

pcbRead
[out] A pointer to a ULONG variable that receives the actual number of bytes
read from the stream object.
You can set this pointer to NULL. In this case, this method does not
return the number of bytes read.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:EE**********************************@microsof t.com...
"Peter Larsen []" <Pe*********@newsgroup.nospamwrote in message
news:uj****************@TK2MSFTNGP03.phx.gbl...
>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

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.

May 9 '07 #5
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:uP**************@TK2MSFTNGP02.phx.gbl...
Read allows the last pointer passed in to be null. From the
documentation for IStream from the Platform SDK:

pcbRead
[out] A pointer to a ULONG variable that receives the actual number of
bytes read from the stream object.
You can set this pointer to NULL. In this case, this method does not
return the number of bytes read.

From the latest greatest Windows SDK docs:

pcbRead
[out] A pointer to a ULONG variable that receives the actual number of bytes
read from the stream object.

and from MSDN:
http://msdn2.microsoft.com/en-us/library/aa380011.aspx

http://msdn2.microsoft.com/en-us/lib...ream.read.aspx

Note this:
For more information, see the existing documentation for
ISequentialStream::Read in the MSDN library.

You see, nothing about NULL to be allowed.

This :
http://msdn2.microsoft.com/en-us/library/ms890697.aspx

effectively talks about NULL to be allowed....
<snip
pcbRead
[out] Pointer to a ULONG variable that receives the actual number of bytes
read from the stream object.
You can set this pointer to NULL to indicate that you are not interested in
this value. In this case, this method does not provide the actual number of
bytes read.
/snip>

but this is about the ole32 implementation for Windows CE.

Willy.

May 9 '07 #6
In this case, I would say that the documentation for the actual .NET
type is superceeded by the documentation for the IStream interface itself,
since the .NET type is nothing more than a representation of the COM
interface, and implementations should stick to the contract (in word and
spirit) defined by the COM interface, not what the .NET documentation
chooses to expose about the IStream interface.

Which brings up an interesting point. I agree, the current
documentation about the COM IStream interface indicates nothing about null
for that parameter. I would argue that the previous documentation trumps
the current documentation here because of the immutability of COM
interfaces. While immutability most certainly applies to the actual
physical structure of the interfaces, I believe it applies to the semantics
(the spirit) of the contract as well.

I can't imagine that MS, a company obsessed with backwards
compatability, suddenly decided, especially for an already published
interface as common as IStream, that all implementations are expected to
perform a null value check on that parameter and error when it was
previously published that they did not have to. I can see it going the
other way (initially saying that all implementations had to check for null,
then saying they could optionally check for null), but it doesn't make sense
in this case.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:D5**********************************@microsof t.com...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:uP**************@TK2MSFTNGP02.phx.gbl...
> Read allows the last pointer passed in to be null. From the
documentation for IStream from the Platform SDK:

pcbRead
[out] A pointer to a ULONG variable that receives the actual number of
bytes read from the stream object.
You can set this pointer to NULL. In this case, this method does not
return the number of bytes read.


From the latest greatest Windows SDK docs:

pcbRead
[out] A pointer to a ULONG variable that receives the actual number of
bytes read from the stream object.

and from MSDN:
http://msdn2.microsoft.com/en-us/library/aa380011.aspx

http://msdn2.microsoft.com/en-us/lib...ream.read.aspx

Note this:
For more information, see the existing documentation for
ISequentialStream::Read in the MSDN library.

You see, nothing about NULL to be allowed.

This :
http://msdn2.microsoft.com/en-us/library/ms890697.aspx

effectively talks about NULL to be allowed....
<snip
pcbRead
[out] Pointer to a ULONG variable that receives the actual number of bytes
read from the stream object.
You can set this pointer to NULL to indicate that you are not interested
in this value. In this case, this method does not provide the actual
number of bytes read.
/snip>

but this is about the ole32 implementation for Windows CE.

Willy.

May 9 '07 #7
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP03.phx.gbl...
In this case, I would say that the documentation for the actual .NET
type is superceeded by the documentation for the IStream interface itself,
since the .NET type is nothing more than a representation of the COM
interface, and implementations should stick to the contract (in word and
spirit) defined by the COM interface, not what the .NET documentation
chooses to expose about the IStream interface.

Which brings up an interesting point. I agree, the current
documentation about the COM IStream interface indicates nothing about null
for that parameter. I would argue that the previous documentation trumps
the current documentation here because of the immutability of COM
interfaces. While immutability most certainly applies to the actual
physical structure of the interfaces, I believe it applies to the
semantics (the spirit) of the contract as well.

I can't imagine that MS, a company obsessed with backwards
compatability, suddenly decided, especially for an already published
interface as common as IStream, that all implementations are expected to
perform a null value check on that parameter and error when it was
previously published that they did not have to. I can see it going the
other way (initially saying that all implementations had to check for
null, then saying they could optionally check for null), but it doesn't
make sense in this case.
Don't know about the older documentation, I don't have access to the older
platform docs at the moment, all I find is that there is no mention of NULL
to be allowed in the on-line docs, nor on MSDN nor in the current Windows
SDK docs.
The IStream.Read docs in System.Runtime.InteropServices.ComTypes
does not mention NULL to be allowed, now if you do as the remark says (For
more information, see the existing documentation for ISequentialStream::Read
in the MSDN library.),
you end here:
http://msdn2.microsoft.com/en-us/library/aa380011.aspx
, again no trace of NULL to be allowed.

There must be something really wrong here, or the current docs are missing a
clause or the old docs were wrong , or the implementation (of ole32 on
Windows - not CE!!) has changed, or the CLR contains a hack to allow for
null pointers. I'll put together some code to find out what really happens
under the covers, and come back when I found out what.

Willy.

May 9 '07 #8
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP03.phx.gbl...
In this case, I would say that the documentation for the actual .NET
type is superceeded by the documentation for the IStream interface itself,
since the .NET type is nothing more than a representation of the COM
interface, and implementations should stick to the contract (in word and
spirit) defined by the COM interface, not what the .NET documentation
chooses to expose about the IStream interface.

Which brings up an interesting point. I agree, the current
documentation about the COM IStream interface indicates nothing about null
for that parameter. I would argue that the previous documentation trumps
the current documentation here because of the immutability of COM
interfaces. While immutability most certainly applies to the actual
physical structure of the interfaces, I believe it applies to the
semantics (the spirit) of the contract as well.

I can't imagine that MS, a company obsessed with backwards
compatability, suddenly decided, especially for an already published
interface as common as IStream, that all implementations are expected to
perform a null value check on that parameter and error when it was
previously published that they did not have to. I can see it going the
other way (initially saying that all implementations had to check for
null, then saying they could optionally check for null), but it doesn't
make sense in this case.
Ok I ran some tests, and I can see why this NULL pointer remark was removed
from the latest docs. It's up to the implementor of ISequentialStream to
accept null or not, so it's part of the interface contract to accept null
for this parameter.
Sure, a check for null is always required, as writing to a null pointer
would AV, but he's free to return an STG_E_INVALIDPOINTER (see
ISequentialStream docs) when he sees fit.

When calling an implementation that returns STG_E_INVALIDPOINTER from .NET,
you will get an:

System.Runtime.InteropServices.COMException (0x80030009): Invalid pointer
error. (Exception from HRESULT: 0x80030009 (STG_E_INVALIDPOINTER)
Note that some implementations are inconsistent in the way they report
conditions, you'll have to check for all possible conditions.

Willy.
May 9 '07 #9
Willy Denoyette [MVP] <wi*************@telenet.bewrote:

<snip>
The IStream.Read docs in System.Runtime.InteropServices.ComTypes
does not mention NULL to be allowed, now if you do as the remark says (For
more information, see the existing documentation for ISequentialStream::Read
in the MSDN library.),
you end here:
http://msdn2.microsoft.com/en-us/library/aa380011.aspx
, again no trace of NULL to be allowed.
It depends on what you mean by "the MSDN library". In the offline
version of the MSDN library (which is what I'd certainly look it up in
normally) it *does* specify it.
There must be something really wrong here, or the current docs are missing a
clause or the old docs were wrong , or the implementation (of ole32 on
Windows - not CE!!) has changed, or the CLR contains a hack to allow for
null pointers. I'll put together some code to find out what really happens
under the covers, and come back when I found out what.
My guess is that the docs are wrong/incomplete. I'm surprised at the
difference between the offline and online docs though :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 9 '07 #10
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP********************@msnews.microsoft.com.. .
Willy Denoyette [MVP] <wi*************@telenet.bewrote:

<snip>
>The IStream.Read docs in System.Runtime.InteropServices.ComTypes
does not mention NULL to be allowed, now if you do as the remark says
(For
more information, see the existing documentation for
ISequentialStream::Read
in the MSDN library.),
you end here:
http://msdn2.microsoft.com/en-us/library/aa380011.aspx
, again no trace of NULL to be allowed.

It depends on what you mean by "the MSDN library". In the offline
version of the MSDN library (which is what I'd certainly look it up in
normally) it *does* specify it.
I mean both, the on-line MSDN Library (msdn2), this one holds the latest
update, and,
Off-line, the latest MSDN from the subscribers download site (Build date:
2/1/2007), and the version that comes with Orcas Beta1 both have the same
description for both IStream.Read and ISequentialStream - no mention about
null pointers.
>There must be something really wrong here, or the current docs are
missing a
clause or the old docs were wrong , or the implementation (of ole32 on
Windows - not CE!!) has changed, or the CLR contains a hack to allow for
null pointers. I'll put together some code to find out what really
happens
under the covers, and come back when I found out what.

My guess is that the docs are wrong/incomplete. I'm surprised at the
difference between the offline and online docs though :(
Incomplete, I agree, wrong? (take care [1]) possibly, confusing -
certainly....

This is a general complaint of mine, the .NET MSDN docs are produced by
"robots", no longer by humans, they offer not much more than what's actually
documented in the .NET library code, that is the minimum, programmers are no
technical writers and having both is a slow and too expensive process, even
for MSFT , so they expect from us that we add the missing pieces and
possibly correct what's wrong (see the MSDN's Community contents), which is
not a bad idea after all.
[1] Finally it will be the community's fault the docs are wrong ;-).
Willy.

May 9 '07 #11
Willy Denoyette [MVP] <wi*************@telenet.bewrote:
It depends on what you mean by "the MSDN library". In the offline
version of the MSDN library (which is what I'd certainly look it up in
normally) it *does* specify it.

I mean both, the on-line MSDN Library (msdn2), this one holds the latest
update, and,
Off-line, the latest MSDN from the subscribers download site (Build date:
2/1/2007), and the version that comes with Orcas Beta1 both have the same
description for both IStream.Read and ISequentialStream - no mention about
null pointers.
Odd - I've got the same quote as Nick had in ISequentialStream::Read,
and I'm sure I've been installing all the latest disks... Is there a
decent way of finding the version of any particular file? The URL in
the offline MSDN is
ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.WIN32COM.v10.en/oledb/htm/
oledbisequentialstream__read.htm
My guess is that the docs are wrong/incomplete. I'm surprised at the
difference between the offline and online docs though :(

Incomplete, I agree, wrong? (take care [1]) possibly, confusing -
certainly....
I think it counts as "wrong" if it's been introduced into the
documentation as if it had always been part of the interface contract,
when older implementations could have legitimately returned the
appropriate error code instead.
This is a general complaint of mine, the .NET MSDN docs are produced by
"robots", no longer by humans, they offer not much more than what's actually
documented in the .NET library code, that is the minimum, programmers are no
technical writers and having both is a slow and too expensive process, even
for MSFT , so they expect from us that we add the missing pieces and
possibly correct what's wrong (see the MSDN's Community contents), which is
not a bad idea after all.
[1] Finally it will be the community's fault the docs are wrong ;-).
Indeed. I'm ashamed to say I haven't found time to contribute to the
MSDN pages, beyond mailing appropriate people when I've spotted
mistakes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 9 '07 #12
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP********************@msnews.microsoft.com.. .
Willy Denoyette [MVP] <wi*************@telenet.bewrote:
It depends on what you mean by "the MSDN library". In the offline
version of the MSDN library (which is what I'd certainly look it up in
normally) it *does* specify it.

I mean both, the on-line MSDN Library (msdn2), this one holds the latest
update, and,
Off-line, the latest MSDN from the subscribers download site (Build date:
2/1/2007), and the version that comes with Orcas Beta1 both have the same
description for both IStream.Read and ISequentialStream - no mention
about
null pointers.

Odd - I've got the same quote as Nick had in ISequentialStream::Read,
and I'm sure I've been installing all the latest disks... Is there a
decent way of finding the version of any particular file? The URL in
the offline MSDN is
ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.WIN32COM.v10.en/oledb/htm/
oledbisequentialstream__read.htm
Hmmm, I see ....
I'm on -
ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.WIN32COM.v10.en/stg/stg/isequentialstream_read.htm
-
see, ISTREAM is part of the "structured storage" interfaces, while you are
at the "oledb programmers reference " stuff, it's not surprising to find
this here, it's old hat that never gest updated in the post OLEDB age ;-),
IMO it doesn't even belong there.

Also take note that I did not install from the distribution, but I install
the latest download available from the MSDN subscribers area, more exactly,
the April 2007 MSDN Library (en_msdn_library_2007_04_dvd_X13-63638.iso
ISO-9660 DVD Image ).

My guess is that the docs are wrong/incomplete. I'm surprised at the
difference between the offline and online docs though :(

Incomplete, I agree, wrong? (take care [1]) possibly, confusing -
certainly....

I think it counts as "wrong" if it's been introduced into the
documentation as if it had always been part of the interface contract,
when older implementations could have legitimately returned the
appropriate error code instead.
Return values are never part of the contract, a COM interface is carved in
stone, right, that means you cannot change the return type nor the argument
types and number of arguments, but the value returned is not part of the
contract
The documentation specifies the "standard" return values, like S_OK, E_FAIL
etc.., but the implementor of an interface method is free to return what he
sees fit.

Now, when you look at the ISequential::Read method, you'll see that
STG_E_INVALIDPOINTER is one of the values that could "possibly" be returned
from a typical implemetation. If the implementor , requires the pointers to
be valid, it can return STG_E_INVALIDPOINTER when one of them is not. I have
found one implementation that actually does that. So IMO, the parameter
description was too general, in the sense that it makes you fell that null
is valid irrespective the implementaton of the interface.
So, again be prepared to get an exception thrown on you when passing null,
note that this is extremely rare to happen, besides, you should never pass a
null pointer, as it's nearly impossible to know what exactly has been
returned from the callee (0 values is valid data to be returned in the
buffer).
..

>This is a general complaint of mine, the .NET MSDN docs are produced by
"robots", no longer by humans, they offer not much more than what's
actually
documented in the .NET library code, that is the minimum, programmers are
no
technical writers and having both is a slow and too expensive process,
even
for MSFT , so they expect from us that we add the missing pieces and
possibly correct what's wrong (see the MSDN's Community contents), which
is
not a bad idea after all.
[1] Finally it will be the community's fault the docs are wrong ;-).

Indeed. I'm ashamed to say I haven't found time to contribute to the
MSDN pages, beyond mailing appropriate people when I've spotted
mistakes.

Same here, but I'm not ashamed ;-)

Willy.

May 9 '07 #13
Thank you for your comments.

Let me try to wrap it up.

The stream points to a file on the local drive - e.g. a *.txt file.

I use the System.Runtime.InteropServices.ComTypes.IStream interface from
dotNet 2.0:

namespace System.Runtime.InteropServices.ComTypes
{
[Guid("0000000c-0000-0000-C000-000000000046")]
[InterfaceType(1)]
public interface IStream
{
void Read(byte[] pv, int cb, IntPtr pcbRead);

etc etc.

The type is an IntPtr, not an ULONG. Also its a bit confusing that there is
no return value.
What should i do to make this work ?

/Peter
May 10 '07 #14
"Peter Larsen []" <Pe*********@newsgroup.nospamwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Thank you for your comments.

Let me try to wrap it up.

The stream points to a file on the local drive - e.g. a *.txt file.

I use the System.Runtime.InteropServices.ComTypes.IStream interface from
dotNet 2.0:

namespace System.Runtime.InteropServices.ComTypes
{
[Guid("0000000c-0000-0000-C000-000000000046")]
[InterfaceType(1)]
public interface IStream
{
void Read(byte[] pv, int cb, IntPtr pcbRead);

etc etc.

The type is an IntPtr, not an ULONG. Also its a bit confusing that there
is no return value.
What should i do to make this work ?

/Peter

Peter,
The intPtr must point to an int, on return you need to compare the value
pointed to by pcbRead against the value passed in cb.
Here is something to get you started.....

...
// Allocate space to hold an Int32, the pointer returned points to the
allocate Int32
IntPtr pcbRead = Marshal.AllocHGlobal(sizeof(int));
// pass the pointer as third argument
IStreamObject.Read(buffer, size, pcbRead);
// read the int value returned from the Read
int bytesReturned = Marshal.ReadInt32(pcbRead);
// comapre against the size of the buffer
if(bytesReturned == size)
// handle size data and go on reading from the stream...
...
if(bytesReturned < size )
// At end of the stream, bytesReturned is the amount of valid bytes
in the buffer, stop reading from the stream
....
if(bytesReturned == 0)
// no more data to read from the stream
....
// Free the unmanaged memory when done!!!
Marshal.FreeHGlobal(pcbRead);

Willy.


May 10 '07 #15
Ahh, now i understand what you mean.
I will try this and let you know the result.

/Peter
May 10 '07 #16
Peter Larsen [] <Pe*********@newsgroup.nospamwrote:
Thank you for your comments.

Let me try to wrap it up.

The stream points to a file on the local drive - e.g. a *.txt file.

I use the System.Runtime.InteropServices.ComTypes.IStream interface from
dotNet 2.0:

namespace System.Runtime.InteropServices.ComTypes
{
[Guid("0000000c-0000-0000-C000-000000000046")]
[InterfaceType(1)]
public interface IStream
{
void Read(byte[] pv, int cb, IntPtr pcbRead);

etc etc.

The type is an IntPtr, not an ULONG. Also its a bit confusing that there is
no return value.
What should i do to make this work ?
I thought I covered this in another post:

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

You might want to make it a uint instead of an int, if you're trying to
read more than 2GB at a time...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 10 '07 #17
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP********************@msnews.microsoft.com.. .
Peter Larsen [] <Pe*********@newsgroup.nospamwrote:
>Thank you for your comments.

Let me try to wrap it up.

The stream points to a file on the local drive - e.g. a *.txt file.

I use the System.Runtime.InteropServices.ComTypes.IStream interface from
dotNet 2.0:

namespace System.Runtime.InteropServices.ComTypes
{
[Guid("0000000c-0000-0000-C000-000000000046")]
[InterfaceType(1)]
public interface IStream
{
void Read(byte[] pv, int cb, IntPtr pcbRead);

etc etc.

The type is an IntPtr, not an ULONG. Also its a bit confusing that there
is
no return value.
What should i do to make this work ?

I thought I covered this in another post:

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

You might want to make it a uint instead of an int, if you're trying to
read more than 2GB at a time...
This will require 64bit Windows, you can't allocate such large buffer on 32
bit, even 2GB won't work.
Willy.

May 10 '07 #18
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:DD**********************************@microsof t.com...
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP********************@msnews.microsoft.com.. .
>Peter Larsen [] <Pe*********@newsgroup.nospamwrote:
>>Thank you for your comments.

Let me try to wrap it up.

The stream points to a file on the local drive - e.g. a *.txt file.

I use the System.Runtime.InteropServices.ComTypes.IStream interface from
dotNet 2.0:

namespace System.Runtime.InteropServices.ComTypes
{
[Guid("0000000c-0000-0000-C000-000000000046")]
[InterfaceType(1)]
public interface IStream
{
void Read(byte[] pv, int cb, IntPtr pcbRead);

etc etc.

The type is an IntPtr, not an ULONG. Also its a bit confusing that there
is
no return value.
What should i do to make this work ?

I thought I covered this in another post:

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

You might want to make it a uint instead of an int, if you're trying to
read more than 2GB at a time...

This will require 64bit Windows, you can't allocate such large buffer on
32 bit, even 2GB won't work.
Willy.

Actually it won't even work on 64bit Windows as the largest byte[] is
limited to 2GB on the current CLR.

Willy.

May 10 '07 #19
Willy Denoyette [MVP] <wi*************@telenet.bewrote:
You might want to make it a uint instead of an int, if you're trying to
read more than 2GB at a time...

This will require 64bit Windows, you can't allocate such large buffer on 32
bit, even 2GB won't work.
Good catch - so my code was inadvertently okay to start with :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 10 '07 #20
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP********************@msnews.microsoft.com.. .
Willy Denoyette [MVP] <wi*************@telenet.bewrote:
You might want to make it a uint instead of an int, if you're trying to
read more than 2GB at a time...

This will require 64bit Windows, you can't allocate such large buffer on
32
bit, even 2GB won't work.

Good catch - so my code was inadvertently okay to start with :)
Oh, sure. I just used an alternative way, without using "unsafe".

Willy.

May 10 '07 #21
Hi Willy and Jon,
Thank you for your help.

I don't know what happened - i think i forgot what is behind C# and similar
high-level languages :-)
I should deliver a pointer to a variable - thats how windows works.

BR
Peter
May 11 '07 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
6
by: Steve | last post by:
Hi, I'm trying to convert a file reading loop into one using streams. The BSD OS read API returns the number of bytes read, but istream::read returns itself. How can I find out the number of...
3
by: lpe540 | last post by:
Hi, I'm having trouble using istream to read in a file in its entirety on UNIX. I've written a dummy program that essencially reads in a file from stdin and writes it out to a file. When I cat a...
13
by: Gianni Mariani | last post by:
What I would like to do is read bytes from a stream, any number and any time. I would like it to wait until there are any bytes to read. I want the exact same functionality as cstdio's "fread"...
3
by: KWienhold | last post by:
I'm currently writing an application (using Visual Studio 2003 SP1 and C#) that stores files and additional information in a single compound file using IStorage/IStream. Since files in a compound...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.