Search code examples
internet-explorervbscriptregistry

Create binary file in windows registry


I am trying create a binary file in windows registry to change the compatibility view settings in IE11. We have some web applications that require certain security settings and I want to make it easy enough for the users to change the settings. I can't just set the settings and lock down IE since the settings are different for each application.

I have tried this solution but did not work. Convert Hex String into Array and Write it to registry - VBSCript

I am running Windows 7 with IE11

Here is the vbs file that I have now. I do not get an error but it also does not write anything to the registry.

strHexValue = "41,1f,00,00,53,08,ad,ba,01,00,00,00,38,00,00,00,01,00,00,00,01,00,00,00,0c,00,00,00,8c,96,3d,03,41,87,d4,01,01,00,00,00,0d,00,6e,00,61,00,76,00,69,00,6d,00,65,00,64,00,69,00,78,00,2e,00,63,00,6f,00,6d,00"

arrValue = Split(strHexValue, ",")
ReDim uBinary(UBound(arrValue))
For i = LBound(arrValue) To UBound(arrValue)
    uBinary(i) = CLng("&h" & arrValue(i))
Next

Const HKEY_CURRENT_USER = &H80000001
Set objRegistry = GetObject("Winmgmts:root\default:StdRegProv")
strPath = "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\BrowserEmulation\ClearableListData"
strValueToWrite = "UserFilter"
intReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, strPath, strValueToWrite, uBinary)

Solution

  • FYI you don't need administrative privileges to write this setting because it's not a machine setting. It's just a user setting so the current invoker is also the current user.

    Second, you also don't need literal string value HKEY_CURRENT_USER\ in strPath. You are already using that constant you defined earlier named HKEY_CURRENT_USER as the first parameter in the .SetBinaryValue method.

    Change strPath as follows.

    strPath = "Software\Microsoft\Internet Explorer\BrowserEmulation\ClearableListData"
    

    Rest of your code looks OK but since .SetBinaryValue does not implicitly create subkeys in the path, you'll need to make sure that the key you want to write data exists. To do it, put the following line just before you call objRegistry.SetBinaryValue.

    objRegistry.CreateKey HKEY_CURRENT_USER, strPath 
    

    This will create the key if it doesn't exist, so your .SetBinaryValue call can work as expected.

    After making these changes, the final version of your code should be something like the following.

    strHexValue = "41,1f,00,00,53,08,ad,ba,01,00,00,00,38,00,00,00,01,00,00,00,01,00,00,00,0c,00,00,00,8c,96,3d,03,41,87,d4,01,01,00,00,00,0d,00,6e,00,61,00,76,00,69,00,6d,00,65,00,64,00,69,00,78,00,2e,00,63,00,6f,00,6d,00"
    
    arrValue = Split(strHexValue, ",")
    ReDim uBinary(UBound(arrValue))
    For i = LBound(arrValue) To UBound(arrValue)
        uBinary(i) = CLng("&h" & arrValue(i))
    Next
    
    Const HKEY_CURRENT_USER = &H80000001
    Set objRegistry = GetObject("Winmgmts:root\default:StdRegProv")
    strPath = "Software\Microsoft\Internet Explorer\BrowserEmulation\ClearableListData"
    strValueToWrite = "UserFilter"
    objRegistry.CreateKey HKEY_CURRENT_USER, strPath
    intReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, strPath, strValueToWrite, uBinary)