How to add a REG_NONE empty value using Batch?
Asked Answered
B

2

6

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?

Bodhisattva answered 20/8, 2014 at 21:11 Comment(3)
In my opinion it is a design error in code (bug) of REG to support type REG_NONE, but adding a value of this type with 00 00 (terminating null of a Unicode string) even if /D "..." is not specified on command line. REG ADD "HKCU\Key Name" /V "Value Name" /T REG_NONE /F should do the job, but definitely does not which could be interpreted as bug of REG worth being reported to Microsoft.Plenitude
@Plenitude thanks for comment, I've tested the same command in Windows XP and the resulting value is the same null string: '00 00', do you know the link to report a possible bug to Microsoft?Bodhisattva
Reporting bugs in Windows and its installed tools is very difficult as it can be read on Where can I report a bug in Windows 7? and How to report a bug in Windows 7? There is more or less no other method than contacting Microsoft support by phone to report things like this and hope that person of Microsoft support interprets it also as an issue to process.Plenitude
B
6

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

Besprent answered 24/8, 2014 at 17:11 Comment(3)
While i can not disagree with the core (the only way to solve the problem is to use a reg file), it is not correct to say that a REG_NONE can only be created with RegSetValueEx (the OP created it with reg.exe), and it is not correct to say reg.exe can not create a empty binary value: reg add "hkcu\volatile environment" /v test /t reg_binary. The problem with reg.exe is that when the type is reg_none, it reads the command line to retrieve a reg_multi_sz value, the reason for the 0x00 0x00 value, a double null terminated string.Procrustes
You can embed the reg file in the batch somewhat as Rob van der Woude posted.Hang
@IrRelevant That is an interesting trick but I don't think it can be applied here because the reg file is not static.Besprent
B
1

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

Bodhisattva answered 25/8, 2014 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.