Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DWORD and QWORD - signed and unsigned madness

Tags:

c#

registry

dword

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?

like image 496
Alex K Avatar asked Jun 09 '15 09:06

Alex K


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

For historical reasons, the .NET API/libraries is normally "signed" instead of being "signed + unsigned".

But in the end, a signed int and a unsigned 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 with SetValue and then if you look at the value in Regedit you'll see that it was written "unsigned".

Note that if your program is compiled in "checked" mode, a more correct code would be:

uint unsignedValue = ... // Your original value

int signedValue = unchecked((int)unsignedValue);
registryKey.SetValue(name, signedValue);

Because in "checked" mode casting between int and uint can throw an exception if the conversion isn't possible.

Note that as written here:

This overload of SetValue stores 64-bit integers as strings (RegistryValueKind.String). To store 64-bit numbers as RegistryValueKind.QWord values, use the SetValue(String, Object, RegistryValueKind) overload that specifies RegistryValueKind.

Clearly you'll have to do the same handling for signed-unsigned.

like image 132
xanatos Avatar answered Sep 17 '22 06:09

xanatos


From the RegistryKey.SetValue page' example:

// Numeric values that cannot be interpreted as DWord (int) values
// are stored as strings.

It seems the stored values are signed ints or strings.

like image 45
Zhertal Avatar answered Sep 20 '22 06:09

Zhertal