Skip to content

Commit 931204a

Browse files
lei9444Copilot
andcommitted
fix: Context menu registry entries are not cleaned up when disabled via GPO (#41411)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request This PR fixes an issue where context menu runtime registration wasn't properly cleaned up when GPO (Group Policy Object) policies disabled the module. The problem occurred because the module constructor didn't consider GPO policies when determining its initial enabled state. <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #41387 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed This pull request refactors how context menu registration and unregistration are handled across several PowerToys modules. The main improvement is the introduction of a new `UpdateRegistration` helper method in each module, which centralizes and simplifies the logic for registering or unregistering context menus when the module is enabled or disabled. This reduces code duplication and ensures consistent behavior. **Context menu registration logic refactor:** * Added a private `UpdateRegistration` method to each of the following modules to handle context menu registration and unregistration based on the enabled state: - `FileLocksmithModule` in `PowerToysModule.cpp` - `NewModule` in `powertoys_module.cpp` - `ImageResizerModule` in `dllmain.cpp` - `PowerRenameModule` in `dllmain.cpp` * Replaced direct calls to registration/unregistration functions in `enable`, `disable`, and `init_settings` methods with calls to the new `UpdateRegistration` method in all affected modules, ensuring consistent and centralized handling [[1]](diffhunk://#diff-256ed936dafec1bf6ff17849b4797dd276f5b07bebe2e483bc1580c8f06e92d9L91-R122) [[2]](diffhunk://#diff-256ed936dafec1bf6ff17849b4797dd276f5b07bebe2e483bc1580c8f06e92d9R155) [[3]](diffhunk://#diff-4a3942d548f3daec02a833983ed9b2b69f75e2cd1b74a8ce1b874f3fd33fde55L101-R125) [[4]](diffhunk://#diff-4a3942d548f3daec02a833983ed9b2b69f75e2cd1b74a8ce1b874f3fd33fde55L153-R177) [[5]](diffhunk://#diff-0c0a89e812ff4625d165417da14f1c3f203e5ac7907555ae4fde122f3dddcf7aL115-L130) [[6]](diffhunk://#diff-34581ec47c37b0d2e1d9b59696225c47342930694e732db06cbdf653ceb2c2d7L205-R234) [[7]](diffhunk://#diff-34581ec47c37b0d2e1d9b59696225c47342930694e732db06cbdf653ceb2c2d7R334). These changes improve maintainability and reduce the risk of inconsistent registration behavior across modules. --------- Co-authored-by: Copilot <[email protected]>
1 parent aaded11 commit 931204a

File tree

4 files changed

+91
-35
lines changed

4 files changed

+91
-35
lines changed

src/modules/FileLocksmith/FileLocksmithExt/PowerToysModule.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@
1919

2020
class FileLocksmithModule : public PowertoyModuleIface
2121
{
22+
private:
23+
// Update registration based on enabled state
24+
void UpdateRegistration(bool enabled)
25+
{
26+
if (enabled)
27+
{
28+
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
29+
FileLocksmithRuntimeRegistration::EnsureRegistered();
30+
Logger::info(L"File Locksmith context menu registered");
31+
#endif
32+
}
33+
else
34+
{
35+
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
36+
FileLocksmithRuntimeRegistration::Unregister();
37+
Logger::info(L"File Locksmith context menu unregistered");
38+
#endif
39+
}
40+
}
41+
2242
public:
2343
FileLocksmithModule()
2444
{
@@ -88,21 +108,16 @@ class FileLocksmithModule : public PowertoyModuleIface
88108
package::RegisterSparsePackage(path, packageUri);
89109
}
90110
}
91-
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
92-
FileLocksmithRuntimeRegistration::EnsureRegistered();
93-
#endif
94111

95112
m_enabled = true;
113+
UpdateRegistration(m_enabled);
96114
}
97115

98116
virtual void disable() override
99117
{
100118
Logger::info(L"File Locksmith disabled");
101-
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
102-
FileLocksmithRuntimeRegistration::Unregister();
103-
Logger::info(L"File Locksmith context menu unregistered (Win10)");
104-
#endif
105119
m_enabled = false;
120+
UpdateRegistration(m_enabled);
106121
}
107122

108123
virtual bool is_enabled() override
@@ -135,6 +150,7 @@ class FileLocksmithModule : public PowertoyModuleIface
135150
{
136151
m_enabled = FileLocksmithSettingsInstance().GetEnabled();
137152
m_extended_only = FileLocksmithSettingsInstance().GetShowInExtendedContextMenu();
153+
UpdateRegistration(m_enabled);
138154
Trace::EnableFileLocksmith(m_enabled);
139155
}
140156

