Sunday, April 01, 2012

Calling WinRT functions from low level C++

The high level projection available in C++ targeting WinRT make working with WinRT from C++ a much easier task. It handles error reporting, reference counting, string conversions etc for you. As an example let's consider the following high level C++ code to retrieve the path of the temporary folder:

void GetTempFolderPathHighLevel(std::wstring& path)
{
  path = Windows::Storage::ApplicationData::Current->TemporaryFolder->Path->Data();
}

Short, simple, and to the point. The equivalent low level code to get this is heinous:

#include <windows.storage.h>
#include <wrl/client.h>
#include <wrl/wrappers/corewrappers.h>

HRESULT GetTempFolderPath(std::wstring& path)
{
  HRESULT hr;
  Microsoft::WRL::ComPtr<ABI::Windows::Storage::IApplicationDataStatics> applicationDataStatics;

  hr = Windows::Foundation::GetActivationFactory(Microsoft::WRL::Wrappers::HStringReference(RuntimeClass_Windows_Storage_ApplicationData).Get(), &applicationDataStatics);
  if (FAILED(hr))
  {
    return hr;
  }

QR: Inline image 1

Posted via email from Jasper-Net