Search code examples
c#registrydwm

Preventing Registry.GetValue overflow


I'm trying to get the DWM colorizationColor using: Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\DWM").GetValue("ColorizationColor")

however it is returning -2144154163 (the real value is 2150813133)

I thinks this is because the value cannot be hold on a 32-bit int... however event casting (or Converting) to int64 fails.

PD: It may sound like an easy question to answer but I cannot find a solution :(


Solution

  • Color values are pretty impractical as int values, it is best to convert it quickly. A little wrapper to dispose the key doesn't hurt either:

    using System.Drawing;
    ...
            public static Color GetDwmColorizationColor() {
                using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\DWM")) {
                    return Color.FromArgb((int)key.GetValue("ColorizationColor"));
                }
            }
    

    But don't do it this way, there's a documented API for it. P/Invoke DwmGetColorizationColor() to get the value, you'll get guaranteed compatibility behavior. Important if some future Windows version changes this registry details. Visit pinvoke.net for the declaration.