Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a DWORD in the registry with the highest bit set

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?

like image 505
Andreas Baus Avatar asked Jul 07 '11 09:07

Andreas Baus


People also ask

How do I change the DWORD value in registry?

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.

What is a 32-bit DWORD?

A DWORD is a 32-bit unsigned integer (range: 0 through 4294967295 decimal).

What is DWORD 32-bit and QWORD 64 bit?

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).


1 Answers

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...

like image 79
Jon Skeet Avatar answered Oct 26 '22 02:10

Jon Skeet