Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 964
ElektroStudios Avatar asked Aug 20 '14 21:08

ElektroStudios


2 Answers

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

like image 161
KennyBOT Avatar answered Nov 15 '22 00:11

KennyBOT


Just my extended solution based on @KennyBOT answer:

:Add_Special_Value :: Support for adding an special registry value type.

Set "KeyRoot=%~1"
Set "KeyName=%~2"
Set "ValueName=%~3"
Set "ValueType=%~4"
Set "ValueData=%~5"

Set "RegFile=%TEMP%\%ValueType%.reg"

If /I "%KeyRoot%" EQU "HKCR" (Set "KeyRoot=HKEY_CLASSES_ROOT")
If /I "%KeyRoot%" EQU "HKCU" (Set "KeyRoot=HKEY_CURRENT_USER")
If /I "%KeyRoot%" EQU "HKLM" (Set "KeyRoot=HKEY_LOCAL_MACHINE")
If /I "%KeyRoot%" EQU "HKCC" (Set "KeyRoot=HKEY_CURRENT_CONFIG")
If /I "%KeyRoot%" EQU  "HKU" (Set "KeyRoot=HKEY_USERS")

If /I "%ValueType%" EQU "REG_NONE"                       (Set "ValueType=hex^(0^)")
If /I "%ValueType%" EQU "REG_RESOURCE_LIST"              (Set "ValueType=hex^(8^)")
If /I "%ValueType%" EQU "REG_RESOURCE_REQUIREMENTS_LIST" (Set "ValueType=hex^(a^)")
If /I "%ValueType%" EQU "REG_FULL_RESOURCE_DESCRIPTOR"   (Set "ValueType=hex^(9^)")

(
 Echo Windows Registry Editor Version 5.00
 Echo [%KeyRoot%\%KeyName%]
 Echo "%ValueName%"=%ValueType%:%ValueData%
)>"%RegFile%"

REG.exe "Import" "%RegFile%"
DEL     /Q       "%RegFile%" 2>NUL
Goto :EOF

Usage:

Call :Add_Special_Value "HKCU" "MyKeyName" "MyValueName" "REG_NONE" "Binary data (If any)"

Oh and guys don't forget to try my application and report me bugs (if any) :)

enter image description here

SOURCE CODE (VB.NET): http://www.mediafire.com/download/1h3zbymfhnb3spt/REG2BAT.rar

like image 32
ElektroStudios Avatar answered Nov 14 '22 23:11

ElektroStudios