Search code examples
windowsbatch-filecmdregistryregistrykey

How to add a REG_NONE empty value using Batch?


This registry script writes a REG_NONE empty value in the reg editor (which is represented as binary data):

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\keyname]
"valuename"=hex(0):

enter image description here

(the english translation of the data-description in the image above is: "zero-length binary value")

I need to reproduce the same in Batch (to improve a Reg2Bat converter), but when I try this:

REG ADD "HKCU\keyname" /V "valuename" /T "REG_NONE" /D "" /F

It adds data:

enter image description here

Maybe the reg.exe command is not compatible with the REG_NONE valuetype? (the command help says its a supported value but... you see)

How I could really add a REG_NONE empty value?


Solution

  • The only nasty option I found so far is creating a .reg file and import that one:

    call :regnone HKEY_CURRENT_USER "keyname" valuename
    
    goto :eof
    
    :regnone
    rem create a reg file
    echo Windows Registry Editor Version 5.00 > none.reg
    echo [%~1\%~2] >> none.reg
    echo "%~3"=hex(0): >> none.reg
    
    rem import it the registry
    reg import none.reg 
    
    del /q none.reg    
    
    goto :eof
    

    REG_NONE is a special type that due to implementation details (the commandline tools are optimized for String and multi string) can only be created with a zero-length binary value by the RegSetValueEx windows api. The higherlevel api's like the one on the WMI provider only allow for SetBinaryValue and there is no SetNoneValue. Beside REG there is also an option to use wmic that sits a little bit closer on the WMI provider but that still doesn't allow you to create a REG_NONE type (it does enable you to create zero-length REG_BINARY, something REG is also unable to do)

    The closest empty binary value you can get with this command (provided by MC ND)

    reg add "hkcu\volatile environment" /v test /t reg_binary
    

    is a two zero bytes: 00 00, caused by the two null character termination from (the not provided with the option /d ) multi String