@@ -179,14 +179,21 @@ void ResetAclPermissionsOnContextMenuFolder()
179
179
aclHelper.ResetAcl (contextMenuPath);
180
180
}
181
181
182
- Package GetSparsePackage ()
182
+ Package GetSparsePackage (bool allUsers )
183
183
{
184
184
PackageManager packageManager;
185
185
IIterable<Package> packages;
186
186
187
187
try
188
188
{
189
- packages = packageManager.FindPackagesForUser (L" " );
189
+ if (allUsers)
190
+ {
191
+ packages = packageManager.FindPackages ();
192
+ }
193
+ else
194
+ {
195
+ packages = packageManager.FindPackagesForUser (L" " );
196
+ }
190
197
}
191
198
catch (winrt::hresult_error)
192
199
{
@@ -206,7 +213,52 @@ Package GetSparsePackage()
206
213
return NULL ;
207
214
}
208
215
209
- HRESULT NppShell::Installer::RegisterSparsePackage ()
216
+ HRESULT NppShell::Installer::RegisterSparsePackageAllUsers ()
217
+ {
218
+ if (::GetSystemMetrics (SM_CLEANBOOT) > 0 )
219
+ {
220
+ return S_FALSE; // Otherwise we will get an unhandled exception later due to HRESULT 0x8007043c (ERROR_NOT_SAFEBOOT_SERVICE).
221
+ }
222
+
223
+ PackageManager packageManager;
224
+ StagePackageOptions options;
225
+
226
+ const wstring externalLocation = GetContextMenuPath ();
227
+ const wstring sparsePkgPath = externalLocation + L" \\ NppShell.msix" ;
228
+
229
+ Uri externalUri (externalLocation);
230
+ Uri packageUri (sparsePkgPath);
231
+
232
+ options.ExternalLocationUri (externalUri);
233
+
234
+ auto deploymentOperation = packageManager.StagePackageByUriAsync (packageUri, options);
235
+ auto deployResult = deploymentOperation.get ();
236
+
237
+ if (!SUCCEEDED (deployResult.ExtendedErrorCode ()))
238
+ {
239
+ return deployResult.ExtendedErrorCode ();
240
+ }
241
+
242
+ Package package = GetSparsePackage (true );
243
+ if (package == NULL )
244
+ {
245
+ return S_FALSE;
246
+ }
247
+
248
+ winrt::hstring familyName = package.Id ().FamilyName ();
249
+
250
+ deploymentOperation = packageManager.ProvisionPackageForAllUsersAsync (familyName);
251
+ deployResult = deploymentOperation.get ();
252
+
253
+ if (!SUCCEEDED (deployResult.ExtendedErrorCode ()))
254
+ {
255
+ return deployResult.ExtendedErrorCode ();
256
+ }
257
+
258
+ return S_OK;
259
+ }
260
+
261
+ HRESULT NppShell::Installer::RegisterSparsePackageCurrentUser ()
210
262
{
211
263
if (::GetSystemMetrics (SM_CLEANBOOT) > 0 )
212
264
{
@@ -245,15 +297,15 @@ HRESULT NppShell::Installer::UnregisterSparsePackage()
245
297
PackageManager packageManager;
246
298
IIterable<Package> packages;
247
299
248
- Package package = GetSparsePackage ();
300
+ Package package = GetSparsePackage (true );
249
301
250
302
if (package == NULL )
251
303
{
252
304
return S_FALSE;
253
305
}
254
306
255
307
winrt::hstring fullName = package.Id ().FullName ();
256
- auto deploymentOperation = packageManager.RemovePackageAsync (fullName, RemovalOptions::None );
308
+ auto deploymentOperation = packageManager.RemovePackageAsync (fullName, RemovalOptions::RemoveForAllUsers );
257
309
auto deployResult = deploymentOperation.get ();
258
310
259
311
if (!SUCCEEDED (deployResult.ExtendedErrorCode ()))
@@ -312,7 +364,8 @@ void ReRegisterSparsePackage()
312
364
UnregisterSparsePackage ();
313
365
314
366
// And then we register it again.
315
- RegisterSparsePackage ();
367
+ RegisterSparsePackageAllUsers ();
368
+ RegisterSparsePackageCurrentUser ();
316
369
}
317
370
318
371
HRESULT NppShell::Installer::Install ()
@@ -393,13 +446,13 @@ void EnsureRegistrationOnCurrentUserWorker()
393
446
winrt::init_apartment ();
394
447
395
448
// Get the package to check if it is already installed for the current user.
396
- Package existingPackage = GetSparsePackage ();
449
+ Package existingPackage = GetSparsePackage (false );
397
450
398
451
if (existingPackage == NULL )
399
452
{
400
453
// The package is not installed for the current user - but we know that Notepad++ is.
401
454
// If it wasn't, this code wouldn't be running, so it is safe to just register the package.
402
- RegisterSparsePackage ();
455
+ RegisterSparsePackageCurrentUser ();
403
456
404
457
// Finally we notify the shell that we have made changes, so it reloads the right click menu items.
405
458
SHChangeNotify (SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0 , 0 );
0 commit comments