microsoft / microsoft/WindowsAppSDK

RegNotifyChangeKeyValue is not working in WinUI3 app

Open
#4,075 36 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area-External needs-triage
Dominant language
C++
Stars
4.7k
Forks
471
Avg merge
3d 13h
Merged PRs (30d)
28

Description

Describe the bug

Following microsoft/WindowsAppSDK#4006, I tried to monitor HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize registry for light/dark theme listener, but RegNotifyChangeKeyValue does not seem to work in a packaged winui3 app.

The same code works in a console application project

Steps to reproduce the bug
  1. Create a C++ winui3 app (packaged)
  2. Use the following code, in MainWindow constructor
std::thread{
                [] {
                    auto handle = CreateEvent(NULL, true, false, nullptr);
                    HKEY key{};
                    RegOpenKeyEx(HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\CurrentVersion\Themes\Personalize)", 0, KEY_READ, &key);
                    while (true)
                    {
                        auto hr = RegNotifyChangeKeyValue(
                            key,
                            true,
                            REG_NOTIFY_CHANGE_NAME |
                            REG_NOTIFY_CHANGE_ATTRIBUTES |
                            REG_NOTIFY_CHANGE_LAST_SET |
                            REG_NOTIFY_CHANGE_SECURITY,
                            handle,
                            true
                        );
                        if (WaitForSingleObject(handle, INFINITE) != WAIT_FAILED)
                        {
                            MessageBox(NULL, L"changed", L"", 0);
                            OutputDebugString(L"Triggered\n");
                        }
                    }

                    //while (true)
                    //{
                    //    DWORD value{};
                    //    DWORD bytes = sizeof(value);
                    //    auto hr = RegGetValue(key, nullptr, LR"(AppsUseLightTheme)", RRF_RT_DWORD, nullptr, &value, &bytes);
                    //    OutputDebugString(std::format(L"hr: {}, value: {}, cb: {}\n", hr, value, bytes).data());
                    //    std::this_thread::sleep_for(std::chrono::seconds{ 1 });
                    //}
                }
            }.detach();
  1. Build and run the app, put a breakpoint on the OutputDebugString line. Toggle light/dark mode in system settings, the breakpoint is not hit.
  2. Use the same code in a console application project, it will break.

Bonus: If you use the commented code to monitor the value, it will reads the correct changes of the value, suggesting there is something wrong internally.

Expected behavior

No response

Screenshots

No response

NuGet package version

Windows App SDK 1.4.3: 1.4.231115000

Packaging type

Packaged (MSIX)

Windows version

Windows 10 version 1809 (17763, October 2018 Update)

IDE

Visual Studio 2022

Additional context

No response

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the code from the MainWindow constructor in a packaged WinUI3 C++ app, monitoring the specified Personalize registry key, and compare it with the console application behavior. Start by tracing RegNotifyChangeKeyValue and the event wait, then verify the polling comparison shown in the issue. Done means identifying the packaged-app cause and providing a confirmed fix or documented limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
desktop, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.