-
Notifications
You must be signed in to change notification settings - Fork 430
Expand file tree
/
Copy pathAppModel.Package.h
More file actions
294 lines (264 loc) · 12.1 KB
/
AppModel.Package.h
File metadata and controls
294 lines (264 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.
#ifndef __APPMODEL_PACKAGE_H
#define __APPMODEL_PACKAGE_H
#include <appmodel.h>
#include <filesystem>
#include <string>
#include <AppModel.Identity.h>
#include <ExportLoader.h>
#include <IsWindowsVersion.h>
namespace AppModel::Package
{
/// Find all Main+Framework packages in a package family registered to the current user
inline std::vector<std::wstring> FindByFamily(PCWSTR packageFamilyName)
{
uint32_t count{};
uint32_t bufferLength{};
const LONG rc{ ::FindPackagesByPackageFamily(packageFamilyName, PACKAGE_FILTER_HEAD | PACKAGE_FILTER_DIRECT, &count, nullptr, &bufferLength, nullptr, nullptr) };
if (rc == ERROR_SUCCESS)
{
// The package family has no packages registered to the user
return std::vector<std::wstring>();
}
else if (rc != ERROR_INSUFFICIENT_BUFFER)
{
THROW_WIN32(rc);
}
auto packageFullNames{ std::make_unique<PWSTR[]>(count) };
auto buffer{ std::make_unique<WCHAR[]>(bufferLength) };
THROW_IF_WIN32_ERROR(::FindPackagesByPackageFamily(packageFamilyName, PACKAGE_FILTER_HEAD | PACKAGE_FILTER_DIRECT, &count, packageFullNames.get(), &bufferLength, buffer.get(), nullptr));
std::vector<std::wstring> packageFullNamesList;
for (UINT32 index = 0; index < count; ++index)
{
const auto packageFullName{ packageFullNames[index] };
packageFullNamesList.push_back(std::wstring(packageFullName));
}
return packageFullNamesList;
}
/// Find all Main+Framework packages in a package family registered to the current user
inline std::vector<std::wstring> FindByFamily(const std::wstring& packageFamilyName)
{
return FindByFamily(packageFamilyName.c_str());
}
inline PACKAGE_VERSION ToPackageVersion(winrt::Windows::ApplicationModel::PackageVersion const& from)
{
PACKAGE_VERSION to{};
to.Major = from.Major;
to.Minor = from.Minor;
to.Build = from.Build;
to.Revision = from.Revision;
return to;
}
inline std::tuple<std::wstring, PACKAGE_VERSION, std::uint32_t, std::wstring, std::wstring, std::wstring> ParsePackageFullName(PCWSTR packageFullName)
{
BYTE buffer[
sizeof(PACKAGE_ID) +
sizeof(WCHAR) * (PACKAGE_NAME_MAX_LENGTH + 1) +
sizeof(WCHAR) * (PACKAGE_VERSION_MAX_LENGTH + 1) +
sizeof(WCHAR) * (PACKAGE_ARCHITECTURE_MAX_LENGTH + 1) +
sizeof(WCHAR) * (PACKAGE_RESOURCEID_MAX_LENGTH + 1) +
sizeof(WCHAR) * (PACKAGE_PUBLISHERID_MAX_LENGTH + 1)]{};
UINT32 bufferLength{ ARRAYSIZE(buffer) };
THROW_IF_WIN32_ERROR_MSG(::PackageIdFromFullName(packageFullName, PACKAGE_INFORMATION_BASIC, &bufferLength, buffer), "%ls", packageFullName);
const auto& packageId{ *reinterpret_cast<PACKAGE_ID*>(buffer) };
WCHAR packageFamilyName[PACKAGE_FAMILY_NAME_MAX_LENGTH + 1]{};
UINT32 packageFamilyNameLength{ ARRAYSIZE(packageFamilyName) };
THROW_IF_WIN32_ERROR_MSG(::PackageFamilyNameFromId(&packageId, &packageFamilyNameLength, packageFamilyName), "%ls", packageFullName);
return { std::wstring(packageId.name), packageId.version, packageId.processorArchitecture, std::wstring(packageId.resourceId ? packageId.resourceId : L""), std::wstring(packageId.publisherId), std::wstring(packageFamilyName) };
}
inline std::tuple<std::wstring, PACKAGE_VERSION, std::uint32_t, std::wstring, std::wstring, std::wstring> ParsePackageFullName(const std::wstring& packageFullName)
{
return ParsePackageFullName(packageFullName.c_str());
}
namespace details
{
// Helper: build the return type from PCWSTR
template <typename TString>
inline TString MakeFromPCWSTR(PCWSTR s)
{
if constexpr (std::is_same_v<TString, std::wstring>)
{
return s ? std::wstring{s} : std::wstring{};
}
else
{
// For WIL unique string wrappers, use WIL's maker.
// WIL string maker functions accept PCWSTR and return a unique_*_string wrapper. [1](https://github-wiki-see.page/m/microsoft/wil/wiki/String-helpers)
return wil::make_unique_string<TString>(s);
}
}
// GetPackagePathByFullName2 requires >=19H1
typedef LONG (WINAPI* GetPackagePathByFullName2Function)(
PCWSTR packageFullName,
PackagePathType packagePathType,
UINT32* pathLength,
PWSTR path);
inline wil::unique_hmodule g_dll_apiset_appmodel_runtime_1_3;
inline GetPackagePathByFullName2Function g_getPackagePathByFullName2{};
inline std::once_flag g_onceFlag{};
inline void initialize()
{
wil::unique_hmodule dll;
if (::ExportLoader::Load(L"api-ms-win-appmodel-runtime-l1-1-3.dll", wil::out_param(dll)))
{
return;
}
if (dll)
{
GetPackagePathByFullName2Function getPackagePathByFullName2{};
if (FAILED(::ExportLoader::GetFunctionIfExists<GetPackagePathByFullName2Function>(dll.get(), "GetPackagePathByFullName2", &getPackagePathByFullName2)))
{
return;
}
if (getPackagePathByFullName2)
{
g_dll_apiset_appmodel_runtime_1_3 = std::move(dll);
g_getPackagePathByFullName2 = std::move(getPackagePathByFullName2);
}
}
}
/// Get the path for a package, if GetPackagePathByFullName2() is available.
/// Return an empty path if the PackagePathType isn't supported on current platform (*pathLength=0, *path="").
/// @see https://learn.microsoft.com/en-us/windows/win32/api/appmodel/nf-appmodel-getpackagepathbyfullname2
inline HRESULT GetPackagePathByFullName2IfSupported(
_In_ PCWSTR packageFullName,
PackagePathType packagePathType,
std::uint32_t* pathLength,
_Out_writes_opt_(*pathLength) PWSTR path)
{
// Availability is a matter of timeline:
// * PackagePathType_Install is available since Win8
// * PackagePathType_Mutable is available since 19H1
// * PackagePathType_Effective is available since 19H1
// * PackagePathType_MachineExternalLocation is available since 20H1
// * PackagePathType_UserExternalLocation is available since 20H1
// * PackagePathType_EffectiveExternalLocation is available since 20H1
// GetPackagePathByFullName() is available since Win8
// GetPackagePathByFullName2() is available since 19H1 (though not all PackagePathType values were supported that early)
//
// Treat asks for locations not supported by the current system the same as not-found
if (::WindowsVersion::IsWindows10_20H1OrGreater() ||
(::WindowsVersion::IsWindows10_19H1OrGreater() &&
((packagePathType == PackagePathType_Install) || (packagePathType == PackagePathType_Mutable) || (packagePathType == PackagePathType_Effective))))
{
std::call_once(g_onceFlag, initialize);
RETURN_HR_IF_NULL(E_NOTIMPL, g_getPackagePathByFullName2);
RETURN_IF_FAILED(g_getPackagePathByFullName2(packageFullName, packagePathType, pathLength, path));
}
else if ((packagePathType == PackagePathType_Install) || (packagePathType == PackagePathType_Effective))
{
// Only Install location is supported by the current system
// Effective is thus equivalent to Install
// Either way, rock it old school...
RETURN_IF_FAILED(::GetPackagePathByFullName(packageFullName, pathLength, path));
}
else
{
// The requested location isn't possible on the current system
if (path && (*pathLength > 0))
{
*path = L'\0';
}
*pathLength = 0;
}
return S_OK;
}
}
/// Get the path for a package.
/// @see https://learn.microsoft.com/en-us/windows/win32/api/appmodel/nf-appmodel-getcurrentpackagepath2
template <typename Tstring>
inline Tstring GetPath(_In_ PCWSTR packageFullName, PackagePathType packagePathType)
{
// Paths can be long but typically short(ish). We can use a quick fixed buffer
// as an optimization and fallback to dynamic allocation if need be
WCHAR path[MAX_PATH]{};
uint32_t pathLength{ ARRAYSIZE(path) };
const auto hr{ details::GetPackagePathByFullName2IfSupported(packageFullName, packagePathType, &pathLength, path) };
if (SUCCEEDED(hr))
{
if (pathLength > 0)
{
return details::MakeFromPCWSTR<Tstring>(path);
}
else
{
return Tstring{};
}
}
else if ((hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) ||
(hr == HRESULT_FROM_WIN32(APPMODEL_ERROR_NO_MUTABLE_DIRECTORY)) ||
(hr == E_NOTIMPL))
{
return Tstring{};
}
else if (hr != HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
{
THROW_HR_MSG(hr, "PackageFullName=%ls PackagePathType=%d", packageFullName ? packageFullName : L"<null>", static_cast<int>(packagePathType));
}
// It's bigger than a breadbox. Allocate memory
std::unique_ptr<WCHAR[]> pathBuffer{ std::make_unique<WCHAR[]>(pathLength) };
THROW_IF_WIN32_ERROR_MSG(details::GetPackagePathByFullName2IfSupported(packageFullName, packagePathType, &pathLength, pathBuffer.get()),
"PackageFullName=%ls PackagePathType=%d", packageFullName ? packageFullName : L"<null>", static_cast<int>(packagePathType));
return details::MakeFromPCWSTR<Tstring>(pathBuffer.get());
}
/// Get the install path for a package.
/// @return null or empty string if the package isn't visible.
/// @see https://learn.microsoft.com/en-us/windows/win32/api/appmodel/ne-appmodel-packagepathtype
template <typename Tstring>
inline Tstring GetInstallPath(_In_ PCWSTR packageFullName)
{
return GetPath<Tstring>(packageFullName, PackagePathType_Install);
}
/// Get the mutable path for a package.
/// @return null or empty string if the package isn't visible to the caller or has no mutable path.
/// @see https://learn.microsoft.com/en-us/windows/win32/api/appmodel/ne-appmodel-packagepathtype
template <typename Tstring>
inline Tstring GetMutablePath(_In_ PCWSTR packageFullName)
{
return GetPath<Tstring>(packageFullName, PackagePathType_Mutable);
}
/// Get the machine external path for a package.
/// @return null or empty string if the package isn't visible to the caller or has no machine external path.
/// @see https://learn.microsoft.com/en-us/windows/win32/api/appmodel/ne-appmodel-packagepathtype
template <typename Tstring>
inline Tstring GetMachineExternalPath(_In_ PCWSTR packageFullName)
{
return GetPath<Tstring>(packageFullName, PackagePathType_MachineExternal);
}
/// Get the user external path for a package.
/// @return null or empty string if the package isn't visible to the caller or has no user external path.
/// @see https://learn.microsoft.com/en-us/windows/win32/api/appmodel/ne-appmodel-packagepathtype
template <typename Tstring>
inline Tstring GetUserExternalPath(_In_ PCWSTR packageFullName)
{
return GetPath<Tstring>(packageFullName, PackagePathType_UserExternal);
}
/// Get the effective external path for a package.
/// @return null or empty string if the package isn't visible to the caller or has no effective external path.
/// @see https://learn.microsoft.com/en-us/windows/win32/api/appmodel/ne-appmodel-packagepathtype
template <typename Tstring>
inline Tstring GetEffectiveExternalPath(_In_ PCWSTR packageFullName)
{
return GetPath<Tstring>(packageFullName, PackagePathType_EffectiveExternal);
}
/// Get the effective path for a package.
/// @return null or empty string if the package isn't visible to the caller.
/// @see https://learn.microsoft.com/en-us/windows/win32/api/appmodel/ne-appmodel-packagepathtype
template <typename Tstring>
inline Tstring GetEffectivePath(_In_ PCWSTR packageFullName)
{
return GetPath<Tstring>(packageFullName, PackagePathType_Effective);
}
inline std::filesystem::path GetAbsoluteFilename(
PCWSTR packageFullName,
PCWSTR filename,
PackagePathType packageType)
{
const auto path{ ::AppModel::Package::GetPath<std::wstring>(packageFullName, packageType) };
std::filesystem::path pathName{ path };
pathName /= filename;
return std::filesystem::absolute(pathName);
}
}
#endif // __APPMODEL_PACKAGE_H