src/modules/NewPlus/NewShellExtensionContextMenu/powertoys_module.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@
2121
// Note: Settings are managed via Settings and UI Settings
2222
class NewModule : public PowertoyModuleIface
2323
{
24+
private:
25+
// Update registration based on enabled state
26+
void UpdateRegistration(bool enabled)
27+
{
28+
if (enabled)
29+
{
30+
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
31+
NewPlusRuntimeRegistration::EnsureRegisteredWin10();
32+
Logger::info(L"New+ context menu registered");
33+
#endif
34+
}
35+
else
36+
{
37+
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
38+
NewPlusRuntimeRegistration::Unregister();
39+
Logger::info(L"New+ context menu unregistered");
40+
#endif
41+
}
42+
}
43+
2444
public:
2545
NewModule()
2646
{
@@ -98,14 +118,9 @@ class NewModule : public PowertoyModuleIface
98118
{
99119
newplus::utilities::register_msix_package();
100120
}
101-
else
102-
{
103-
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
104-
NewPlusRuntimeRegistration::EnsureRegisteredWin10();
105-
#endif
106-
}
107121

108122
powertoy_new_enabled = true;
123+
UpdateRegistration(powertoy_new_enabled);
109124
}
110125

111126
virtual void disable() override
@@ -150,19 +165,14 @@ class NewModule : public PowertoyModuleIface
150165
{
151166
Trace::EventToggleOnOff(false);
152167
}
153-
if (!package::IsWin11OrGreater())
154-
{
155-
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
156-
NewPlusRuntimeRegistration::Unregister();
157-
Logger::info(L"New+ context menu unregistered (Win10)");
158-
#endif
159-
}
160168
powertoy_new_enabled = false;
169+
UpdateRegistration(powertoy_new_enabled);
161170
}
162171

163172
void init_settings()
164173
{
165174
powertoy_new_enabled = NewSettingsInstance().GetEnabled();
175+
UpdateRegistration(powertoy_new_enabled);
166176
}
167177
};
168178

src/modules/imageresizer/dll/dllmain.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,32 @@ class ImageResizerModule : public PowertoyModuleIface
4343
//contains the non localized key of the powertoy
4444
std::wstring app_key;
4545

46+
// Update registration based on enabled state
47+
void UpdateRegistration(bool enabled)
48+
{
49+
if (enabled)
50+
{
51+
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
52+
ImageResizerRuntimeRegistration::EnsureRegistered();
53+
Logger::info(L"ImageResizer context menu registered");
54+
#endif
55+
}
56+
else
57+
{
58+
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
59+
ImageResizerRuntimeRegistration::Unregister();
60+
Logger::info(L"ImageResizer context menu unregistered");
61+
#endif
62+
}
63+
}
64+
65+
4666
public:
4767
// Constructor
4868
ImageResizerModule()
4969
{
5070
m_enabled = CSettingsInstance().GetEnabled();
71+
UpdateRegistration(m_enabled);
5172
app_name = GET_RESOURCE_STRING(IDS_IMAGERESIZER);
5273
app_key = ImageResizerConstants::ModuleKey;
5374
LoggerHelpers::init_logger(app_key, L"ModuleInterface", LogSettings::imageResizerLoggerName);
@@ -112,22 +133,16 @@ class ImageResizerModule : public PowertoyModuleIface
112133
package::RegisterSparsePackage(path, packageUri);
113134
}
114135
}
115-
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
116-
ImageResizerRuntimeRegistration::EnsureRegistered();
117-
#endif
118-
136+
UpdateRegistration(m_enabled);
119137
Trace::EnableImageResizer(m_enabled);
120138
}
121139

122140
// Disable the powertoy
123141
virtual void disable()
124142
{
125143
m_enabled = false;
144+
UpdateRegistration(m_enabled);
126145
Trace::EnableImageResizer(m_enabled);
127-
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
128-
ImageResizerRuntimeRegistration::Unregister();
129-
Logger::info(L"ImageResizer context menu unregistered (Win10)");
130-
#endif
131146
}
132147

133148
// Returns if the powertoys is enabled

src/modules/powerrename/dll/dllmain.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,25 @@ class PowerRenameModule : public PowertoyModuleIface
168168
//contains the non localized key of the powertoy
169169
std::wstring app_key;
170170

171+
// Update registration based on enabled state
172+
void UpdateRegistration(bool enabled)
173+
{
174+
if (enabled)
175+
{
176+
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
177+
PowerRenameRuntimeRegistration::EnsureRegistered();
178+
Logger::info(L"PowerRename context menu registered");
179+
#endif
180+
}
181+
else
182+
{
183+
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
184+
PowerRenameRuntimeRegistration::Unregister();
185+
Logger::info(L"PowerRename context menu unregistered");
186+
#endif
187+
}
188+
}
189+
171190
public:
172191
// Return the localized display name of the powertoy
173192
virtual PCWSTR get_name() override
@@ -202,20 +221,15 @@ class PowerRenameModule : public PowertoyModuleIface
202221
package::RegisterSparsePackage(path, packageUri);
203222
}
204223
}
205-
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
206-
PowerRenameRuntimeRegistration::EnsureRegistered();
207-
#endif
224+
UpdateRegistration(m_enabled);
208225
}
209226

210227
// Disable the powertoy
211228
virtual void disable()
212229
{
213230
m_enabled = false;
214231
Logger::info(L"PowerRename disabled");
215-
#if defined(ENABLE_REGISTRATION) || defined(NDEBUG)
216-
PowerRenameRuntimeRegistration::Unregister();
217-
Logger::info(L"PowerRename context menu unregistered (Win10)");
218-
#endif
232+
UpdateRegistration(m_enabled);
219233
}
220234

221235
// Returns if the powertoy is enabled
@@ -315,6 +329,7 @@ class PowerRenameModule : public PowertoyModuleIface
315329
void init_settings()
316330
{
317331
m_enabled = CSettingsInstance().GetEnabled();
332+
UpdateRegistration(m_enabled);
318333
Trace::EnablePowerRename(m_enabled);
319334
}
320335

0 commit comments

Comments
 (0)