Skip to content

RegNotifyChangeKeyValue is not working in WinUI3 app #4075

Open
@HO-COOH

Description

@HO-COOH

Describe the bug

Following #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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions