• Converting a Binary Reg Value to a String??

    Home » Forums » Developers, developers, developers » Web design and development » Converting a Binary Reg Value to a String??

    Author
    Topic
    #391094

    I want to use WScript.RegRead to read a binary registry value, then convert that into a String. Here is my example:

    ————————————————-

    ‘ declare variables
    Dim sVal, bData, sData

    ‘ create scripting objects
    Set oWshShell = WScript.CreateObject(“WScript.Shell”)

    ‘define variable
    sVal = “HKCRWord.Document.8EditFlags”
    bData = oWshShell.RegRead(sVal)

    –insert function to convert binary value to a string–
    (i.e., bData => sData)

    ———————————-

    I need to be able to display the EditFlags as text, so I am assuming that I must convert the binary data to a string first. I have found some recommendations on web sites, but none actually work the way I want them to…

    Thanks.

    Viewing 1 reply thread
    Author
    Replies
    • #697119

      Jefferson — and I assume you will at least look in here — this is what I am working on. Presently it is at an early stage — I may even compile it into a program in the future. But first I need to figure out have to retrieve and manipulate the EditFlags data…

      The present script purposefully does NOT correctly read the EditFlags value — because I need to figure out how to turn the data into a string first…

      Thanks.

      • #697178

        laugh How did you know I would come in here?

        Or that I would find this project irresistable? I think I’ve become too predictable. laugh

        Well, according to my trusty reference (O’Reilly’s VBScript in a Nutshell, good but with just enough typos to kill a few hairs now and then), reading a binary value such as EditFlags should give you a variant of subtype byte array. According to Getz & Gilbert’s VBA Developer’s Handbook, they say that you can convert a byte array into a string simply by assigning it to a string variable. Because VBScript is not strongly typed, however, Peter’s function has better odds of making the conversion actually happen. Either way, you’re not getting a string like you would see in the registry, you’re getting the character code equivalents, which is useless.

        But… why do you want a string? Byte array data can be read in an orderly manner; maybe that’st just as good?

        Also, in doing a search through the registry, I found a number of values which were DWORD (Long) rather than BINARY. That’s a bit troubling; you might need to sniff the data first to see what you got before moving ahead with figuring out what it means.

        Anyway, here’s an example of how to parse the byte arrays. Not sure about the Longs… I’m not a hexadecimal mathematician.

        ' Sample EditFlags Reader
        Dim oWshShell, sType
        Dim bFlags	' byte array when read from binary value, otherwise long
        ' create scripting objects
        Set oWshShell = WScript.CreateObject("WScript.Shell")
         
          ' define variables
        sType = InputBox("Enter ASF or CSS for testing - all others could bomb.") & "File"
        bFlags = oWshShell.RegRead("HKCR" + sType + "EditFlags")
        If VarType(bFlags) And vbLong Then
          MsgBox "bFlags is of type Long. Parse this: " & bFlags, , "Testing"
        ElseIf VarType(bFlags) And vbArray Then
          MsgBox "bFlags is of type variant array. The first 4 elements are: " & vbcrlf & _
            bflags(0) & vbcrlf & bflags(1) & vbcrlf & _
            bflags(2) & vbcrlf & bflags(3) & vbcrlf, , "Testing"
        Else
          MsgBox "What is this? " & VarType(bFlags)
        End If
         
        ' release the objects
        Set oWshShell = nothing
        Set oFS = nothing

        Hope this helps.

        • #697189

          Thanks for all the advice. I will have to work on this tomorrow — no more time today.

          I wanted the string because I know how to manipulate the string to extract the information I need. I have very limited skills — so I try to rely on what I already know!! String manipulation is something I can do, so if I can get a string, I can use it without having to learn too many new things!

          I have the VBS Nutshell book too — eh, at home — but I am on vacation! I usually bring it with me smile, but I was short on space!

          I want to be able to read the EditFlags to determine at least two things:

          1) Whether or not IE will offer to download the file extension, or simply open it in the specified program. (Yes, this is the storage place of the “Confirm open after download” data.)

          2) Whether or not the “Confirm open…” check box is grayed out in the File Types dialog box — for this is also stored in the EditFlags.

          There is more data there as well, but these two are paramount to figuring out these File Extension questions….

          Thanks.

        • #697300

          OK, it certainly does work. Thank you very much. Now I have a byte array.

          Oh, just to side step — despite all my spelunking in the registry, I don’t recall ever noticing that some EditFlags were listed a DWORDs. I thought they were always Binary values. Live and learn.

          I believe need to change the array to be a string — and I think I can do that with Cstr — or I may not even have to. I need to review this some more… frown

          Interestingly, the array is actually a set of 4 decimal numbers — it is no longer in Hex. So, 00 10 00 01 returns :0 16 0 1.

          Looking at the DWORD values, it is hard to know what to do with them. I am not sure they are used in the same manner. I’ll have to comtemplate that some more…

          Thanks again.

          (Add: [TT] does not seem to work… hmmm.)

          • #697302

            >> [TT] does not seem to work… hmmm.

            As far as I know, [TT] has never been a valid Lounge tag (since I joined). To preserve indentation, use

             before and 

            after the text, or use the [tab] tag to insert 4 spaces. I use a little Word macro to replace a set number of spaces within the selected text with [tab] tags:

            Sub Spaces2Tabs()
            Const NumberOfSpaces = 4 ‘ Adapt as needed
            On Error GoTo Err_Sub
            Selection.Find.Execute FindText:=Space(NumberOfSpaces), _
            ReplaceWith:=”[tab]”, Replace:=wdReplaceAll
            Exit Sub
            Err_Sub:
            If Err 91 Then
            MsgBox Err.Description, vbExclamation
            End If
            End Sub

            • #697308

              Thanks! I got used to using [TT] on other sites. The PRE tag uses the fixed width font (which is what I was after), so that is fine. smile

              I am still screwing up. I can get the script to work the way I want to by adding these lines to Jeff’s code:

              ' combining the data into one line
              sText = bflags(0) & " " & bflags(1) & " " & bflags(2) & " " & bflags(3)
              
              MsgBox sText, vbInformation, "Single line data"
              

              BUT… when I try to do it my way, I am getting into problems. frown

              Here is my FAILING code:

              ' On Error Resume Next
              
              ' declare variables
              Dim oWshShell, sExt, sKey, sType, sTypeKey, sFlags
              Dim vFlags	' byte array when read from binary value, otherwise long
              
              
              ' create scripting object
              Set oWshShell = WScript.CreateObject("WScript.Shell")
              
              
              ' obtain the file extention from the user
              sExt = InputBox("Enter File Extension:" + vbCRLF + _
              "(Do not include the preceding period)" + vbCRLF + "Example: txt")
              
              
              ' obtain the File Type from the file extention
              sKey = "HKCR." + sExt + ""
              sType = oWshShell.RegRead(sKey)
              sTypeKey = "HKCR" + sType + ""
              
              
              ' collect the EditFlags data
              vFlags = oWshShell.RegRead(sTypeKey + "EditFlags")
                 
              
              ' divide the EditFlags data based on array type
              If VarType(vFlags) And vbArray Then
                 sFlags = vFlags(0) + " " + vFlags(1) + " " + _
                   vFlags(2) + " " & vFlags(3)
              ElseIf VarType(vFlags) And vbLong Then
                 sFlags = vFlags
              Else
                 sFlags = VarType(vFlags)
              End If
              
              
              ' display results
              MsgBox "The File Type Key is: " + sTypeKey + vbCRLF + _
              "The Edit Flags are: " + sFlags + vbCRLF, _
              bInformation, "File Extension Data for the ." + sExt + " Extension"
               
              
              ' release the object
              Set oWshShell = nothing
              

              So, I obviously don’t understand something that I should… frown I am getting a “type mismatch” error again. May just be a typo… I’ll keep hacking away at it… Thanks for all the advice.

              (That’s funny, in the PRE text the CRLF is not being done as preformatted… )

            • #697309

              Ah… caught one bug! smile

              If I use the ampersand (&) instead of the plus sign (+) when combining strings, I can get it to work when the EditFlags is a Binary value. I guess I should get used to using the ampersand… frown

              I still get a type mismatch when the EditFlags is a DWORD. Hmmm…

            • #697310

              I can’t help you with the specific problem, but I have some general remarks:
              – Shouldn’t bFlags be vFlags?
              – Although “+” can be used to concatenate strings, “&” is safer; in the vicinity of a numeric value “+” tends to be interpreted as the addition operator.
              – You’ll have to add error checking; on my PC, the typekey for csv is Excel.CSV but there is no EditFlags value for Excel.csv.

            • #697312

              I think you are correct on all points — and I caught some of the errors after I posted (typical!).

              I have some preliminary error checking written, but I will likely include “(no value)” for cases where there are no values in the registry. I am still just trying to get it to run smoothly! smile

              It works fine if you activate the “On Error Resume Next” line — blowing through the missing values. It then returns blank spots for data — which is actually OK at this point in time.

              Thanks for all the help.

            • #697369

              (Edited by jscher2000 on 28-Jul-03 11:31. I need more caffeine, sorry.)

              You guys get up so early!! laugh

              > sFlags = vFlags(0) + ” ” & vFlags(1) & ” ” & vFlags(2) & ” ” & vFlags(3)

              I suggest that because the size of the numbers in vFlags is unpredictable — here’s an odd example:

                [HKEY_CLASSES_ROOTbatfile]
                @=”MS-DOS Batch File”
                “EditFlags”=hex:30,04,00,00[/list](I didn’t test this with your script — I added a picture of testing it with my short script from last night) — you try something like this:

                WRONG: sFlags = Left(“00″ & CStr(vFlags(0)), 2) & ” ” & etc.
                RIGHT: sFlags = Right(“00″ & CStr(vFlags(0)), 2) & ” ” & etc.

                This will give you the original look you were seeking and allow you to always know that it will be two characters, space, two characters, space, etc., to handle those odd cases.

    • #697175

      u probly want CStr().

      Of interest is Windows Scripting Technologies, online documentation, downloadable from the MS site.

    Viewing 1 reply thread
    Reply To: Converting a Binary Reg Value to a String??

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: