Results 1 to 6 of 6

Thread: Register a binary key (REG_BINARY)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Register a binary key (REG_BINARY)

    I really need your help guys. I browse, search around the forums, on google, yup, there are many tutorials on it , but since i'm a newbie, i just can't understand how it's works, could you guys post a working example on it. I thanks.
    here's the value of the key:

    39,ff,0d,d8,7c,74,a4,7f,73,99,b8,cb,ba,3d,b1,44,c9,71,\
    63,4c,ca,fe,2f,cc
    .... and so on

  2. #2
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Register a binary key (REG_BINARY)

    You'll have to be more specific. That looks like it was exported from the registry using regedit. Why not just merge the key ? i.e. export it to a file, say, "C:\Temp\MySettings.reg" then:-

    Code:
    '  /s means silent mode merging.
    
    regedit /s C:\Temp\MySettings.reg
    If you want to do it in VB use ShellExecute:-
    VB Code:
    1. Option Explicit
    2.  
    3. 'Form level code.
    4. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    5.    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    6.    ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    7.  
    8. Const SW_HIDE = 0
    9.  
    10. Private Sub Form_Click()
    11.    Dim retval As Long
    12.  
    13. 'Merge it without confirmation messages.
    14.    retval = ShellExecute(Me.hwnd, "open", "regedit.exe", "/s C:\Temp\MySettings.reg", vbNullString, SW_HIDE)
    15. End Sub

  3. #3
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Register a binary key (REG_BINARY)

    i was looking all night also for a solution to this ..

    anyway finally found a Hex function from a member on this forum, and then used some existing code i had plus integrated what i found on VB Accelerator to come up with this ..

    Maybe this will help someone else save some time ..

    Notice how the Data is passed to the Save Procedure as a Variant to accomodate Arrays as well as other types.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
    4. (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    5.  
    6. Private Declare Function RegCloseKey Lib "advapi32.dll" _
    7. (ByVal hKey As Long) As Long
    8.  
    9. ' STRING
    10. Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" _
    11. (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, _
    12. ByVal lpValue As String, ByVal cbData As Long) As Long
    13.  
    14. ' LONG
    15. Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" _
    16. (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, _
    17. lpValue As Long, ByVal cbData As Long) As Long
    18.  
    19. ' BYTE
    20. Private Declare Function RegSetValueExByte Lib "advapi32" Alias "RegSetValueExA" _
    21. (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, _
    22. szData As Byte, ByVal cbData As Long) As Long
    23.  
    24. Private Const m_HCU As Long = &H80000001    'LOCAL MACHINE
    25. Private Const m_HLM As Long = &H80000002    'CURRENT USER
    26.  
    27. ' REGISTRY VALUES
    28. Public Enum regTypes
    29.    ValNull = 0
    30.    ValString = 1
    31.    ValXString = 2
    32.    ValBinary = 3
    33.    ValDWord = 4
    34.    ValDWordLE = 4
    35.    ValDWordBE = 5
    36.    ValLink = 6
    37.    ValMultiString = 7
    38.    ValResList = 8
    39. End Enum
    40.  
    41. ' CLICK TO OPEN VALUES
    42. Public Enum enClickToOpen
    43.     ctoDouble = 1
    44.     ctoSingle = 0
    45. End Enum
    46.  
    47. ' Single or Double Click on Items
    48. Public Property Let ClickToOpen(ByVal inNew As enClickToOpen)
    49.     Dim bArr() As Byte
    50.     If inNew = ctoDouble Then    'double click
    51.         bArr = Hex2ByteArr("2400000033a80000000000000000000000000000010000000d0000000000000000000000")
    52.         Save_Value m_HCU, "Software\Microsoft\Windows\CurrentVersion\Explorer", "ShellState", bArr, ValBinary
    53.     Else                         'single click
    54.         bArr = Hex2ByteArr("2400000013a80100000000000000000000000000010000000d0000000000000002000000")
    55.         Save_Value m_HCU, "Software\Microsoft\Windows\CurrentVersion\Explorer", "ShellState", bArr, ValBinary
    56.     End If
    57. End Property
    58.  
    59. ' SAVE / UPDATE REGISTRY VALUE
    60. Private Sub Save_Value(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String, _
    61. ByVal varData As Variant, ByVal ValType As regTypes)
    62.     Dim keyhand As Long
    63.     Dim lResult As Long
    64.     Dim ordType As Long
    65.     Dim c       As Long
    66.     lResult = RegCreateKey(hKey, strPath, keyhand)
    67.     If lResult Then
    68.         'error
    69.     Else
    70.         Select Case ValType
    71.             ' BINARY VALUE (BYTE ARRAY)
    72.             Case ValBinary
    73.                 If (VarType(varData) = vbArray + vbByte) Then
    74.                     Dim ab() As Byte
    75.                     ab = varData
    76.                     c = UBound(ab) - LBound(ab) + 1
    77.                     lResult = RegSetValueExByte(keyhand, strValue, 0&, ValType, ab(0), c)
    78.                 Else
    79.                     'error
    80.                 End If
    81.             ' DWORD VALUE (INT/LONG)
    82.             Case ValDWord, ValDWordBE, ValDWordLE
    83.                 If (VarType(varData) = vbInteger) Or (VarType(varData) = vbLong) Then
    84.                     Dim i As Long
    85.                     i = varData
    86.                     ordType = ValDWord
    87.                     lResult = RegSetValueExLong(keyhand, strValue, 0&, ordType, i, 4)
    88.                 Else
    89.                     'error
    90.                 End If
    91.             ' STRING VALUE
    92.             Case ValString, ValXString
    93.                 Dim s As String, iPos As Long
    94.                 s = varData
    95.                 ordType = ValString
    96.                 iPos = InStr(s, "%")
    97.                 If iPos Then
    98.                     If InStr(iPos + 2, s, "%") Then ordType = ValXString
    99.                 End If
    100.                 c = Len(s) + 1
    101.                 s = s & vbNullChar
    102.                 lResult = RegSetValueExString(keyhand, strValue, 0&, ordType, s, c)
    103.             Case Else
    104.                 'error
    105.         End Select
    106.         If lResult Then
    107.             'error
    108.         End If
    109.         RegCloseKey keyhand
    110.     End If
    111. End Sub
    112.  
    113. ' HEX TO BYTE ARR - Joacim Andersson
    114. Private Function Hex2ByteArr(ByVal sHex As String) As Byte()
    115.     Dim n As Long
    116.     Dim nCount As Long
    117.     Dim bArr() As Byte
    118.     'First of all, make sure the length of the hex string is even, if it is not then
    119.     'put a "0" at the beginning
    120.     nCount = Len(sHex)
    121.     If (nCount And 1) = 1 Then
    122.         sHex = "0" & sHex
    123.         nCount = nCount + 1
    124.     End If
    125.     'ReDim the Byte array
    126.     ReDim bArr(nCount \ 2 - 1) 'we subtract 1 since the array is zero based
    127.     For n = 1 To nCount Step 2
    128.         'Convert the hex numbers into decimal values and store them in the byte array
    129.         bArr((n - 1) \ 2) = CByte("&H" & Mid$(sHex, n, 2))
    130.     Next
    131.     'Return the array
    132.     Hex2ByteArr = bArr
    133. End Function

    Since this is a Class File in my case, I call i like this in my app ..
    VB Code:
    1. Dim appe As cTweak
    2.     Set appe = New cTweak
    3.         appe.ClickToOpen = ctoDouble
    4.     Set appe = Nothing

  4. #4
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Register a binary key (REG_BINARY)

    Quote Originally Posted by rory
    i was looking all night also for a solution to this ..
    You should have asked . I have ready code for just about any registry manipulation anyone can think of ... (Including renaming/copying a registry subkey in just 3 lines of code). Simply wanted Vntalk to prove he'd made an effort...

  5. #5
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Register a binary key (REG_BINARY)

    Quote Originally Posted by schoolbusdriver
    You should have asked . I have ready code for just about any registry manipulation anyone can think of ... (Including renaming/copying a registry subkey in just 3 lines of code). Simply wanted Vntalk to prove he'd made an effort...
    Im stubborn .. dont like to ask unless its a last resort ..
    but would have saved some of my hair in this case .. was pulling it all out all night long ..

  6. #6
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Register a binary key (REG_BINARY)

    Quote Originally Posted by rory
    Im stubborn .. dont like to ask unless its a last resort ..

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