xM4ddy/OFGB · critical · InvalidOperationException

OFGB: Failed to create subkey during initialization!

Error message

OFGB: Failed to create subkey during initialization!

What it means

OFGB (One Free Gadget's Buddy, a Windows telemetry-debloat utility) throws this InvalidOperationException in CreateKey (MainWindow.xaml.cs:112) when it cannot open or create the required HKCU registry subkey during window initialization. In the source, keyRef is only null when both OpenSubKey(loc, true) and CreateSubKey(loc) fail; note that in practice CreateSubKey throws RegistryException rather than returning null, so this guard is a defensive invariant check, but when it fires the app cannot read/write the Windows privacy settings it manages and aborts startup after showing a fatal-error MessageBox. It means the app cannot touch HKEY_CURRENT_USER, so none of the telemetry toggles can be initialized or applied.

Solutions

  1. Run the app on Windows as the interactive user whose HKCU hive is loaded (not as SYSTEM/scheduled task without a user profile) so Registry.CurrentUser maps to a real hive.
  2. Verify registry access manually: in regedit or `reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager"` confirm the path exists and is writable; if permissions are wrong, fix ACLs on HKCU\Software or recreate the user profile.
  3. Check that no policy, antivirus, or AppLocker rule blocks writes under HKCU\Software\Policies\Microsoft and ContentDeliveryManager; temporarily disable AV filtering to test.
  4. If running on non-Windows (cross-platform .NET), guard with OperatingSystem.IsWindows() before calling CreateKey, since Microsoft.Win32.Registry is Windows-only and fails otherwise.
  5. Wrap CreateSubKey calls in try/catch (UnauthorizedAccessException / SecurityException / IOException) and surface a clearer message instead of the generic null-check throw.

Example fix

// before
keyRef = Registry.CurrentUser.CreateSubKey(loc);
keyRef.SetValue(key, 0);
if (keyRef is null)
{
    throw new InvalidOperationException("OFGB: Failed to create subkey during initialization!");
}
// after
if (!OperatingSystem.IsWindows())
{
    MessageBox.Show("OFGB only supports Windows.", "OFGB: Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error);
    throw new PlatformNotSupportedException("OFGB requires Windows registry access.");
}
try
{
    keyRef = Registry.CurrentUser.OpenSubKey(loc, true) ?? Registry.CurrentUser.CreateSubKey(loc);
    keyRef.SetValue(key, 0);
}
catch (Exception ex) when (ex is UnauthorizedAccessException or SecurityException or IOException)
{
    MessageBox.Show($"Cannot access HKCU\\{loc}: {ex.Message}", "OFGB: Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error);
    throw new InvalidOperationException("OFGB: Failed to create subkey during initialization!", ex);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-check before calling CreateKey
bool canWrite = OperatingSystem.IsWindows();
if (canWrite)
{
    try
    {
        using var probe = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\ContentDeliveryManager", writable: true);
        canWrite = probe is not null;
    }
    catch { canWrite = false; }
}
if (!canWrite) { /* show fatal message / abort init */ }

Type guard

// Narrow the nullable RegistryKey before use
static bool TryOpenWritableKey(string loc, out RegistryKey key)
{
    var k = Registry.CurrentUser.OpenSubKey(loc, true) ?? Registry.CurrentUser.CreateSubKey(loc);
    if (k is null) { key = null!; return false; }
    key = k; return true;
}

Try / catch

try
{
    bool value = CreateKey(loc, key);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("Failed to create subkey"))
{
    // Registry unavailable: degrade gracefully — disable the UI toggles
    // and log ex.Message instead of crashing the app.
}
catch (UnauthorizedAccessException ex)
{
    // Hint the user to run as the regular interactive user / fix HKCU ACLs.
}

Prevention

When it happens

Trigger: CreateKey(loc, key) is called for each telemetry setting (key1..key6 include ContentDeliveryManager, UserProfileEngagement, AdvertisingInfo, Privacy, Explorer\Advanced, Notifications\Settings paths); the error surfaces when Registry.CurrentUser.OpenSubKey(loc, true) returns null AND Registry.CurrentUser.CreateSubKey(loc) yields no usable key — i.e., HKCU access is unavailable or the key path cannot be created/opened with write access.

Common situations: Running OFGB on a non-Windows platform (e.g., .NET on Linux/macOS where Microsoft.Win32.Registry throws or registry APIs are unavailable); running with a corrupted or locked HKCU hive; the app launched in a restricted sandbox/containers or with group policy restrictions on HKCU\Software; antivirus or registry permissions blocking writes; a race/TOCTOU where the key is deleted between OpenSubKey and use; a disposed CurrentUser base key in odd hosting scenarios.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.


AI-assisted analysis of xM4ddy/OFGB@a2f3ecf6a5 (2026-09-14). Data as JSON: /api/errors/29dc8f4ce4ad0f1c. Report an issue: GitHub.

Appendix: source

Thrown at MainWindow.xaml.cs:112

        private static bool CreateKey(string loc, string key)
        {
            RegistryKey? keyRef;
            bool value;

            if (Registry.CurrentUser.OpenSubKey(loc, true) is not null)
            {
                keyRef = Registry.CurrentUser.OpenSubKey(loc, true);
            }
            else
            {
                keyRef = Registry.CurrentUser.CreateSubKey(loc);
                keyRef.SetValue(key, 0);
            }

            if (keyRef is null)
            {
                MessageBox.Show("Failed to create a registry subkey during initialization!", "OFGB: Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error);
                throw new InvalidOperationException("OFGB: Failed to create subkey during initialization!");
            }

            value = Convert.ToBoolean(keyRef.GetValue(key));
            keyRef.Close();

            return value;
        }

        private static void ToggleOptions(string checkboxName, bool enable)
        {
            switch (checkboxName)
            {
                case "cb1":
                    Registry.SetValue("HKEY_CURRENT_USER\\" + cur_ver + "Explorer\\Advanced\\", "ShowSyncProviderNotifications", Convert.ToInt32(!enable));
                    break;
                case "cb2":
                    Registry.SetValue("HKEY_CURRENT_USER\\" + cur_ver + "ContentDeliveryManager", "RotatingLockScreenOverlayEnabled", Convert.ToInt32(!enable));
                    Registry.SetValue("HKEY_CURRENT_USER\\" + cur_ver + "ContentDeliveryManager", "SubscribedContent-338387Enabled", Convert.ToInt32(!enable));

View on GitHub (pinned to a2f3ecf6a5)