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);
I get an exception ("The type of the value object did not match the specified RegistryValueKind or the object could not be properly converted").
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?
You can also change the base of a value by using the Registry Editor. After you select the subkey containing the value you want to change, use the Registry Editor commands. On the Edit menu, click New, then point to DWORD Value. Click Modify, then use the buttons in the Edit DWORD Value dialog box.
A DWORD is a 32-bit unsigned integer (range: 0 through 4294967295 decimal).
DWORD (32-bit) Values & QWORD (64-bit) Values This means that you can have both types of registry values on both 32-bit and 64-bit operating systems. In this context, a "word" means 16 bits. DWORD, then, means "double-word," or 32 bits (16 X 2). Following this logic, QWORD means "quad-word," or 64 bits (16 X 4).
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);
I agree it's a bit odd, when you consider that DWORD is normally unsigned (IIRC) but it's at least worth a try...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With