bug found in Microsoft.Win32.RegistryValueKind.DWord conversion

G

Guest

I have found what I can only conclude to be a bug in .net after days of
messing around. I am trying to assign a large value to a registry dword
entry close to it's limit of 4294967295 or 0xFFFFFFFF

I have tried making the call through vb.net and c# and it produces the same
result. I am assigning the value like so:

uint myVar = 4294967295
Microsoft.Win32.Registry.LocalMachine.SetValue(VideoSettingsKey &
"RecordingSoftPostPadding",myVar,Microsoft.Win32.RegistryValueKind.DWord)

which fails with a conversion failure, I SHOULD be able to assign a uint
value though. I can successfully assign an int value (smaller of course)
without a problem, but int does not cover the range of values which can be
contained in a regisry dword, only a uint can cover that range.
 
N

Nick Hounsome

Ben Wilson said:
I have found what I can only conclude to be a bug in .net after days of
messing around. I am trying to assign a large value to a registry dword
entry close to it's limit of 4294967295 or 0xFFFFFFFF

I have tried making the call through vb.net and c# and it produces the
same
result. I am assigning the value like so:

uint myVar = 4294967295
Microsoft.Win32.Registry.LocalMachine.SetValue(VideoSettingsKey &
"RecordingSoftPostPadding",myVar,Microsoft.Win32.RegistryValueKind.DWord)

which fails with a conversion failure, I SHOULD be able to assign a uint
value though. I can successfully assign an int value (smaller of course)
without a problem, but int does not cover the range of values which can be
contained in a regisry dword, only a uint can cover that range.

I agree it looks like a bug.
The work around is easy - use an int:

int myIVar = unchecked((int)myVar);

This will give the right value in the registry.
 
G

Guest

not sure I follow that code example you gave me, could you show me how you
would implement that in my sample code?

glad to see someone agrees it's a bug, was banging my head on a wall trying
to figure it out!
 
S

Stephany Young

I'm not convinced it's a bug.

Which statement fails, the assignment or the method call?

If it's the assignment, I've come across a similar gotcha in VB.NET. The
literal is treated as an Integer (Int32) and the value of the literal is too
big for an Integer so kaboom.

In VB.NET, one can specify that the literal is a UInt32 by writing it as:

Dim myVar As UInteger = 4294967295UI
 
G

Guest

Hello Ben,

You are trying to store an Int32 that is grater than Int32.MaxValue.
The registry do not store UInt32 values; you have to convert it before writing and after reading from the registry to a supported type.
For a list of supported types, see:
RegistryValueKind Enumeration
http://msdn2.microsoft.com/en-us/library/microsoft.win32.registryvaluekind(VS.80).aspx

Regards.


"Ben Wilson" <[email protected]> escribió en el mensaje |I have found what I can only conclude to be a bug in .net after days of
| messing around. I am trying to assign a large value to a registry dword
| entry close to it's limit of 4294967295 or 0xFFFFFFFF
|
| I have tried making the call through vb.net and c# and it produces the same
| result. I am assigning the value like so:
|
| uint myVar = 4294967295
| Microsoft.Win32.Registry.LocalMachine.SetValue(VideoSettingsKey &
| "RecordingSoftPostPadding",myVar,Microsoft.Win32.RegistryValueKind.DWord)
|
| which fails with a conversion failure, I SHOULD be able to assign a uint
| value though. I can successfully assign an int value (smaller of course)
| without a problem, but int does not cover the range of values which can be
| contained in a regisry dword, only a uint can cover that range.
 
G

Guest

The registry DWORD can store values up to 0xFFFFFFFF or 4294967295, yet there
is no way to assign this, and this is the bug.

Using an int or uint works, however it will not accept values higher than
0x7FFFFFFF or 2147483647, which is the maximum positive value of an int.

I believe it is impossible to to successfully assign a registry dword value
2147483647 programmaticlly through the Registry.LocalMachine.SetValue
method, and this is the bug, as you *should* be able to do this.
 
G

Guest

I think you shouldn't be able, as long as UInt32 is not CLS compliant (it's only my opinion).
Why don't you use bitconverter to convert it to Int32 and then save it to the registry?

Regards.


"Ben Wilson" <[email protected]> escribió en el mensaje | The registry DWORD can store values up to 0xFFFFFFFF or 4294967295, yet there
| is no way to assign this, and this is the bug.
|
| Using an int or uint works, however it will not accept values higher than
| 0x7FFFFFFF or 2147483647, which is the maximum positive value of an int.
|
| I believe it is impossible to to successfully assign a registry dword value
| > 2147483647 programmaticlly through the Registry.LocalMachine.SetValue
| method, and this is the bug, as you *should* be able to do this.
|
| "José Manuel Agüero" wrote:
|
| > Hello Ben,
| >
| > You are trying to store an Int32 that is grater than Int32.MaxValue.
| > The registry do not store UInt32 values; you have to convert it before writing and after reading from the registry to a supported type.
| > For a list of supported types, see:
| > RegistryValueKind Enumeration
| > http://msdn2.microsoft.com/en-us/library/microsoft.win32.registryvaluekind(VS.80).aspx
| >
| > Regards.
| >
| >
| > "Ben Wilson" <[email protected]> escribió en el mensaje | > |I have found what I can only conclude to be a bug in .net after days of
| > | messing around. I am trying to assign a large value to a registry dword
| > | entry close to it's limit of 4294967295 or 0xFFFFFFFF
| > |
| > | I have tried making the call through vb.net and c# and it produces the same
| > | result. I am assigning the value like so:
| > |
| > | uint myVar = 4294967295
| > | Microsoft.Win32.Registry.LocalMachine.SetValue(VideoSettingsKey &
| > | "RecordingSoftPostPadding",myVar,Microsoft.Win32.RegistryValueKind.DWord)
| > |
| > | which fails with a conversion failure, I SHOULD be able to assign a uint
| > | value though. I can successfully assign an int value (smaller of course)
| > | without a problem, but int does not cover the range of values which can be
| > | contained in a regisry dword, only a uint can cover that range.
| >
 
N

Nick Hounsome

uint 4294967295

has exactly the same binary representation as

int -1

so just convert it to int without checking and it will hopefully give you
the value that you want in the registry.

I don't understand the argument that uint is not CLS compliant.
Only the interface needs to be CLS compliant and that takes Object so there
is no problem.

The registry is undoubtedly unsigned - just go into regedit and try to
enter -1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top