d2phap/ImageGlass · error · Exception

Cannot open registry key: {key}

Error message

Cannot open registry key: {key}

What it means

Thrown inside the legacy v9 DesktopApi.SetWallpaper when Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", writable:true) returns null (no write access, or key missing). Unlike the v10 Win32 path this method catches the exception and returns it as the result (signature Exception?), so callers receive the error object rather than seeing it propagate. The message carries the same interpolated-null defect as the v10 version: '{key}' is always null at the throw point.

Source

Thrown at v9/Components/ImageGlass.Base/WinApi/DesktopApi.cs:54


public static partial class DesktopApi
{
    /// <summary>
    /// Set the desktop wallpaper.
    /// </summary>
    /// <param name="bmpPath">BMP image file path</param>
    /// <param name="style">Style of wallpaper</param>
    /// <returns>Success/failure indication.</returns>
    public static unsafe Exception? SetWallpaper(string bmpPath, WallpaperStyle style)
    {
        try
        {
            using var key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);

            if (key == null)
            {
                throw new Exception($"Cannot open registry key: {key}");
            }

            var tileVal = "0"; // default not-tiled
            var winStyle = "1"; // default centered

            switch (style)
            {
                case WallpaperStyle.Tiled:
                    tileVal = "1";
                    break;

                case WallpaperStyle.Stretched:
                    winStyle = "2";
                    break;

                case WallpaperStyle.Current:
                    tileVal = key.GetValue("TileWallpaper")?.ToString() ?? "";
                    winStyle = key.GetValue("WallpaperStyle")?.ToString() ?? "";

View on GitHub (pinned to 4a3c4fecef)

Solutions

  1. Inspect the returned Exception: if non-null, report 'cannot set wallpaper (registry access denied)' and run as a user with HKCU write access.
  2. Verify the key with 'reg query "HKCU\Control Panel\Desktop"' and check the ACL grants the current user write.
  3. Request a policy/ACL exception, or use an alternative wallpaper mechanism that does not require writing HKCU.
  4. Fix the interpolated-null in the message so the returned error names the actual key path for diagnosis.

Example fix

// before
var ex = DesktopApi.SetWallpaper(bmpPath, style);
if (ex != null) throw ex;

// after: meaningful handling
var ex = DesktopApi.SetWallpaper(bmpPath, style);
if (ex != null)
    Log.Warn($"SetWallpaper failed (registry access): {ex.Message}");
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check HKCU\Control Panel\Desktop writability (legacy v9 path).
const string KeyPath = @"Control Panel\Desktop";
using var probe = Registry.CurrentUser.OpenSubKey(KeyPath, writable: true);
if (probe is null) return new InvalidOperationException($"No write access to HKCU\\{KeyPath}.");

Try / catch

var ex = DesktopApi.SetWallpaper(bmpPath, style);
if (ex is not null) Log.Warn($"Wallpaper not set: {ex.Message}");

Prevention

When it happens

Trigger: Calling DesktopApi.SetWallpaper on Windows from a context without write access to HKCU\Control Panel\Desktop (restricted user, kiosk, sandbox, group-policy lock, or a cleaned/locked registry). The returned Exception is then inspected/logged by the caller.

Common situations: MDM-locked enterprise machines; running as a standard user where the registry ACL denies writes; a registry security tool that revoked permissions; corrupted user hive.

Related errors


AI-assisted analysis of d2phap/ImageGlass@4a3c4fecef (2026-08-13). Data as JSON: /api/errors/a45250c7072b3a97. Report an issue: GitHub.