locked
How to use DataWriter->WriteBuffer(IBuffer)?

    Question

  • The WriteBuffer method asks an IBuffer as parameter but I can't store a value to it, any suggestion on how to do this? Thanks.
    Wednesday, April 25, 2012 2:58 AM

Answers

  • Hi,

    Please note that the parameter IBuffer of WriteBuffer method is source content used to write to output stream.

    You can use DataWriter.WriteXXX method to write something and then use DataWriter.DetachBuffer to get a buffer that is associated with the data writer.

    Another approach is using FileIO.ReadBufferAsync to read the contents of the specified file and return a buffer. Then you can pass in it as a parameter of DataWriter.WriteBuffer.

    Here is sample code.

    IRandomAccessStream^ writeStream;
    // initialize writeStream
    ......
    DataWriter^ dataWriter = ref new DataWriter(writeStream);
    dataWriter->WriteString("test");
    IBuffer^ buffer = dataWriter->DetachBuffer(); 

    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by cppmachine Wednesday, April 25, 2012 7:32 AM
    Wednesday, April 25, 2012 7:26 AM
    Moderator

All replies

  • Are you trying to write into the buffer or to read out of the buffer?

    DataWriter->WriteBuffer(IBuffer) reads from the buffer and writes into the stream the DataWriter is attached to.

    To write into an IBuffer query for its IBufferByteAccess interface to get the contents of the buffer and you can write into its byte array.

    --Rob

    Wednesday, April 25, 2012 6:36 AM
    Owner
  • Sorry, I think my main question is inappropriate.

    What I really need to do is to write into the buffer so that I can call DataWriter->WriteBuffer(IBuffer) but I can't find a way to initialize the buffer.

    I tried using the IBufferByteAccess interface but its Buffer method returns a value HRESULT. How can I convert it to IBuffer?


    • Edited by cppmachine Wednesday, April 25, 2012 7:10 AM
    Wednesday, April 25, 2012 7:05 AM
  • Hi,

    Please note that the parameter IBuffer of WriteBuffer method is source content used to write to output stream.

    You can use DataWriter.WriteXXX method to write something and then use DataWriter.DetachBuffer to get a buffer that is associated with the data writer.

    Another approach is using FileIO.ReadBufferAsync to read the contents of the specified file and return a buffer. Then you can pass in it as a parameter of DataWriter.WriteBuffer.

    Here is sample code.

    IRandomAccessStream^ writeStream;
    // initialize writeStream
    ......
    DataWriter^ dataWriter = ref new DataWriter(writeStream);
    dataWriter->WriteString("test");
    IBuffer^ buffer = dataWriter->DetachBuffer(); 

    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by cppmachine Wednesday, April 25, 2012 7:32 AM
    Wednesday, April 25, 2012 7:26 AM
    Moderator