Skip to content
Open
61 changes: 58 additions & 3 deletions dev/ApplicationData/M.W.S.ApplicationData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

#include "Validate.h"

namespace wil
{
typedef unique_any<::ApplicationDataState, decltype(&::ApplicationData_Close), ::ApplicationData_Close> unique_application_data_state;
}

static_assert(static_cast<int32_t>(winrt::Microsoft::Windows::Storage::ApplicationDataLocality::Local) == static_cast<int32_t>(winrt::Windows::Storage::ApplicationDataLocality::Local));
static_assert(static_cast<int32_t>(winrt::Microsoft::Windows::Storage::ApplicationDataLocality::LocalCache) == static_cast<int32_t>(winrt::Windows::Storage::ApplicationDataLocality::LocalCache));
static_assert(static_cast<int32_t>(winrt::Microsoft::Windows::Storage::ApplicationDataLocality::SharedLocal) == static_cast<int32_t>(winrt::Windows::Storage::ApplicationDataLocality::SharedLocal));
Expand Down Expand Up @@ -118,15 +123,39 @@ namespace winrt::Microsoft::Windows::Storage::implementation
{
return winrt::hstring{};
}
return StorageFolderToPath(m_applicationData.LocalCacheFolder());

wil::unique_application_data_state applicationDataState{ OpenApplicationData() };
WCHAR path[MAX_PATH]{};
UINT32 pathLength{ ARRAYSIZE(path) };
const HRESULT hr{ ApplicationData_GetLocalCachePath(applicationDataState.get(), &pathLength, path) };
if (SUCCEEDED(hr))
{
return winrt::hstring{ path };
}
THROW_HR_IF_MSG(hr, hr != HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), "%ls", m_packageFamilyName.c_str());
auto longPath{ std::make_unique<WCHAR[]>(pathLength) };
THROW_IF_FAILED_MSG(ApplicationData_GetLocalCachePath(applicationDataState.get(), &pathLength, path), "%ls", m_packageFamilyName.c_str());
return winrt::hstring(longPath.get());
}
hstring ApplicationData::LocalPath()
{
if (!m_applicationData)
{
return winrt::hstring{};
}
return StorageFolderToPath(m_applicationData.LocalFolder());

wil::unique_application_data_state applicationDataState{ OpenApplicationData() };
WCHAR path[MAX_PATH]{};
UINT32 pathLength{ ARRAYSIZE(path) };
const HRESULT hr{ ApplicationData_GetLocalPath(applicationDataState.get(), &pathLength, path) };
if (SUCCEEDED(hr))
{
return winrt::hstring{ path };
}
THROW_HR_IF_MSG(hr, hr != HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), "%ls", m_packageFamilyName.c_str());
auto longPath{ std::make_unique<WCHAR[]>(pathLength) };
THROW_IF_FAILED_MSG(ApplicationData_GetLocalPath(applicationDataState.get(), &pathLength, path), "%ls", m_packageFamilyName.c_str());
return winrt::hstring(longPath.get());
}
hstring ApplicationData::MachinePath()
{
Expand Down Expand Up @@ -157,7 +186,19 @@ namespace winrt::Microsoft::Windows::Storage::implementation
{
return winrt::hstring{};
}
return StorageFolderToPath(m_applicationData.TemporaryFolder());

wil::unique_application_data_state applicationDataState{ OpenApplicationData() };
WCHAR path[MAX_PATH]{};
UINT32 pathLength{ ARRAYSIZE(path) };
const HRESULT hr{ ApplicationData_GetTemporaryPath(applicationDataState.get(), &pathLength, path) };
if (SUCCEEDED(hr))
{
return winrt::hstring{ path };
}
THROW_HR_IF_MSG(hr, hr != HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), "%ls", m_packageFamilyName.c_str());
auto longPath{ std::make_unique<WCHAR[]>(pathLength) };
THROW_IF_FAILED_MSG(ApplicationData_GetTemporaryPath(applicationDataState.get(), &pathLength, path), "%ls", m_packageFamilyName.c_str());
return winrt::hstring(longPath.get());
}
winrt::Windows::Storage::StorageFolder ApplicationData::LocalCacheFolder()
{
Expand Down Expand Up @@ -241,6 +282,11 @@ namespace winrt::Microsoft::Windows::Storage::implementation
}
hstring ApplicationData::GetPublisherCachePath(hstring const& folderName)
{
if (!m_applicationData)
{
return winrt::hstring{};
}

auto folder{ GetPublisherCacheFolder(folderName) };
winrt::hstring path;
if (folder)
Expand Down Expand Up @@ -331,4 +377,13 @@ namespace winrt::Microsoft::Windows::Storage::implementation
THROW_HR_IF_MSG(E_INVALIDARG, ::Microsoft::Foundation::String::IsNullOrEmpty(string.c_str()), "Product not valid (%ls)", string.c_str());
THROW_HR_IF_MSG(E_INVALIDARG, ::Microsoft::Windows::Storage::is_prohibited_string(string.c_str()), "Product not valid (%ls)", string.c_str());
}

