How to do data conversion in Windows Store app

Q: How to convert streams in .NET?

A: Here is the list to show the different stream types conversion.

Conversion between Stream and IRandomAccessStream

// stream to IRandomAccessStream

var randomAccessStream = new InMemoryRandomAccessStream();

var outputStream = randomAccessStream.GetOutputStreamAt(0);

await RandomAccessStream.CopyAsync(stream.AsInputStream(), outputStream);

 

// IRandomAccessStream to stream

Stream stream = WindowsRuntimeStreamExtensions.AsStreamForRead(randomStream.GetInputStreamAt(0));

 

Conversion between IBuffer and Stream

// buffer to stream

Stream stream = WindowsRuntimeBufferExtensions.AsStream(buffer);

 

// stream to buffer

MemoryStream memoryStream = new MemoryStream();           

 if (stream != null)

 {

      byte[] bytes = ReadFully(stream);

      if (bytes != null)

      {

           var binaryWriter = new BinaryWriter(memoryStream);

           binaryWriter.Write(bytes);

       }

  }

 IBuffer buffer=WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream,0,(int)memoryStream.Length);

 

Conversion between IRandomAccessStream and FileInputStream/FileOutputStream

FileInputStream inputStream = randomStream.GetInputStreamAt(0) as FileInputStream;

 

FileOutputStream outStream = randomStream.GetOutputStreamAt(0) as FileOutputStream;

 

Related MSDN Forum Threads

https://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/4cda684d-77bf-4a0f-b3bd-a6b6a895bfff

https://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/4b2d1889-d4dd-4980-adef-540db2836239/

https://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/5d76bbc4-caed-43dc-8d68-6edc47cffd3b/

 

Q: How to convert IBuffer and COM IStream in C++/CX?

A: Conversion between IBuffer and COM IStream

// IBuffer to IStream

IStream* createIStreamFromIBuffer(Streams::IBuffer ^buffer) {

    // convert the IBuffer into an IStream to be used with WIC

    IStream *fileContentsStream;

    HRESULT res = CreateStreamOnHGlobal(NULL, TRUE, &fileContentsStream);

    if (FAILED(res) || !fileContentsStream) {

        throw ref new FailureException();

    }

    Streams::DataReader^ dataReader = Streams::DataReader::FromBuffer(buffer);

    // read the data into the stream in chunks of 1MB to preserve memory

    while (dataReader->UnconsumedBufferLength > 0) {

        UINT chunkSize = min(1024*1024, dataReader->UnconsumedBufferLength);

        auto data = ref new Platform::Array<uint8>(chunkSize);

        dataReader->ReadBytes(data);

        ULONG written;

        res = fileContentsStream->Write(data->Data, chunkSize, &written);

        if (FAILED(res) || written != chunkSize) {

            fileContentsStream->Release();

            throw ref new FailureException();

        }

    }

    return fileContentsStream;

}

 

//Com buffer to IBufferByteAccess

IUnknown* pUnk = reinterpret_cast<IUnknown*>(buffer);

                   

IBufferByteAccess* pBufferByteAccess = nullptr;

HRESULT hr = pUnk->QueryInterface(IID_PPV_ARGS(&pBufferByteAccess);

Related MSDN Forum Threads

https://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/dc39d796-1a3a-48e1-a095-2609600d01b7/

https://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/5e70aba9-1eb4-4960-88ba-8851bb9df365

https://social.msdn.microsoft.com/Forums/br/winappswithnativecode/thread/3acd59e7-389d-4147-95fa-15a9b1ad765c

 

Q: How to convert native char* and String^ in C++/CX?

A: Conversion between String^ and Char*

// String^ to char*

String^ str1 = "AAAAAAAA";

wstring wstr( str1->Data() );

int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);

char* buffer = new char[len + 1]; 

WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);

buffer[len] = '\0';

 

//Char* to String^

std::wstring s2ws(const std::string& s)

{

    int len;

    int slength = (int)s.length() + 1;

    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);

    wchar_t* buf = new wchar_t[len];

    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);

    std::wstring r(buf);

    delete[] buf;

    return r;

}

 

String ^statusString = ref new String( s2ws(statusHelper).c_str());

Related MSDN Forum Threads

https://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/c39b3060-0c2d-40b7-aa8c-035f1fad441c

https://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/48794f27-0db7-4335-bdfe-dca664ca28a9