Results 1 to 6 of 6

Thread: [2008] Writing binary data to the registry

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    [2008] Writing binary data to the registry

    so yeah, I am trying to create/overwrite the data in a registry key that is of type "REG_BINARY" and I cant quite figure out how to write the data in my program thats going to be written into it.

    Bit of background info, I'm making a small GUI that installs the remote control program 'VNC' on remote PCs. One part of the installation needs to write the password to connect to VNC on that machine to the registry. Now on my PC, where VNC is already installed, there is some data in the Password value that looks like this: DA 42 47 27 F9 14 98 and the type of the value is REG_BINARY. This value is encrypted by the VNC program when the user sets it, and I have a program that encrypts data using the same algorithm, but it outputs data in a txt file and this is what the txt file contains:

    \registry\machine\software\orl\winvnc3\default
    Password = REG_BINARY 0x00000008 0x0a6d14c7 0x081e2e08

    So does anyone know how I can convert and write that data into the type of data that the registry type REG_BINARY is expecting? Sorry if this isnt really a VB.NET related question, I have trawled the internet for hours but dont really know what to search for so if someone could even just point me in the correct direction I would be very grateful
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Writing binary data to the registry

    You have to supply a Byte array, e.g.
    vb.net Code:
    1. Dim data As Byte() = {&HDA, &H42, &H47, &H27, &HF9, &H14, &H98}
    To convert text to a Byte array you would use the Encoding.GetBytes method. I'm guessing that you'd use System.Text.Encoding.ASCII as the Encoding object, but it's possible that the program uses something different.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Writing binary data to the registry

    OK thanks, well that lets me write data to the reg_binary value no problem ,which is great but I still have a problem. If we go back to my example above, where I am trying to write data in the following format to the registry key: 0x00000008 0x0a6d14c7 0x081e2e08 then I find that writing this data does not produce the desired results.

    Here is the code I am using. It reads text in from the text file and strips the first useless part of the text out, then writes the rest to the REG_BINARY key in question.
    Code:
    Dim fileContents As String = My.Computer.FileSystem.ReadAllText("C:\vnctemp\vncenctemp.txt")
    Dim binarystring As String = fileContents.Remove(0, 74)
    Dim binaryvalue() As Byte = System.Text.Encoding.ASCII.GetBytes(binarystring)
    Dim tempreg = My.Computer.Registry.LocalMachine.OpenSubKey("software\realvnc\winvnc4")
    My.Computer.Registry.SetValue(tempreg.ToString, "Password", binaryvalue, Microsoft.Win32.RegistryValueKind.Binary)
    So using this code, if the binarystring value (after its had some characters removed from the start of it) is this:
    0x00000008 0x57bf2d2e 0x9e6cb06e
    but the registry value that gets set is this: 30 78 30 30 30 30 30 30 30 38 20 etc etc (it goes on for quite a while)

    So yeah, I dont know how to convert that initial string of 0x00000008 etc into the correct data that needs to be written as binary. Is there a name for this type of string? maybe I can research it a little more myself if there is..
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Writing binary data to the registry

    I very much doubt that you're supposed to be converting from a string contain that data. Those are C-style hexadecimal representations of 32-bit numbers. For instance 0x9e6cb06e is 2657923182 and would be represented as &H9e6cb06e in VB. 32-bit numbers consist of four bytes, so that's &H9e, &H6c, &Hb0 and &H6e. It's really not obvious how you would convert those three hex values to a byte array.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Writing binary data to the registry

    Oh right I see. Well basically I am using a command-line version of the app im making as a guide, and in this batch file that someone else wrote, they use a text file with this data in to write the REG_BINARY key that containts the password:
    \registry\machine\software\orl\winvnc3\default
    Password = REG_BINARY 0x00000008 0x0a6d14c7 0x081e2e08
    Thats all that is in the text file, and it looks like they just use an executeable named REGINI to import it to the registry so I assumed there must be a way in VB.NET to just write these values to the REG_BINARY key in question.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Writing binary data to the registry

    Of course there's a way. Like I said, you need to convert the data to a Byte array and then you can write it to the Registry. The problem is that the conversion algorithm is not immediately obvious. Maybe you can just break each 32-bit number into 4 bytes but doing so on that data is not going to produce the series of bytes you specified earlier.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width