6 Replies - 2647 Views - Last Post: 24 September 2012 - 01:54 PM Rate Topic: -----

#1 CoRiden  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 4
  • Joined: 24-September 12

Reading registry binary values

Posted 24 September 2012 - 08:28 AM

            string InstallPath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Bohemia Interactive Studio\ArmA 2 OA", "Key", null);
            if (InstallPath != null)
            {
                label1.Text = InstallPath;
            }
            else
            {
                label1.Text = "Missing.";
            }

            return;

Program starts, pressing button and CRASH.. Error is: Unable to cast object of type 'System.Byte[]' to type 'System.String'.
I don't know what im doing wrong.. I searched whole internet, and i can't find answer :S

Is This A Good Question/Topic? 0
  • +

Replies To: Reading registry binary values

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Reading registry binary values

Posted 24 September 2012 - 08:55 AM

The error is right there. It says that it can't cast a byte array to a string. So instead of casting to a string, cast to a byte array, and then read off the the path from the byte array, or use the Encoding.GetString() method to decode the bytes into a string, assuming you know what the byte encoding was.
http://msdn.microsof...y/744y86tc.aspx
Was This Post Helpful? 1
  • +
  • -

#3 CoRiden  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 4
  • Joined: 24-September 12

Re: Reading registry binary values

Posted 24 September 2012 - 09:05 AM

I failed.. Im beginner of C# and it's very hard to understand things..
I did something like this:
        public virtual string GetString(byte[] bytes);
        private void button1_Click(object sender, EventArgs e)
        {
            string lol = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Bohemia Interactive Studio\ArmA 2 OA", "Key", null);
            byte[] data = GetString(data, lol);
            string K = GetString(data, s);
            if (data != null)
            {
                label1.Text = data;
            }
            else
            {
                label1.Text = "Puudub";
            }

            return;
        }


Was This Post Helpful? -1
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Reading registry binary values

Posted 24 September 2012 - 09:08 AM

Did you even look at the code in the MSDN documentation?
Was This Post Helpful? 1
  • +
  • -

#5 CoRiden  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 4
  • Joined: 24-September 12

Re: Reading registry binary values

Posted 24 September 2012 - 09:33 AM

I don't get it :/
        private void button1_Click(object sender, EventArgs e)
        {
            const int MAX_BUFFER_SIZE = 2048;
            byte[] bytes = new Byte[MAX_BUFFER_SIZE];
            string k = GetString(bytes);
            if (k != null)
            {
                label1.Text = k;
            }
            else
            {
                label1.Text = "Puudub";
            }

            return;
        }



... where i need to put
Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Bohemia Interactive Studio\ArmA 2 OA", "Key", null);
? I started learning this programming language today.. So hope you understand why im such a dumbass..
Was This Post Helpful? 0
  • +
  • -

#6 CoRiden  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 4
  • Joined: 24-September 12

Re: Reading registry binary values

Posted 24 September 2012 - 10:38 AM

Btw, i got this working without errors, but output is wrong..
        private void button1_Click(object sender, EventArgs e)
        {
            const int MAX_BUFFER_SIZE = 2048;
            byte[] bytes = new Byte[MAX_BUFFER_SIZE];
            bytes = (byte[])Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Bohemia Interactive Studio\ArmA 2 OA", "Key", bytes);
            string k = System.Text.UTF8Encoding.UTF8.GetString(bytes);
            if (k != null)
            {
                label1.Text = k;
            }
            else
            {
                label1.Text = "Puudub";
            }

            return;
        }


Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Reading registry binary values

Posted 24 September 2012 - 01:54 PM

That would indicate that tho the "Key" registry value is not encoded as UTF8. This is why I said "assuming you know what the byte encoding was".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1