Jump to content
Software FX Community

Problem with import/export via stream in C++


User (Legacy)

Recommended Posts

I am trying to export a chart as a stream, read the info from that stream

into a byte array and then create a different IStream object, write the data

from the array into that new stream and import a chart from the data that

was stored in the byte array. Export "seems" to work fine, but using the

import I get an error code of some sort returned in the lResult of the

raw_Import function.

Here is the code i am usuing....I modified the CfxImpExp.cpp file that is in

the samples/mfc directory when I installed chartfx. I also made it so that

the series legend is always present because i want to be able to save

information about where it is and the data that it contains as well. Also I

added 2 more members to the class, a byte* ( to hold data from the stream to

simulate disk operations ) and an int to store length of the byte *.

EXPORT:

void CCfxImpExp::OnExptofile()

{

CoInitialize( NULL );

GetDlgItem(IDC_CHANGES)->EnableWindow(TRUE);

CComPtr<IStream> stream;

if( !stream ){

HRESULT hr = CreateStreamOnHGlobal( NULL, FALSE, &stream );

VARIANT var;

long retval;

V_VT( &var ) = VT_STREAM;

V_UNKNOWN( &var ) = (IStream*)stream;

m_pChartFX->PutFileMask( (CfxFileMask)( m_pChartFX->FileMask |

FMASK_ALL ) );

hr = m_pChartFX->raw_Export(

CHART_CFXOLEFILE/*CHART_CFXOLETEMPLATE*/, var, &retval );

if( SUCCEEDED( hr ) ){

LARGE_INTEGER li;

li.QuadPart = (LONGLONG)0;

stream->Seek( li, STREAM_SEEK_SET, NULL );

long count = 0;

ULONG read = 0;

byte *ary = (byte*)malloc( 1024 );

hr = stream->Stat (&stg, 0);

while( ( hr = stream->Read( ary, 1024, &read ) ) == S_OK && read

> 0 ){

if( read == 1024 ) realloc( (void*)ary, 1024 );

count += read;

}

stream->Seek( li, STREAM_SEEK_SET, NULL );

array = ary;

ary_size = count;

}

}

}

IMPORT:

void CCfxImpExp::OnImpfromfile()

{

CoInitialize( NULL );

CComPtr<IStream> stream1;

HRESULT hr = CreateStreamOnHGlobal( NULL, true, &stream1 );

LARGE_INTEGER li;

ULARGE_INTEGER lu;

li.QuadPart = 0;

stream1->Seek(li,STREAM_SEEK_SET,&lu); // Make sure the stream is

positioned to where the chart is saved

ULONG written = 0;

hr = stream1->Write( array, ary_size, &written );

stream1->Seek(li,STREAM_SEEK_SET,&lu); // Make sure the stream is

positioned to where the chart is saved

long lResult;

VARIANT v;

V_VT(&v) = VT_STREAM;

V_UNKNOWN(&v) = (IStream*)stream1;

hr = m_pChartFX->raw_Import(

CHART_CFXOLEFILE/*CHART_CFXOLETEMPLATE*/,v,&lResult);

}

The hresult for the raw_Import returns S_OK, but the lResult returns

as -2146827967( 0x800a0141 )....what does this mean? What am I doing wrong?

Link to comment
Share on other sites

Your code has a bug, the re-allocation is incorrect, it fails when the

stream is bigger than 1024.

This has nothing to do with Chart FX, the Stream is right, if you import

from that memory stream you will get it correctly, it's the copying of the

Stream to the array that is wrong when the Stream is bigger than 1024 bytes.

You are reading each 1024 block in the SAME piece of memory overriding the

previous one.

Here is a code that copies a Stream to an Array correctly.

LARGE_INTEGER li;

li.QuadPart = (LONGLONG)0;

stream->Seek( li, STREAM_SEEK_SET, NULL );

unsigned long cbRead;

STATSTG stg;

hr = stream->Stat (&stg, 0);

array = (BYTE *)malloc( stg.cbSize.LowPart );

stream->Read((void *) array,stg.cbSize.LowPart,&cbRead);

ary_size = stg.cbSize.LowPart;

This, of course, works for streams smaller than 4GB.

--

FP

Software FX, Inc.

Link to comment
Share on other sites

woopsie...sry bout that...should have noticed that...thanks though

"SoftwareFX Support" <support@softwarefx.com> wrote in message

news:nVbtrWe0CHA.2656@webserver1.softwarefx.com...

> Your code has a bug, the re-allocation is incorrect, it fails when the

> stream is bigger than 1024.

>

> This has nothing to do with Chart FX, the Stream is right, if you import

> from that memory stream you will get it correctly, it's the copying of the

> Stream to the array that is wrong when the Stream is bigger than 1024

bytes.

> You are reading each 1024 block in the SAME piece of memory overriding the

> previous one.

>

> Here is a code that copies a Stream to an Array correctly.

>

> LARGE_INTEGER li;

> li.QuadPart = (LONGLONG)0;

> stream->Seek( li, STREAM_SEEK_SET, NULL );

> unsigned long cbRead;

> STATSTG stg;

> hr = stream->Stat (&stg, 0);

> array = (BYTE *)malloc( stg.cbSize.LowPart );

> stream->Read((void *) array,stg.cbSize.LowPart,&cbRead);

> ary_size = stg.cbSize.LowPart;

>

> This, of course, works for streams smaller than 4GB.

>

> --

> FP

> Software FX, Inc.

>

>

post-2107-13922394614436_thumb.gif

Link to comment
Share on other sites

  • 2 weeks later...

Do you have an example of this for VB.

Also, is there a way to save this to another file, such as an ini file?

Thanks.

Ed Kennedy

SoftwareFX Support wrote in message ...

>Your code has a bug, the re-allocation is incorrect, it fails when the

>stream is bigger than 1024.

>

>This has nothing to do with Chart FX, the Stream is right, if you import

>from that memory stream you will get it correctly, it's the copying of the

>Stream to the array that is wrong when the Stream is bigger than 1024

bytes.

>You are reading each 1024 block in the SAME piece of memory overriding the

>previous one.

>

>Here is a code that copies a Stream to an Array correctly.

>

> LARGE_INTEGER li;

> li.QuadPart = (LONGLONG)0;

> stream->Seek( li, STREAM_SEEK_SET, NULL );

> unsigned long cbRead;

> STATSTG stg;

> hr = stream->Stat (&stg, 0);

> array = (BYTE *)malloc( stg.cbSize.LowPart );

> stream->Read((void *) array,stg.cbSize.LowPart,&cbRead);

> ary_size = stg.cbSize.LowPart;

>

>This, of course, works for streams smaller than 4GB.

>

>--

>FP

>Software FX, Inc.

>

>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...