1 #pragma once 2 3 #include <wrl/client.h> 4 #include <ppl.h> 5 #include <ppltasks.h> 6 7 namespace DX 8 { 9 inline void ThrowIfFailed(HRESULT hr) 10 { 11 if (FAILED(hr)) 12 { 13 // Set a breakpoint on this line to catch Win32 API errors. 14 throw Platform::Exception::CreateException(hr); 15 } 16 } 17 18 // Function that reads from a binary file asynchronously. 19 inline Concurrency::task<Platform::Array<byte>^> ReadDataAsync(Platform::String^ filename) 20 { 21 using namespace Windows::Storage; 22 using namespace Concurrency; 23 24 auto folder = Windows::ApplicationModel::Package::Current->InstalledLocation; 25 26 return create_task(folder->GetFileAsync(filename)).then([] (StorageFile^ file) 27 { 28 return file->OpenReadAsync(); 29 }).then([] (Streams::IRandomAccessStreamWithContentType^ stream) 30 { 31 unsigned int bufferSize = static_cast<unsigned int>(stream->Size); 32 auto fileBuffer = ref new Streams::Buffer(bufferSize); 33 return stream->ReadAsync(fileBuffer, bufferSize, Streams::InputStreamOptions::None); 34 }).then([] (Streams::IBuffer^ fileBuffer) -> Platform::Array<byte>^ 35 { 36 auto fileData = ref new Platform::Array<byte>(fileBuffer->Length); 37 Streams::DataReader::FromBuffer(fileBuffer)->ReadBytes(fileData); 38 return fileData; 39 }); 40 } 41 }