From aca13d8a0b8a614e0d24b8d1925b7a9945b7b27d Mon Sep 17 00:00:00 2001 From: Bjarke Istrup Pedersen Date: Sat, 18 Nov 2023 10:29:31 +0100 Subject: [PATCH 1/3] Code cleanup - Add missing bracers --- Installer.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Installer.cpp b/Installer.cpp index 65f52ee..9e396d6 100644 --- a/Installer.cpp +++ b/Installer.cpp @@ -209,7 +209,9 @@ Package GetSparsePackage() HRESULT NppShell::Installer::RegisterSparsePackage() { if (::GetSystemMetrics(SM_CLEANBOOT) > 0) + { return S_FALSE; // Otherwise we will get an unhandled exception later due to HRESULT 0x8007043c (ERROR_NOT_SAFEBOOT_SERVICE). + } PackageManager packageManager; AddPackageOptions options; @@ -236,7 +238,9 @@ HRESULT NppShell::Installer::RegisterSparsePackage() HRESULT NppShell::Installer::UnregisterSparsePackage() { if (::GetSystemMetrics(SM_CLEANBOOT) > 0) + { return S_FALSE; // Only to speed up things a bit here. (code in the following GetSparsePackage() is safe against the ERROR_NOT_SAFEBOOT_SERVICE) + } PackageManager packageManager; IIterable packages; @@ -298,7 +302,9 @@ HRESULT NppShell::Installer::UnregisterOldContextMenu() void ReRegisterSparsePackage() { if (::GetSystemMetrics(SM_CLEANBOOT) > 0) + { return; // Sparse package reg/unreg cannot be done in the Windows OS SafeMode. + } winrt::init_apartment(); From 388f4564fc9e55d080c2e5241a76f08816dc628b Mon Sep 17 00:00:00 2001 From: Bjarke Istrup Pedersen Date: Wed, 22 Nov 2023 12:27:58 +0100 Subject: [PATCH 2/3] Change folder permission reset to reset the correct folder --- Installer.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Installer.cpp b/Installer.cpp index 9e396d6..ea479cc 100644 --- a/Installer.cpp +++ b/Installer.cpp @@ -167,16 +167,16 @@ HRESULT MoveFileToTempAndScheduleDeletion(const wstring& filePath, bool moveToTe return S_OK; } -void ResetAclPermissionsOnApplicationFolder() +void ResetAclPermissionsOnContextMenuFolder() { - // First we get the path where Notepad++ is installed. - const wstring applicationPath = GetApplicationPath(); + // First we get the path where Notepad++ context menu is installed. + const wstring contextMenuPath = GetContextMenuPath(); // Create a new AclHelper AclHelper aclHelper; - // Reset the ACL of the folder where Notepad++ is installed. - aclHelper.ResetAcl(applicationPath); + // Reset the ACL of the folder where Notepad++ context menu is installed. + aclHelper.ResetAcl(contextMenuPath); } Package GetSparsePackage() @@ -262,7 +262,7 @@ HRESULT NppShell::Installer::UnregisterSparsePackage() } // After unregistering the sparse package, we reset the folder permissions of the folder where we are installed. - ResetAclPermissionsOnApplicationFolder(); + ResetAclPermissionsOnContextMenuFolder(); return S_OK; } From 5e410dd6450a2572dd2eb209250561b6eec97535 Mon Sep 17 00:00:00 2001 From: Bjarke Istrup Pedersen Date: Sat, 18 Nov 2023 10:27:43 +0100 Subject: [PATCH 3/3] Changes needed to install msix for all users, provided by ddomingos-encora on github --- Installer.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++------ Installer.h | 3 ++- 2 files changed, 63 insertions(+), 9 deletions(-) diff --git a/Installer.cpp b/Installer.cpp index ea479cc..2cdeea3 100644 --- a/Installer.cpp +++ b/Installer.cpp @@ -179,14 +179,21 @@ void ResetAclPermissionsOnContextMenuFolder() aclHelper.ResetAcl(contextMenuPath); } -Package GetSparsePackage() +Package GetSparsePackage(bool allUsers) { PackageManager packageManager; IIterable packages; try { - packages = packageManager.FindPackagesForUser(L""); + if (allUsers) + { + packages = packageManager.FindPackages(); + } + else + { + packages = packageManager.FindPackagesForUser(L""); + } } catch (winrt::hresult_error) { @@ -206,7 +213,52 @@ Package GetSparsePackage() return NULL; } -HRESULT NppShell::Installer::RegisterSparsePackage() +HRESULT NppShell::Installer::RegisterSparsePackageAllUsers() +{ + if (::GetSystemMetrics(SM_CLEANBOOT) > 0) + { + return S_FALSE; // Otherwise we will get an unhandled exception later due to HRESULT 0x8007043c (ERROR_NOT_SAFEBOOT_SERVICE). + } + + PackageManager packageManager; + StagePackageOptions options; + + const wstring externalLocation = GetContextMenuPath(); + const wstring sparsePkgPath = externalLocation + L"\\NppShell.msix"; + + Uri externalUri(externalLocation); + Uri packageUri(sparsePkgPath); + + options.ExternalLocationUri(externalUri); + + auto deploymentOperation = packageManager.StagePackageByUriAsync(packageUri, options); + auto deployResult = deploymentOperation.get(); + + if (!SUCCEEDED(deployResult.ExtendedErrorCode())) + { + return deployResult.ExtendedErrorCode(); + } + + Package package = GetSparsePackage(true); + if (package == NULL) + { + return S_FALSE; + } + + winrt::hstring familyName = package.Id().FamilyName(); + + deploymentOperation = packageManager.ProvisionPackageForAllUsersAsync(familyName); + deployResult = deploymentOperation.get(); + + if (!SUCCEEDED(deployResult.ExtendedErrorCode())) + { + return deployResult.ExtendedErrorCode(); + } + + return S_OK; +} + +HRESULT NppShell::Installer::RegisterSparsePackageCurrentUser() { if (::GetSystemMetrics(SM_CLEANBOOT) > 0) { @@ -245,7 +297,7 @@ HRESULT NppShell::Installer::UnregisterSparsePackage() PackageManager packageManager; IIterable packages; - Package package = GetSparsePackage(); + Package package = GetSparsePackage(true); if (package == NULL) { @@ -253,7 +305,7 @@ HRESULT NppShell::Installer::UnregisterSparsePackage() } winrt::hstring fullName = package.Id().FullName(); - auto deploymentOperation = packageManager.RemovePackageAsync(fullName, RemovalOptions::None); + auto deploymentOperation = packageManager.RemovePackageAsync(fullName, RemovalOptions::RemoveForAllUsers); auto deployResult = deploymentOperation.get(); if (!SUCCEEDED(deployResult.ExtendedErrorCode())) @@ -312,7 +364,8 @@ void ReRegisterSparsePackage() UnregisterSparsePackage(); // And then we register it again. - RegisterSparsePackage(); + RegisterSparsePackageAllUsers(); + RegisterSparsePackageCurrentUser(); } HRESULT NppShell::Installer::Install() @@ -393,13 +446,13 @@ void EnsureRegistrationOnCurrentUserWorker() winrt::init_apartment(); // Get the package to check if it is already installed for the current user. - Package existingPackage = GetSparsePackage(); + Package existingPackage = GetSparsePackage(false); if (existingPackage == NULL) { // The package is not installed for the current user - but we know that Notepad++ is. // If it wasn't, this code wouldn't be running, so it is safe to just register the package. - RegisterSparsePackage(); + RegisterSparsePackageCurrentUser(); // Finally we notify the shell that we have made changes, so it reloads the right click menu items. SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0); diff --git a/Installer.h b/Installer.h index 563d611..27e9f40 100644 --- a/Installer.h +++ b/Installer.h @@ -6,7 +6,8 @@ namespace NppShell::Installer HRESULT RegisterOldContextMenu(); HRESULT UnregisterOldContextMenu(); - HRESULT RegisterSparsePackage(); + HRESULT RegisterSparsePackageAllUsers(); + HRESULT RegisterSparsePackageCurrentUser(); HRESULT UnregisterSparsePackage(); HRESULT Install();