Question

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 :(

Was it helpful?

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.

OTHER TIPS

You need to make an unchecked cast:

unchecked {
    value = (uint)intValue;
}

EDIT: Registry.GetValue returns an object containing a boxed Int32 value.
You cannot unbox the value and cast to a different value type in a single cast.

When casting directly from object, you need to first unbox it to its actual type, then cast it to uint:

unchecked {
    value = (uint)(int)boxedObject;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top