c++ - 当 UWP 调用我的 DLL 时,如何访问它的 RoamingState 文件夹?

标签 c++ dll win-universal-app uwp appdata

我无法让 C++ 使用“ms-appdata:///roaming/”调用来检索文件

我目前正在使用cpp编写一个中文输入法编辑器,并且将其封装为dll。
因此,当我调用 ifstream 来读取我的设置文件时,文件权限受到任何事件应用程序的限制,例如通用 Windows 程序(该程序被沙箱化到 AppData 中自己的文件夹中),甚至无法读取其他文件,更不用说写信给他们了。我当前的困难是首先在该沙箱中查找文件(特别是设置文件)。

例如这一行:

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

可以很好地配合

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

当 facebook Messenger 是事件程序,但此行不是:

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

尽管我无法对每个 UserProfile 和 UWP 目录的路径进行硬编码。

有谁知道我可能做错了什么?我在 Windows 10 上使用 Visual Studio 2015 Community,并且有一个适用于 x86 和 x64 EXE 的通用设置文件,并且我计划编写一项服务,以便在该文件发生更改时将该设置文件复制到每个 UWP 的 RoamingState 文件夹中。

最佳答案

使用 Windows::Storage::ApplicationData::RoamingFolder::Path 属性获取漫游文件夹的完整路径:

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://”仅适用于 WinRT 文件 API。

以下是如何从标准 C++ 访问该 API:

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

关于c++ - 当 UWP 调用我的 DLL 时,如何访问它的 RoamingState 文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39781540/

相关文章:

c++ - 与顺序索引相比,随机索引数组是否会对性能产生影响?

c++ - 如何将 cv::Mat 转换为 cv::Vec3f?

dll - 带有 scipy.signal 导入错误 : DLL load failed 的 Pyinstaller

win-universal-app - 如何使用 ef core 1 在 UWP 中运行迁移

c# - Xamarin.Forms Windows 10 通用错误 "CS0103 InitializeComponent does not exist"

xaml - 如何在 UWP 中使用资源字典中的路径数据

c++ - 在多个文件中使用时包含全局 vector 时出错

c++ - 为什么这两种多维数组的实现执行时间相差这么大呢?

powershell - 如何从 DLL 导入 Cmdlet

c# - 如何使用 C++ DLL 中的 C# 代码