如何把一个DWORD在注册表中最高位设置 [英] How to put a DWORD in the registry with the highest bit set

查看:355
本文介绍了如何把一个DWORD在注册表中最高位设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到一个奇怪的问题:从我的C#应用​​程序设置在Windows注册表中的DWORD类型的值的时候,我一直在最高位被置收到错误信息。 。显然,似乎有某种符号和无符号整数之间的转换问题。

I've run into a strange problem: when setting values of the DWORD type in the Windows Registry from my C# application, I keep getting errors when the highest bit is set. Apparently there seems to be some kind of conversion problem between signed and unsigned integers.

例如:当我做这样的事情。

Example: when I do something like this

regKey.SetValue("Value", 0x70000000u, RegistryValueKind.DWord);



它工作正常。但是,当我添加的最高位(其中,因为我特别是与无符号整数处理,应该只是一个价值位),像这样

it works fine. But when I add the highest bit (which, since I'm specifically dealing with unsigned integers, should be just another value bit), like this

regKey.SetValue("Value", 0xf0000000u, RegistryValueKind.DWord);



我得到一个异常(以下简称类型的值对象的不匹配指定RegistryValueKind或对象不能正确转换)。

I get an exception ("The type of the value object did not match the specified RegistryValueKind or the object could not be properly converted").

但不应该工作? DWORD是一个32位无符号整数数据类型,所以是 0xf0000000u 文字(C#自动为其分配UInt32的数据类型),所以他们应该是一个完美的比赛(和设置在注册表编辑器中手动值设置为0xf0000000做工精细,太)。这是在.NET中的错误还是我做错了什么?

But shouldn't it work? DWORD is an unsigned 32-bit integer data type, and so is the 0xf0000000u literal (C# automatically assigns it the UInt32 datatype), so they should be a perfect match (and setting the value manually in the registry editor to "0xf0000000" works fine, too). Is this a bug in .NET or am I doing something wrong?

推荐答案

我的猜测的是,你需要使用一个签署 INT 来代替。因此,只要将其转换是这样的:

My guess is that you need to use a signed int instead. So just convert it like this:

regKey.SetValue("Value", unchecked((int) 0xf0000000u),
                RegistryValueKind.DWord);



我同意这是一个有些奇怪,当你考虑到DWORD通常是无符号(IIRC),但它在至少值得一试...

I agree it's a bit odd, when you consider that DWORD is normally unsigned (IIRC) but it's at least worth a try...

这篇关于如何把一个DWORD在注册表中最高位设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