2

I can't get C++ to work with the "ms-appdata:///roaming/" call to retrieve files

I am currently using cpp to write a Chinese Input Method Editor, and it is packaged as a dll.
Therefore, when I make a call to ifstream to read my Settings File, file permissions are limited by whichever application is active, such as for when a Universal Windows Program, which is sandboxed to it's own folders within AppData, and cannot even read other files, much less write to them. My current difficulty is locating files (specifically, the settings file) within that sandbox in the first place.

For example, this line:

WCHAR* FileName2 = L"C:/Users/Dog/AppData/Local/Packages/Facebook.317180B0BB486_8xx8rvfyw5nnt/RoamingState/Settings.txt";

works fine with

std::ifstream settingsFile;
settingsFile.open(FileName2, std::ios::in ); //this reading is successful for hard-coded path
settingsFile.get(myChar);
settingsFile.close();

when facebook messenger is the active program, but this line does not:

WCHAR* FileName2 = L"ms-appdata:///roaming/Settings.txt";

Even though I can't hard code the path for every UserProfile and UWP directory.

Does anyone know what I could be doing wrong? I am using Visual Studio 2015 Community on Windows 10 and have a universal settings file for x86 and x64 EXEs, and I plan to write a service to copy that settings file to the RoamingState folder of each UWP whenever that file changes.

HelloDog
  • 47
  • 1
  • 6

1 Answers1

4

Use Windows::Storage::ApplicationData::RoamingFolder::Path property to get a full path to the roaming folder:

https://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.roamingfolder.aspx

https://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefolder.path.aspx

"ms-appdata://" thing works only with WinRT file APIs.

Here's how to access that API from standard C++:

#include <cstdint>
#include <string>
#include <windows.storage.h>
#include <wrl.h>

using namespace ABI::Windows::Storage;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;

std::wstring GetRoamingFolderPath()
{
    ComPtr<IApplicationDataStatics> appDataStatics;
    auto hr = RoGetActivationFactory(HStringReference(L"Windows.Storage.ApplicationData").Get(), __uuidof(appDataStatics), &appDataStatics);
    if (FAILED(hr)) throw std::runtime_error("Failed to retrieve application data statics");

    ComPtr<IApplicationData> appData;
    hr = appDataStatics->get_Current(&appData);
    if (FAILED(hr)) throw std::runtime_error("Failed to retrieve current application data");

    ComPtr<IStorageFolder> roamingFolder;
    hr = appData->get_RoamingFolder(&roamingFolder);
    if (FAILED(hr)) throw std::runtime_error("Failed to retrieve roaming folder");

    ComPtr<IStorageItem> folderItem;
    hr = roamingFolder.As(&folderItem);
    if (FAILED(hr)) throw std::runtime_error("Failed to cast roaming folder to IStorageItem");

    HString roamingPathHString;
    hr = folderItem->get_Path(roamingPathHString.GetAddressOf());
    if (FAILED(hr)) throw std::runtime_error("Failed to retrieve roaming folder path");

    uint32_t pathLength;
    auto roamingPathCStr = roamingPathHString.GetRawBuffer(&pathLength);
    return std::wstring(roamingPathCStr, pathLength);
}
Sunius
  • 2,789
  • 18
  • 30
  • I can't get the Windows::Storage namespace working: http://imgur.com/a/LP7hH . Is there something wrong with my configuration, or am I doing something else wrong? – HelloDog Oct 03 '16 at 15:39
  • "Windows::Storage::ApplicationData::RoamingFolder::Path" isn't really valid code, I just used that syntax to refer to that API. Either way, what are your project settings/type? – Sunius Oct 03 '16 at 17:42
  • I think that this is the correct syntax (at least, it shows up that way when I insert it into Microsoft Code samples), but it still shows up as an error when inserted into the dll project I am working on: http://imgur.com/a/8Ij2i Also, here is the general configuration for my DLL project: http://imgur.com/a/tQC4m Is there something wrong there, elsewhere in the settings, or is the error arising from something else entirely? – HelloDog Oct 03 '16 at 20:21
  • I'll try it out and then report back; Thanks! – HelloDog Oct 04 '16 at 22:02