473,386 Members | 1,721 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,386 software developers and data experts.

How to convert from IStream to System.IO.Stream

MJB
I'm getting an IStream back from function xmlHttp.responsestream. I would
like to convert this to a System.IO.Stream in order to work with it in my
application. Has anyone encountered this and written a conversion?

TIA,
Matt
Nov 20 '05 #1
3 2435
I don't think you can just plain convert it, but you can always read the
contents of one into another.
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

"MJB" <mb*@email.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
I'm getting an IStream back from function xmlHttp.responsestream. I would
like to convert this to a System.IO.Stream in order to work with it in my
application. Has anyone encountered this and written a conversion?

TIA,
Matt

Nov 20 '05 #2
You can wrap the IStream in a .NET wrapper which .NET objects can the
access, like this:

using System;

using System.IO;

using System.Runtime.InteropServices;
public class ComStream : Stream {

// the managed stream being wrapped

UCOMIStream originalStream_;

public ComStream( UCOMIStream stream ) {

if( stream != null ) {

originalStream_ = stream;

}

else {

throw new ArgumentNullException( "stream" );

}

}

~ComStream() {

Close();

}

// property to get original stream object

public UCOMIStream UnderlyingStream {

get {

return originalStream_;

}

}

// reads a specified number of bytes from the stream object

// into memory starting at the current seek pointer

public unsafe override int Read( byte[] buffer, int offset, int count ) {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

if( offset != 0 ) {

throw new NotSupportedException("only 0 offset is supported");

}

int bytesRead;

IntPtr address = new IntPtr( &bytesRead );

originalStream_.Read( buffer, count, address );

return bytesRead;

}
// writes a specified number of bytes into the stream object

// starting at the current seek pointer

public override void Write( byte[] buffer, int offset, int count ) {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

if( offset != 0 ) {

throw new NotSupportedException("only 0 offset is supported");

}

originalStream_.Write( buffer, count, IntPtr.Zero );

}

// changes the seek pointer to a new location relative to the beginning

// of the stream, the end of the stream, or the current seek position

public unsafe override long Seek( long offset, SeekOrigin origin ){
if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

long position = 0;

IntPtr address = new IntPtr( &position );

originalStream_.Seek( offset, (int) origin, address );
return position;

}

public override long Length {

get {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

STATSTG statstg;

originalStream_.Stat( out statstg, 1 /* STATSFLAG_NONAME*/ );

return statstg.cbSize;

}

}

public override long Position {

get { return Seek( 0, SeekOrigin.Current ) ; }

set { Seek( value, SeekOrigin.Begin ) ; }

}
// changes the size of the stream object

public override void SetLength( long value ) {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

originalStream_.SetSize( value );

}

// closes (disposes) the stream

public override void Close() {

if( originalStream_ != null ) {

originalStream_.Commit( 0 );

// Marshal.ReleaseComObject( originalStream_ );

originalStream_= null;

GC.SuppressFinalize( this );

}

}

public override void Flush() {

originalStream_.Commit(0);

}

public override bool CanRead {

get { return true ; }

}

public override bool CanWrite {

get { return true ;}

}

public override bool CanSeek {

get { return true ; }

}

}

This comes mostly from this book:
http://www.amazon.com/exec/obidos/tg...lance&n=507846

"Klaus H. Probst" <us*******@vbbox.com> wrote in message
news:up**************@TK2MSFTNGP10.phx.gbl...
I don't think you can just plain convert it, but you can always read the
contents of one into another.
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

"MJB" <mb*@email.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and written a conversion?

TIA,
Matt


Nov 20 '05 #3
MJB
Great information. Thanks John

"john farrow" <vi*******@xtra.co.nz> wrote in message
news:Ov**************@TK2MSFTNGP11.phx.gbl...
You can wrap the IStream in a .NET wrapper which .NET objects can the
access, like this:

using System;

using System.IO;

