locked
DataReader issue RRS feed

  • Question

  • When working with DataReader in WinRT, I would like to read the string contained in underlying buffer.

    For example I could use:

    DataReader reader = ..;
    reader.ReadString(uint lengthOfString);

    The question is? "How one can know" how long the string is?


    Damir Dobric
    developers.de
    daenet.de
    daenet.eu
    daenet.com
    Thursday, November 10, 2011 10:00 PM

Answers

All replies

  • Damir - assuming that you've assigned the stream which the DataReader is reading from, you can find the length of the stream by using Stream.Size.  The ReadString simply returns a string of the length that you define in the parameter from the stream.  The size is whatever you want it to be, as long as the stream holds that many bytes.
    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    Friday, November 11, 2011 3:02 PM
    Moderator
  • Hi Matt,

    thanks for your quick answer.
    I didn't assign the stream. I have received the stream in one callback method.

    void callback(MessageWebSocketMessageReceivedWebArgs arg){

     DataReader reader = arg.GetDataReader();

     // The question is how to get out the data.
     // After 10 years of evolution of .NET one would do following
     // MyObject obj = reader.GetObject<MyObject>();
     // OR
     // var msg = reader.ReadAsString();
     . . .

    }

    As a .NET developer I do not expect to know the type of the string (like BSTR 10 years ago) or the length of the string (15-20 years ago).

     

    Thanks

    Damir

     


    Damir Dobric
    developers.de
    daenet.de
    daenet.eu
    daenet.com
    Sunday, November 13, 2011 6:21 PM
  • Hi Damir,

    Try this and let me know if it works:

    var stringLength = reader.UnconsumedBufferLength;

    var msg = reader.ReadString(stringLength);

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.datareader.unconsumedbufferlength(v=VS.85).aspx

     


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    Tuesday, November 15, 2011 8:44 PM
    Moderator