From 242843a8af0579873924aaa64787adc4d862d632 Mon Sep 17 00:00:00 2001 From: Howard Kapustein Date: Fri, 28 Aug 2026 18:00:07 -0700 Subject: [PATCH 1/3] ApplicationData: Optimize *Path properties --- dev/ApplicationData/M.W.S.ApplicationData.cpp | 76 ++++++++++++++-- dev/ApplicationData/M.W.S.ApplicationData.h | 3 + dev/Common/Security.User.h | 15 +++ .../ApplicationDataTests.vcxproj | 1 + .../StoragePickersTestApp.vcxproj | 91 +++++++++---------- 5 files changed, 128 insertions(+), 58 deletions(-) diff --git a/dev/ApplicationData/M.W.S.ApplicationData.cpp b/dev/ApplicationData/M.W.S.ApplicationData.cpp index 9890a1460a..9e2d567cf9 100644 --- a/dev/ApplicationData/M.W.S.ApplicationData.cpp +++ b/dev/ApplicationData/M.W.S.ApplicationData.cpp @@ -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(winrt::Microsoft::Windows::Storage::ApplicationDataLocality::Local) == static_cast(winrt::Windows::Storage::ApplicationDataLocality::Local)); static_assert(static_cast(winrt::Microsoft::Windows::Storage::ApplicationDataLocality::LocalCache) == static_cast(winrt::Windows::Storage::ApplicationDataLocality::LocalCache)); static_assert(static_cast(winrt::Microsoft::Windows::Storage::ApplicationDataLocality::SharedLocal) == static_cast(winrt::Windows::Storage::ApplicationDataLocality::SharedLocal)); @@ -118,7 +123,19 @@ 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(pathLength) }; + THROW_IF_FAILED_MSG(ApplicationData_GetLocalCachePath(applicationDataState.get(), &pathLength, path), "%ls", m_packageFamilyName.c_str()); + return winrt::hstring(longPath.get()); } hstring ApplicationData::LocalPath() { @@ -126,7 +143,19 @@ namespace winrt::Microsoft::Windows::Storage::implementation { 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(pathLength) }; + THROW_IF_FAILED_MSG(ApplicationData_GetLocalPath(applicationDataState.get(), &pathLength, path), "%ls", m_packageFamilyName.c_str()); + return winrt::hstring(longPath.get()); } hstring ApplicationData::MachinePath() { @@ -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(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() { @@ -241,13 +282,23 @@ namespace winrt::Microsoft::Windows::Storage::implementation } hstring ApplicationData::GetPublisherCachePath(hstring const& folderName) { - auto folder{ GetPublisherCacheFolder(folderName) }; - winrt::hstring path; - if (folder) + if (!m_applicationData) { - path = folder.Path(); + return winrt::hstring{}; } - return path; + + wil::unique_application_data_state applicationDataState{ OpenApplicationData() }; + WCHAR path[MAX_PATH]{}; + UINT32 pathLength{ ARRAYSIZE(path) }; + const HRESULT hr{ ApplicationData_GetPublisherCachePath(applicationDataState.get(), folderName.c_str(), &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(pathLength) }; + THROW_IF_FAILED_MSG(ApplicationData_GetPublisherCachePath(applicationDataState.get(), folderName.c_str(), &pathLength, path), "%ls", m_packageFamilyName.c_str()); + return winrt::hstring(longPath.get()); } winrt::Windows::Storage::StorageFolder ApplicationData::GetPublisherCacheFolder(hstring const& folderName) { @@ -331,4 +382,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(GetCurrentProcessToken()) }; + PSID userSid{ tokenUser->User.Sid }; + ::ApplicationDataState applicationDataState{}; + THROW_IF_FAILED(::ApplicationData_Open(userSid, m_packageFamilyName.c_str(), &applicationDataState)); + return applicationDataState; + } } diff --git a/dev/ApplicationData/M.W.S.ApplicationData.h b/dev/ApplicationData/M.W.S.ApplicationData.h index 0d9f975561..48c001ead0 100644 --- a/dev/ApplicationData/M.W.S.ApplicationData.h +++ b/dev/ApplicationData/M.W.S.ApplicationData.h @@ -5,6 +5,8 @@ #include "Microsoft.Windows.Storage.ApplicationData.g.h" +#include + namespace winrt::Microsoft::Windows::Storage::implementation { struct ApplicationData : ApplicationDataT @@ -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; diff --git a/dev/Common/Security.User.h b/dev/Common/Security.User.h index 2ebaa8f1c6..800bd1f8b5 100644 --- a/dev/Common/Security.User.h +++ b/dev/Common/Security.User.h @@ -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 null; + } + + const auto size{ ::GetLengthSid(source) }; + wil::unique_any_psid copy{ static_cast(::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 diff --git a/test/ApplicationData/ApplicationDataTests.vcxproj b/test/ApplicationData/ApplicationDataTests.vcxproj index 261cef4581..40a70501eb 100644 --- a/test/ApplicationData/ApplicationDataTests.vcxproj +++ b/test/ApplicationData/ApplicationDataTests.vcxproj @@ -132,6 +132,7 @@ + diff --git a/test/TestApps/StoragePickersTestApp/StoragePickersTestApp.vcxproj b/test/TestApps/StoragePickersTestApp/StoragePickersTestApp.vcxproj index 55ea4959ae..8bd53f5aef 100644 --- a/test/TestApps/StoragePickersTestApp/StoragePickersTestApp.vcxproj +++ b/test/TestApps/StoragePickersTestApp/StoragePickersTestApp.vcxproj @@ -2,7 +2,7 @@ - + true true @@ -52,9 +52,9 @@ true false - + - + Debug @@ -81,72 +81,63 @@ ARM64 - + Application v143 Unicode true - + true - true - + false - true - false - + - + - + - + - + Use pch.h $(IntDir)pch.pch - Level4 %(AdditionalOptions) /bigobj DISABLE_XAML_GENERATED_MAIN;%(PreprocessorDefinitions) - Guard true - + _DEBUG;%(PreprocessorDefinitions) - + NDEBUG;%(PreprocessorDefinitions) - - true - true - - + - + @@ -156,12 +147,12 @@ MainWindow.xaml - + - + Create @@ -177,20 +168,20 @@ - + Code MainWindow.xaml - + false - + @@ -200,15 +191,15 @@ - + - + - + @@ -216,13 +207,13 @@ true - + true - + - + @@ -230,7 +221,7 @@ - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. @@ -243,41 +234,41 @@ - + - - - - - - - - @@ -285,10 +276,10 @@ <_WindowsAppRuntimeDll>$(RepoRoot)BuildOutput\$(Configuration)\$(WindowsAppRuntimeBuildPlatform)\WindowsAppRuntime_DLL\Microsoft.WindowsAppRuntime.dll <_WindowsAppRuntimeWinmd>$(RepoRoot)BuildOutput\$(Configuration)\$(WindowsAppRuntimeBuildPlatform)\WindowsAppRuntime_DLL\Microsoft.Windows.Storage.Pickers.winmd - - From 128e930056922578a4c68ef219ee4baf58cdb634 Mon Sep 17 00:00:00 2001 From: Howard Kapustein Date: Mon, 14 Sep 2026 23:03:08 -0400 Subject: [PATCH 2/3] Fixed typo --- dev/Common/Security.User.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/Common/Security.User.h b/dev/Common/Security.User.h index 800bd1f8b5..e8850af361 100644 --- a/dev/Common/Security.User.h +++ b/dev/Common/Security.User.h @@ -52,7 +52,7 @@ inline wil::unique_any_psid CopySid(PSID source) { if (!source) { - return null; + return nullptr; } const auto size{ ::GetLengthSid(source) }; From d44a25c1ab3ab7351b7f3e35f496d9b5d0d35e7a Mon Sep 17 00:00:00 2001 From: Howard Kapustein Date: Sat, 26 Sep 2026 18:17:37 -0700 Subject: [PATCH 3/3] Unlike Application's other *Folder APIs, Windows.Storage.ApplicationData.GetPublisherCacheFolder does *part* of its work in the same lower level implementation like the other *Folder APIs but also does functional work at a higher level. That means the get-path optimizations in FrameworkUdk won't work for PublisherCache. So we revert its implementation to GetPath() = GetFolder().Path and only optimize the other *Path() methods. Simimlar performance optimization here would require much larger OS/FrUdk effort, so no dice for today. This API's rarely used so it's not a problem; optmiizing LocalPath etc is the priority, so, yay, Mission Accomplished :-) --- dev/ApplicationData/M.W.S.ApplicationData.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/dev/ApplicationData/M.W.S.ApplicationData.cpp b/dev/ApplicationData/M.W.S.ApplicationData.cpp index 9e2d567cf9..38a2c471a9 100644 --- a/dev/ApplicationData/M.W.S.ApplicationData.cpp +++ b/dev/ApplicationData/M.W.S.ApplicationData.cpp @@ -287,18 +287,13 @@ namespace winrt::Microsoft::Windows::Storage::implementation return winrt::hstring{}; } - wil::unique_application_data_state applicationDataState{ OpenApplicationData() }; - WCHAR path[MAX_PATH]{}; - UINT32 pathLength{ ARRAYSIZE(path) }; - const HRESULT hr{ ApplicationData_GetPublisherCachePath(applicationDataState.get(), folderName.c_str(), &pathLength, path)}; - if (SUCCEEDED(hr)) + auto folder{ GetPublisherCacheFolder(folderName) }; + winrt::hstring path; + if (folder) { - return winrt::hstring{ path }; + path = folder.Path(); } - THROW_HR_IF_MSG(hr, hr != HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), "%ls", m_packageFamilyName.c_str()); - auto longPath{ std::make_unique(pathLength) }; - THROW_IF_FAILED_MSG(ApplicationData_GetPublisherCachePath(applicationDataState.get(), folderName.c_str(), &pathLength, path), "%ls", m_packageFamilyName.c_str()); - return winrt::hstring(longPath.get()); + return path; } winrt::Windows::Storage::StorageFolder ApplicationData::GetPublisherCacheFolder(hstring const& folderName) {