@@ -179,14 +179,21 @@ void ResetAclPermissionsOnContextMenuFolder()
179179 aclHelper.ResetAcl (contextMenuPath);
180180}
181181
182- Package GetSparsePackage ()
182+ Package GetSparsePackage (bool allUsers )
183183{
184184 PackageManager packageManager;
185185 IIterable<Package> packages;
186186
187187 try
188188 {
189- packages = packageManager.FindPackagesForUser (L" " );
189+ if (allUsers)
190+ {
191+ packages = packageManager.FindPackages ();
192+ }
193+ else
194+ {
195+ packages = packageManager.FindPackagesForUser (L" " );
196+ }
190197 }
191198 catch (winrt::hresult_error)
192199 {
@@ -206,7 +213,52 @@ Package GetSparsePackage()
206213 return NULL ;
207214}
208215
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 ()
210262{
211263 if (::GetSystemMetrics (SM_CLEANBOOT) > 0 )
212264 {
@@ -245,15 +297,15 @@ HRESULT NppShell::Installer::UnregisterSparsePackage()
245297 PackageManager packageManager;
246298 IIterable<Package> packages;
247299
248- Package package = GetSparsePackage ();
300+ Package package = GetSparsePackage (true );
249301
250302 if (package == NULL )
251303 {
252304 return S_FALSE;
253305 }
254306
255307 winrt::hstring fullName = package.Id ().FullName ();
256- auto deploymentOperation = packageManager.RemovePackageAsync (fullName, RemovalOptions::None );
308+ auto deploymentOperation = packageManager.RemovePackageAsync (fullName, RemovalOptions::RemoveForAllUsers );
257309 auto deployResult = deploymentOperation.get ();
258310
259311 if (!SUCCEEDED (deployResult.ExtendedErrorCode ()))
@@ -312,7 +364,8 @@ void ReRegisterSparsePackage()
312364 UnregisterSparsePackage ();
313365
314366 // And then we register it again.
315- RegisterSparsePackage ();
367+ RegisterSparsePackageAllUsers ();
368+ RegisterSparsePackageCurrentUser ();
316369}
317370
318371HRESULT NppShell::Installer::Install ()
@@ -393,13 +446,13 @@ void EnsureRegistrationOnCurrentUserWorker()
393446 winrt::init_apartment ();
394447
395448 // Get the package to check if it is already installed for the current user.
396- Package existingPackage = GetSparsePackage ();
449+ Package existingPackage = GetSparsePackage (false );
397450
398451 if (existingPackage == NULL )
399452 {
400453 // The package is not installed for the current user - but we know that Notepad++ is.
401454 // If it wasn't, this code wouldn't be running, so it is safe to just register the package.
402- RegisterSparsePackage ();
455+ RegisterSparsePackageCurrentUser ();
403456
404457 // Finally we notify the shell that we have made changes, so it reloads the right click menu items.
405458 SHChangeNotify (SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0 , 0 );
0 commit comments