2016-09-29 178 views
2

我無法讓C++使用「ms-appdata:/// roaming /」調用來檢索文件如何在調用我的DLL時訪問UWP的RoamingState文件夾?

我目前使用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信使活動的程序,但此行不:

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

即使我無法硬編碼每個UserProfile和UWP目錄的路徑。

有誰知道我可能做錯了什麼?我在Windows 10上使用Visual Studio 2015社區,並且有一個用於x86和x64 EXE的通用設置文件,並且我打算編寫一個服務,以便在該文件更改時將該設置文件複製到每個UWP的RoamingState文件夾。

回答

2

使用Windows ::存儲:: ::的ApplicationData :: RoamingFolder路徑屬性得到一個完整路徑漫遊文件夾:

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-應用程序數據:// 「事情只適用於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); 
} 
+0

我無法獲得Windows ::存儲命名空間的工作:http://imgur.com/a/LP7hH。我的配置有問題嗎,還是我做了其他問題? – HelloDog

+0

「Windows :: Storage :: ApplicationData :: RoamingFolder :: Path」不是真正有效的代碼,我只是使用該語法來引用該API。無論哪種方式,你的項目設置/類型是什麼? – Sunius

+0

我認爲這是正確的語法(至少,當我將它插入到Microsoft代碼示例中時,它顯示了這種方式),但是當它插入到我正在處理的dll項目中時,它仍然顯示爲錯誤: http: //imgur.com/a/8Ij2i 此外,這裏是我的DLL項目的一般配置: http://imgur.com/a/tQC4m 是否有錯誤存在,設置中的其他地方,或者是錯誤由其他事物完全產生? – HelloDog

相關問題