1

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

David Fernández
  • 494
  • 1
  • 6
  • 22

2 Answers2

3

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • wow, it is exactly but i was looking for, thanks for the advice anyway, however is was using this because of http://www.withinwindows.com/2010/07/01/retrieving-aero-glass-base-color-for-opaque-surface-rendering/ the value can change depending whether transpareceny is enabled or not – David Fernández Nov 14 '10 at 15:32
  • See http://stackoverflow.com/questions/3560890/vista-7-how-to-get-glass-color for detailed examples showing `DwmGetColorizationColor` doesn't work as advertised. – Ian Boyd Jan 18 '11 at 18:04
2

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;
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • @Luffy: See my edit, and see http://blogs.msdn.com/b/ericlippert/archive/2009/03/19/representation-and-identity.aspx – SLaks Nov 14 '10 at 15:20