【问题标题】:How do I access the RoamingState folder of an UWP when it calls my DLL?当 UWP 调用我的 DLL 时,如何访问它的 RoamingState 文件夹?
【发布时间】:2017-02-08 10:37:42
【问题描述】:

我无法让 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 社区,并且有一个用于 x86 和 x64 EXE 的通用设置文件,并且我计划编写一个服务,以便在该文件更改时将该设置文件复制到每个 UWP 的 RoamingState 文件夹。

【问题讨论】:

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


    【解决方案1】:

    使用 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);
    }
    

    【讨论】:

    • 我无法让 Windows::Storage 命名空间工作:imgur.com/a/LP7hH。我的配置有问题,还是我做错了什么?
    • "Windows::Storage::ApplicationData::RoamingFolder::Path" 不是真正有效的代码,我只是使用该语法来引用该 API。无论哪种方式,您的项目设置/类型是什么?
    • 我认为这是正确的语法(至少,当我将它插入 Microsoft 代码示例时,它会以这种方式显示),但在插入我的 dll 项目时仍然显示为错误我正在处理:imgur.com/a/8Ij2i 另外,这是我的 DLL 项目的一般配置:imgur.com/a/tQC4m 那里有什么问题,在设置的其他地方,还是完全由其他原因引起的错误?
    • 我会尝试一下,然后再报告;谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    相关资源
    最近更新 更多