::ApplicationDataState ApplicationData::OpenApplicationData()
{
auto tokenUser{ wil::get_token_information<TOKEN_USER>(GetCurrentProcessToken()) };
PSID userSid{ tokenUser->User.Sid };
::ApplicationDataState applicationDataState{};
THROW_IF_FAILED(::ApplicationData_Open(userSid, m_packageFamilyName.c_str(), &applicationDataState));
return applicationDataState;
}
}
3 changes: 3 additions & 0 deletions dev/ApplicationData/M.W.S.ApplicationData.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "Microsoft.Windows.Storage.ApplicationData.g.h"

#include <FrameworkUdk/ApplicationData.h>

namespace winrt::Microsoft::Windows::Storage::implementation
{
struct ApplicationData : ApplicationDataT<ApplicationData>
Expand Down Expand Up @@ -42,6 +44,7 @@ namespace winrt::Microsoft::Windows::Storage::implementation
static hstring StorageFolderToPath(winrt::Windows::Storage::StorageFolder storageFolder);
static void _VerifyPublisher(winrt::hstring const& string);
static void _VerifyProduct(winrt::hstring const& string);
::ApplicationDataState OpenApplicationData();

private:
winrt::Windows::Storage::ApplicationData m_applicationData;
Expand Down
15 changes: 15 additions & 0 deletions dev/Common/Security.User.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ inline PWSTR SidToString(PSID sid)
THROW_IF_WIN32_BOOL_FALSE(::ConvertSidToStringSidW(sid, &sidString));
return sidString;
}

/// @return a new sid. Allocated via LocalAlloc; use LocalFree to deallocate
inline wil::unique_any_psid CopySid(PSID source)
{
if (!source)
{
return nullptr;
}

const auto size{ ::GetLengthSid(source) };
wil::unique_any_psid copy{ static_cast<PSID>(::LocalAlloc(LMEM_FIXED, size))};
THROW_IF_NULL_ALLOC(copy);
THROW_IF_WIN32_BOOL_FALSE(::CopySid(size, copy.get(), source));
return copy;
}
}

#endif // __SECURITY_USER_H
1 change: 1 addition & 0 deletions test/ApplicationData/ApplicationDataTests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
</Reference>
</ItemGroup>
<Target Name="CopyFiles" AfterTargets="AfterBuild">
<Copy SkipUnchangedFiles="true" SourceFiles="$(OutDir)\..\WindowsAppRuntime_DLL\Microsoft.Internal.FrameworkUdk.dll" DestinationFolder="$(OutDir)" />
<Copy SkipUnchangedFiles="true" SourceFiles="Test.testdef" DestinationFolder="$(OutDir)" />
</Target>
<ItemGroup>
Expand Down