InvalidCastException when converting from object to int

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!
I am using the following piece of code to get registry data from the device:

string keyName = "ControlPanel\\Owner";
string valueName = "PowrPass";
int powrPass = 0;
RegistryKey passwordRegKey = Registry.CurrentUser.OpenSubKey(keyName);
if (passwordRegKey.ValueCount == 0)
{
MessageBox.Show("Unknown");
}
else
{
powrPass = (int) passwordRegKey.GetValue(valueName); //Exception raised
here
MessageBox.Show(powrPass.ToString());
}
passwordRegKey.Close();

When I run the app, I get an InvalidCastException. How can I resolve this
issue?
Thanks for your time.

Regards,
Kumar
 
Thanks for responding.
That didn't do any good. I am still getting the same exception. Any other
ideas?!

Regards,
Kumar
 
why are you converting the result of the GetValue call to an int?

why not just do this...
MessageBox.Show(Registry.CurrentUser.OpenSubKey(keyName).ToString());

-Darren
 
Are you saying that
powrPass = Convert.ToInt32(passwordRegKey.GetValue(valueName));
produces an invalid cast exception?
What is the actual value of the registry string?
What is the value type?
Try assigning passwordRegKey.GetValue(valueName) to an Object variable and
see in the debugger what is its actual type
 
Alex:
Yes, powrPass = Convert.ToInt32(passwordRegKey.GetValue(valueName));
produces an invalid cast exception?

After assigning passwordRegKey.GetValue(valueName) to an Object variable I
got a value of 1 as the device had a 4-digit password enabled. The type was
System.Byte[]. Hmm.. I was trying to convert a Byte array to an int! Now, how
can I get this to work?

Darren:
Using MessageBox.Show(Registry.CurrentUser.OpenSubKey(keyName).ToString());
I will only get the path HKCU\ControlPanel\Owner[0xE9c80]. What I need to
find out is the data in "PowrPass" value under "Owner" key. Once I get that
data, I need to check if the password on the device is enabled or not
depending on the values I get [0,1,2,3]

Thank you for your time.
Regards,
Kumar
 
Raj Kumar said:
Alex:
Yes, powrPass = Convert.ToInt32(passwordRegKey.GetValue(valueName));
produces an invalid cast exception?

After assigning passwordRegKey.GetValue(valueName) to an Object variable I
got a value of 1 as the device had a 4-digit password enabled. The type was
System.Byte[]. Hmm.. I was trying to convert a Byte array to an int! Now, how
can I get this to work?

That sounds like the registry value is a binary rather than a dword.
Darren:
Using MessageBox.Show(Registry.CurrentUser.OpenSubKey(keyName).ToString());
I will only get the path HKCU\ControlPanel\Owner[0xE9c80]. What I need to
find out is the data in "PowrPass" value under "Owner" key. Once I get that
data, I need to check if the password on the device is enabled or not
depending on the values I get [0,1,2,3]

Looking at the registry on my device, that value is indeed a binary
value. Just use its first element.
 
Jon:
How do I use the first element and still get the values I need? I am not
sure I understood :-(

Regards,
Kumar

Jon Skeet said:
Raj Kumar said:
Alex:
Yes, powrPass = Convert.ToInt32(passwordRegKey.GetValue(valueName));
produces an invalid cast exception?

After assigning passwordRegKey.GetValue(valueName) to an Object variable I
got a value of 1 as the device had a 4-digit password enabled. The type was
System.Byte[]. Hmm.. I was trying to convert a Byte array to an int! Now, how
can I get this to work?

That sounds like the registry value is a binary rather than a dword.
Darren:
Using MessageBox.Show(Registry.CurrentUser.OpenSubKey(keyName).ToString());
I will only get the path HKCU\ControlPanel\Owner[0xE9c80]. What I need to
find out is the data in "PowrPass" value under "Owner" key. Once I get that
data, I need to check if the password on the device is enabled or not
depending on the values I get [0,1,2,3]

Looking at the registry on my device, that value is indeed a binary
value. Just use its first element.
 
Should have spent a little more time working on this. Here's what I did:

powrPassData = (int) ( ( (byte[] ) passwordRegKey.GetValue( valueName ) )[0]
);

and it works. If this is not a right way of doing, then please suggest
another approach.

Thanks for your time.
Regards,
Kumar

Jon Skeet said:
Raj Kumar said:
Alex:
Yes, powrPass = Convert.ToInt32(passwordRegKey.GetValue(valueName));
produces an invalid cast exception?

After assigning passwordRegKey.GetValue(valueName) to an Object variable I
got a value of 1 as the device had a 4-digit password enabled. The type was
System.Byte[]. Hmm.. I was trying to convert a Byte array to an int! Now, how
can I get this to work?

That sounds like the registry value is a binary rather than a dword.
Darren:
Using MessageBox.Show(Registry.CurrentUser.OpenSubKey(keyName).ToString());
I will only get the path HKCU\ControlPanel\Owner[0xE9c80]. What I need to
find out is the data in "PowrPass" value under "Owner" key. Once I get that
data, I need to check if the password on the device is enabled or not
depending on the values I get [0,1,2,3]

Looking at the registry on my device, that value is indeed a binary
value. Just use its first element.
 
Raj Kumar said:
Should have spent a little more time working on this. Here's what I did:

powrPassData = (int) ( ( (byte[] ) passwordRegKey.GetValue( valueName ) )[0]
);

and it works. If this is not a right way of doing, then please suggest
another approach.

That's fine. I'd split it up into a couple of lines, and get rid of the
unnecessary explicit cast to int, just for readability:

byte[] powrPassDataBinary = (byte[])passwordRegKey.GetValue(valueName);
powrPassData = powrPassDataBinary[0];
 
Back
Top