I've noticed that DWord
and QWord
values when written to the Registry supposed to be Signed Integers, not Unsigned. This code will throw an exception if value is UInt64 or UInt32:
registryKey.SetValue(name, value);
According to MSDN DWORD
is a 32-bit unsigned integer (range: 0 through 4294967295 decimal) https://msdn.microsoft.com/en-us/library/cc230318.aspx
So, to write new DWORD
value to the Registry I need to cast it to signed integer like so:
UInt32 unsignedValue = (UInt32)someValue;
Int32 signedValue = (Int32)unsignedValue;
registryKey.SetValue(name, signedValue);
Passing unsigned value to SetValue method will throw an exception. Am I missing something or I just being retarded?
For historical reasons, the .NET API/libraries is normally "signed" instead of being "signed + unsigned".
But in the end, a
signed int
and aunsigned int
both occupy the same memory space, and there is no special handling done for negative values. So you can do as you said: cast the unsigned value to signed, write it withSetValue
and then if you look at the value inRegedit
you'll see that it was written "unsigned".Note that if your program is compiled in "checked" mode, a more correct code would be:
Because in "checked" mode casting between
int
anduint
can throw an exception if the conversion isn't possible.Note that as written here:
Clearly you'll have to do the same handling for signed-unsigned.