Exception when trying to convert an IInputStream to a Platform::String using DataReader loop RRS feed

  • Question

  • Hi,

    I am trying to convert an inputstream from a zip file holding an utf8 string into a Platform::String to load it Inside the xml parser, so I wrote the following code :

    Platform::String^ Container::ConvertUTF8StreamToPString(IInputStream^ inputStream, size_t BUF_SIZE /*= 4096*/)
    	Platform::String^ str = nullptr;
    	std::wstring wstr;
    	using Converter = std::wstring_convert<std::codecvt_utf8<wchar_t>>;
    	Converter converter;
    	if (inputStream != nullptr)
    		Platform::WriteOnlyArray<unsigned char, 1U>^ buf = nullptr;
    		DataReader^ dataReader = ref new DataReader(inputStream);
    		dataReader->UnicodeEncoding = UnicodeEncoding::Utf8;
    		int numRead = 0;
    			auto opLoadData = create_task(dataReader->LoadAsync(BUF_SIZE));
    			opLoadData.then([dataReader, &converter, &wstr, &numRead](unsigned int numBytesLoaded) {
    				numRead = numBytesLoaded;
    				if (numRead > 0)
    					Platform::WriteOnlyArray<unsigned char, 1U>^ buf = ref new Platform::Array<unsigned char, 1U>(numRead);
    					wstr.append(converter.from_bytes(reinterpret_cast<char*>(buf->Data), reinterpret_cast<char*>(buf->Data + numRead)));
    		} while (numRead > 0);
    	str = ref new Platform::String(wstr.c_str());
    	return str;

    The first loop works fine but the second one where there is nothing more to read throw an null pointer exception, how can I fix this code ?


    Thursday, January 30, 2014 3:00 PM


  • I might have found the reason why, it might be due to the fact that I am implementing a IInpuStream and when there was not data left to read I was returning an null IBuffer^ from ReadAsync, now I return an empty IBuffer^...

    I think I am going to give up my application, I am loosing too much time with c++/cx

    • Marked as answer by Vincent Rich Thursday, January 30, 2014 4:38 PM
    • Edited by Vincent Rich Thursday, January 30, 2014 5:36 PM
    Thursday, January 30, 2014 4:36 PM