using System.Runtime.InteropServices;
public class ComStream : Stream {

// the managed stream being wrapped

UCOMIStream originalStream_;

public ComStream( UCOMIStream stream ) {

if( stream != null ) {

originalStream_ = stream;

}

else {

throw new ArgumentNullException( "stream" );

}

}

~ComStream() {

Close();

}

// property to get original stream object

public UCOMIStream UnderlyingStream {

get {

return originalStream_;

}

}

// reads a specified number of bytes from the stream object

// into memory starting at the current seek pointer

public unsafe override int Read( byte[] buffer, int offset, int count ) {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

if( offset != 0 ) {

throw new NotSupportedException("only 0 offset is supported");

}

int bytesRead;

IntPtr address = new IntPtr( &bytesRead );

originalStream_.Read( buffer, count, address );

return bytesRead;

}
// writes a specified number of bytes into the stream object

// starting at the current seek pointer

public override void Write( byte[] buffer, int offset, int count ) {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

if( offset != 0 ) {

throw new NotSupportedException("only 0 offset is supported");

}

originalStream_.Write( buffer, count, IntPtr.Zero );

}

// changes the seek pointer to a new location relative to the beginning

// of the stream, the end of the stream, or the current seek position

public unsafe override long Seek( long offset, SeekOrigin origin ){
if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

long position = 0;

IntPtr address = new IntPtr( &position );

originalStream_.Seek( offset, (int) origin, address );
return position;

}

public override long Length {

get {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

STATSTG statstg;

originalStream_.Stat( out statstg, 1 /* STATSFLAG_NONAME*/ );

return statstg.cbSize;

}

}

public override long Position {

get { return Seek( 0, SeekOrigin.Current ) ; }

set { Seek( value, SeekOrigin.Begin ) ; }

}
// changes the size of the stream object

public override void SetLength( long value ) {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

originalStream_.SetSize( value );

}

// closes (disposes) the stream

public override void Close() {

if( originalStream_ != null ) {

originalStream_.Commit( 0 );

// Marshal.ReleaseComObject( originalStream_ );

originalStream_= null;

GC.SuppressFinalize( this );

}

}

public override void Flush() {

originalStream_.Commit(0);

}

public override bool CanRead {

get { return true ; }

}

public override bool CanWrite {

get { return true ;}

}

public override bool CanSeek {

get { return true ; }

}

}

This comes mostly from this book:
http://www.amazon.com/exec/obidos/tg...lance&n=507846
"Klaus H. Probst" <us*******@vbbox.com> wrote in message
news:up**************@TK2MSFTNGP10.phx.gbl...
I don't think you can just plain convert it, but you can always read the
contents of one into another.
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

"MJB" <mb*@email.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and written a conversion?

TIA,
Matt



Nov 20 '05 #4

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

Similar topics

0
by: Juan Carlos CORUÑA | last post by:
Hello all, I have developed an automation server using Mark's win32 extensions and I must return a IStream COM object from one method of the automation server. Here is an extract of my code:...
3
by: Roger That | last post by:
Hi, I am trying to use the function "CreateStreamOnHGlobal" from python code (I need to pass an IStream object to MSXML, like I do in C++ code). I was able to retrieve a pointer on the IStream...
3
by: MJB | last post by:
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and...
3
by: Charles Law | last post by:
Could anyone please tell me what is wrong with the code below: STDMETHODIMP CSimpleObj::WriteUnmanagedData(BSTR pString, IStream** ppData) { HRESULT hr; hr = CreateStreamOnHGlobal(0, TRUE,...
13
by: Peteroid | last post by:
These don't work (I'm using VS C++.NET 2005 Express with clr:/pure syntax): ostream& operator <<( ostream& output, String^ str ) { output << str ; //compile error return output ; } ...
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"...
21
by: Peter Larsen [] | last post by:
Hi, I have a problem using System.Runtime.InteropServices.ComTypes.IStream. Sample using the Read() method: if (IStreamObject != null) { IntPtr readBytes = IntPtr.Zero;...
4
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am developing website application in asp.net , visual C# and atl com. I am using atl com component in visual C# application. One of the function of com component interface returns...
3
by: Kourosh | last post by:
Hi all, I'm trying to call a COM function from C# The function gets a paramter of type IStream in C++. I've found the type System.Runtime.InteropServices.ComTypes.IStream, however I'm not sure...
8
by: Cartoper | last post by:
I have an image stored in a HGlobal and would like to load it into a System::Image via the System::Image::FromStream(). How do I convert the HGlobal into a stream?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.