Results 1 to 12 of 12
  1. #1
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Converting a Binary Reg Value to a String??

    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.

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Converting a Binary Reg Value to a String??

    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.
    Attached Files Attached Files

  3. #3
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Converting a Binary Reg Value to a String??

    u probly want CStr().

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

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Converting a Binary Reg Value to a String??

    <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15> How did you know I would come in here?

    Or that I would find this project irresistable? I think I've become too predictable. <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>

    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.

    <pre>' 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</pre>

    Hope this helps.

  5. #5
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Converting a Binary Reg Value to a String??

    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 <img src=/S/smile.gif border=0 alt=smile width=15 height=15>, 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.

  6. #6
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Converting a Binary Reg Value to a String??

    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... <img src=/S/frown.gif border=0 alt=frown width=15 height=15>

    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.)
    Attached Images Attached Images

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Converting a Binary Reg Value to a String??

    >> [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 <!t>[pre]<!/t> before and <!t>[/pre]<!/t> after the text, or use the <!t>[tab]<!/t> tag to insert 4 spaces. I use a little Word macro to replace a set number of spaces within the selected text with <!t>[tab]<!/t> tags:

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

  8. #8
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Converting a Binary Reg Value to a String??

    Ah... caught one bug! <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    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... <img src=/S/frown.gif border=0 alt=frown width=15 height=15>

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

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Converting a Binary Reg Value to a String??

    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.

  10. #10
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Converting a Binary Reg Value to a String??

    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. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    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:
    <pre>' combining the data into one line
    sText = bflags(0) & " " & bflags(1) & " " & bflags(2) & " " & bflags(3)

    MsgBox sText, vbInformation, "Single line data"
    </pre>

    BUT... when I try to do it my way, I am getting into problems. <img src=/S/frown.gif border=0 alt=frown width=15 height=15>

    Here is my FAILING code:
    <pre>' 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
    </pre>

    So, I obviously don't understand something that I should... <img src=/S/frown.gif border=0 alt=frown width=15 height=15> 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... )

  11. #11
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Converting a Binary Reg Value to a String??

    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! <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    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.

  12. #12
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Converting a Binary Reg Value to a String??

    <P ID="edit" class=small>(Edited by jscher2000 on 28-Jul-03 11:31. I need more caffeine, sorry.)</P>You guys get up so early!! <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>

    > 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:
    <UL>[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.
    Attached Images Attached Images

Posting Permissions

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