开发者

Preventing Registry.GetValue overflow

I'm trying to get the DWM colorizationColor using: Microsoft.Win32.R开发者_高级运维egistry.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 :(


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.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