防止Registry.GetValue溢出

发布于 2024-10-02 13:22:09 字数 354 浏览 10 评论 0原文

我正在尝试使用以下方法获取 DWM colorizationColorMicrosoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\DWM").GetValue("ColorizationColor")

但是它返回 -2144154163 (真实值是2150813133

我认为这是因为该值无法保存在32位int上...但是事件转换(或转换)为int64失败。

PD:这听起来像是一个很容易回答的问题,但我找不到解决方案:(

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饭团 2024-10-09 13:22:09

颜色值作为 int 值非常不切实际,最好快速转换它。使用一个小包装来处理密钥也没有什么坏处:

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"));
            }
        }

但是不要这样做,有一个记录在案的 API。 P/Invoke DwmGetColorizationColor() 来获取值,您将获得有保证的兼容性行为。如果未来的某个 Windows 版本更改了此注册表详细信息,则很重要。请访问 pinvoke.net 查看声明。

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.

蓦然回首 2024-10-09 13:22:09

您需要进行未经检查的转换:

unchecked {
    value = (uint)intValue;
}

编辑Registry.GetValue返回一个包含装箱的Int32值的对象
您无法在一次转换中取消对值的装箱并转换为不同的值类型< /a>.

当直接从对象进行转换时,您需要首先将其拆箱为其实际类型,然后将其转换为uint

unchecked {
    value = (uint)(int)boxedObject;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文