locked
how to write an IStream to a File ?

    Question

  • Hi,

    i have an IStream object (created via CreateStreamOnHGlobal) and i want to write this Stream to a File ( i have a StorageFile^ object with me which i got it via FileSavePicker's PickSaveFileAsync function)

    how i do that ? 

    i checked all FileIO related methods and classes and didnt find a way to do it. 

    FileIO provides a way to write a IBuffer to StorageFile but i didnt find a way to even get IBuffer object from the given IStream object.

    Thanks,



    Raj

    Wednesday, May 8, 2013 11:03 AM

Answers

  • Hi,

    You can use IRandomAccess to cover steam to Istream. Please take a look of these codes.

    // IRandomAccess to IStream
    ComPtr<IStream> stream ;
    
    HRESULT hr=CreateStreamOverRandomAccessStream(randomAccessStream, IID_PPV_ARGS(&stream));
    if (FAILED(hr)) 
    { 
    throw Platform::Exception::CreateException(hr); 
    }
    
    // IStream to IRandomAccess
    
    Windows::Storage::Streams::IRandomAccessStream^ comRAS;
    IUnknown* p11 = reinterpret_cast<IUnknown *>(comRAS);
    
    static const GUID guidIRandomAccessStream = 
    { 0x905a0fe1, 0xbc53, 0x11df, { 0x8c, 0x49, 0x00, 0x1e, 0x4f, 0xc6, 0x86, 0xda } };
    
    hr = CreateRandomAccessStreamOverStream(stream.Get(), BSOS_DEFAULT, guidIRandomAccessStream, (void**)&p11);


    If you just want to use steam in Windows Store, you can use IRandomAccessStream, you can use InMemoryRandomAccessStream to use it
    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.inmemoryrandomaccessstream.aspx

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jesse Jiang Monday, May 13, 2013 7:21 AM
    • Marked as answer by Jesse Jiang Tuesday, May 14, 2013 2:09 AM
    Friday, May 10, 2013 2:34 AM

All replies

  • You just read the data via IStream::Read to a byte array, then write the byte array to the storage file. loop until you have no more data. 


    Visual C++ MVP

    Wednesday, May 8, 2013 6:25 PM
  • i did this 

    IStream *stream;
    unsigned char buf[4096];
    ULONG actualRead;
    HRESULT hr = stream->Read(buf , sizeof(buf), &actualRead);
    Windows::Storage::StorageFile ^file;
    Platform::ArrayReference<uint8> array1(buf, sizeof(buf));

    FileIO::WriteBytesAsync(file , array1);

    i dont see a way to loop .. how do i do that ?

    looks like i need to read the entire data into a byte array and then write everything in one go.

    is there any other way that i am missing .. 

    Thanks


    Raj

    Thursday, May 9, 2013 11:05 AM
  • Hi,

    You can use IRandomAccess to cover steam to Istream. Please take a look of these codes.

    // IRandomAccess to IStream
    ComPtr<IStream> stream ;
    
    HRESULT hr=CreateStreamOverRandomAccessStream(randomAccessStream, IID_PPV_ARGS(&stream));
    if (FAILED(hr)) 
    { 
    throw Platform::Exception::CreateException(hr); 
    }
    
    // IStream to IRandomAccess
    
    Windows::Storage::Streams::IRandomAccessStream^ comRAS;
    IUnknown* p11 = reinterpret_cast<IUnknown *>(comRAS);
    
    static const GUID guidIRandomAccessStream = 
    { 0x905a0fe1, 0xbc53, 0x11df, { 0x8c, 0x49, 0x00, 0x1e, 0x4f, 0xc6, 0x86, 0xda } };
    
    hr = CreateRandomAccessStreamOverStream(stream.Get(), BSOS_DEFAULT, guidIRandomAccessStream, (void**)&p11);


    If you just want to use steam in Windows Store, you can use IRandomAccessStream, you can use InMemoryRandomAccessStream to use it
    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.inmemoryrandomaccessstream.aspx

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jesse Jiang Monday, May 13, 2013 7:21 AM
    • Marked as answer by Jesse Jiang Tuesday, May 14, 2013 2:09 AM
    Friday, May 10, 2013 2:34 AM