다음을 통해 공유


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;
  }

  Microsoft::WRL::ComPtr<ABI::Windows::Storage::IApplicationData> applicationData;
  hr = applicationDataStatics->get_Current(&applicationData);
  if (FAILED(hr))
  {
    return hr;
  }

  Microsoft::WRL::ComPtr<ABI::Windows::Storage::IStorageFolder> storageFolder;
  hr = applicationData->get_TemporaryFolder(&storageFolder);
  if (FAILED(hr))
  {
    return hr;
  }

  Microsoft::WRL::ComPtr<ABI::Windows::Storage::IStorageItem> storageItem;
  hr = storageFolder.As(&storageItem);
  if (FAILED(hr))
  {
    return hr;
  }

  HSTRING folderName;
  hr = storageItem->get_Path(&folderName);
  if (FAILED(hr))
  {
    return hr;
  }

  UINT32 length;
  PCWSTR value = WindowsGetStringRawBuffer(folderName, &length);
  path = value;
  WindowsDeleteString(folderName);
return S_OK;
}

Comments

  • Anonymous
    April 10, 2012
    Do you need to release HSTRING from get_Path ? WindowsDeleteString ?

  • Anonymous
    April 10, 2012
    Could you tell me any document on ABI ?