Skip to content

Fix preview handlers failing when PowerToys is installed in AppData\Local directory#27

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/fix-26
Draft

Fix preview handlers failing when PowerToys is installed in AppData\Local directory#27
Copilot wants to merge 4 commits intomainfrom
copilot/fix-26

Conversation

Copy link

Copilot AI commented Jun 29, 2025

Summary

This PR fixes a critical issue where PowerToys preview handlers (SVG, Markdown, PDF, etc.) fail to work when PowerToys is installed in C:\Users\[username]\AppData\Local\PowerToys or its subdirectories, while working correctly in other installation locations.

Problem

Users reported that preview handlers don't function when PowerToys is installed in AppData\Local paths:

  • C:\Users\leilzh\AppData\Local\PowerToys - doesn't work
  • C:\Users\leilzh\AppData\Local\PowerToys\test - doesn't work
  • ✅ Other locations (copied elsewhere) - works fine

This affects all preview handler types and prevents file previews in Windows Explorer.

Root Cause

The issue stems from Windows COM registration and activation context problems specific to AppData\Local directories:

  1. Path Format Issues: DLL paths with spaces weren't properly quoted in registry entries
  2. COM Activation Context: Per-user COM servers in AppData\Local require additional AppID configuration for proper activation by system processes like prevhost.exe
  3. Security Context: Preview handlers run under prevhost.exe which has different security permissions when accessing user directories

Solution

1. Enhanced Path Handling

  • Added normalizePathForComRegistration() to resolve and normalize DLL paths using std::filesystem::canonical
  • Added quotePathIfNeeded() to properly quote paths containing spaces
  • Applied to all preview handler registrations and Registry Preview functions

2. AppID Configuration for Per-User Installations

For AppData\Local installations, added critical COM AppID registry entries:

// HKEY_CURRENT_USER\Software\Classes\AppID\{6d2b5079-2f0b-48dd-ab7f-97cec514d30b}
changes.push_back({ scope, appIdPath, L"DllSurrogate", L"" }); // Use prevhost.exe surrogate
changes.push_back({ scope, appIdPath, L"AppIDFlags", DWORD(0x4) }); // APPIDREGFLAGS_ACTIVATE_IUSERVER_INDESKTOP  
changes.push_back({ scope, appIdPath, L"AuthenticationLevel", DWORD(1) }); // RPC_C_AUTHN_LEVEL_NONE

3. Debug Support

  • Added logging to track path transformations: original_path → normalized_path
  • Added logging for per-user AppID configuration creation
  • Graceful fallback when path canonicalization fails during installation

Files Changed

  • src/common/utils/registry.h - Core COM registration with AppID configuration
  • src/common/utils/modulesRegistry.h - Updated all preview handler change sets

Testing

Path Scenarios Tested:

  • Paths with spaces: C:\Program Files\PowerToys\*.dll"C:\Program Files\PowerToys\*.dll"
  • AppData paths: C:\Users\user\AppData\Local\PowerToys\*.dll → normalized + AppID config
  • Relative paths: Resolved using std::filesystem::canonical
  • Non-existent files: Fallback behavior during installation

Registry Validation:

  • DWORD values compile correctly
  • String escaping works properly
  • AppID entries created only for per-user installations

Expected Impact

  • AppData\Local installations work: Preview handlers activate correctly from user directories
  • Backward compatibility: Existing installations in other locations remain unaffected
  • Enhanced reliability: Normalized paths reduce COM activation failures
  • Better debugging: Logs help identify any remaining activation issues

Registry Structure Created

HKEY_CURRENT_USER\Software\Classes\CLSID\{handler-guid}\
├── InprocServer32\(Default) = "C:\Users\user\AppData\Local\PowerToys\Handler.dll"
└── AppID = "{6d2b5079-2f0b-48dd-ab7f-97cec514d30b}"

HKEY_CURRENT_USER\Software\Classes\AppID\{6d2b5079-2f0b-48dd-ab7f-97cec514d30b}\
├── DllSurrogate = ""
├── AppIDFlags = 4  
└── AuthenticationLevel = 1

This comprehensive fix addresses the core COM activation and security context issues while maintaining full compatibility with existing installations.

Fixes #26.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 3 commits June 29, 2025 10:12
Co-authored-by: lei9444 <39758135+lei9444@users.noreply.github.com>
…tion issues

Co-authored-by: lei9444 <39758135+lei9444@users.noreply.github.com>
…ndler issues

Co-authored-by: lei9444 <39758135+lei9444@users.noreply.github.com>
Copilot AI changed the title [WIP] preview pane里面的preview handler当安装在C:\Users\leilzh\AppData\Local\PowerToys这个目录下打不开 Fix preview handlers failing when PowerToys is installed in AppData\Local directory Jun 29, 2025
Copilot AI requested a review from lei9444 June 29, 2025 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

preview pane里面的preview handler当安装在C:\Users\leilzh\AppData\Local\PowerToys这个目录下打不开

2 participants