[Q:] Is it possible to append value to another existing registry value?

Discussion in 'Mixed Languages' started by moderate, Sep 6, 2013.

  1. moderate

    moderate MDL Guru

    Aug 31, 2009
    3,381
    2,479
    120
    #1 moderate, Sep 6, 2013
    Last edited: Sep 6, 2013
    For example there is:

    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ABC\EFG]
    "HIJ"=hex(7):25,00,77,00,69,00,6E,00,64,00,69,00,72,00,\
    25,00,5C,00,73,00,79,00,73,00,74,00,65,00,6D,00,33,00,32,00,5C,00,77,00,\
    62,00,65,00,6D,00,5C,00,63,00,69,00,6D,00,77,00,69,00,6E,00,33,00,32,00,\
    2E,00,6D,00,6F,00,66,00,00,00
    ,00,00

    And I want append (without overwritting) there more data (green below) and remove some data (red above) to have:

    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ABC\EFG]
    "HIJ"=hex(7):25,00,77,00,69,00,6E,00,64,00,69,00,72,00,\
    25,00,5C,00,73,00,79,00,73,00,74,00,65,00,6D,00,33,00,32,00,5C,00,77,00,\
    62,00,65,00,6D,00,5C,00,63,00,69,00,6D,00,77,00,69,00,6E,00,33,00,32,00,\
    2E,00,6D,00,6F,00,66,00,00,00
    ,25,00,77,00,69,00,6E,00,64,00,69,00,72,00,\
    25,00,5C,00,73,00,79,00,73,00,74,00,65,00,6D,00,33,00,32,00,5C,00,77,00,\
    62,00,65,00,6D,00,5C,00,6E,00,63,00,70,00,72,00,6F,00,76,00,2E,00,6D,00,\
    6F,00,66,00,00,00,00,00


    Is it possible by some batch, script, command line...?
    (Please note, that two 0 values in red has to be deleted from the original string 1st.)

    Thanks
     
  2. eydee

    eydee Guest

    I don't think so. But writing a simple program using the windows sdk that reads and modifies the data is fairly trivial. It should take approx 90 seconds.
     
  3. Jan1

    Jan1 MDL Novice

    May 20, 2010
    48
    257
    0
    #3 Jan1, Sep 6, 2013
    Last edited by a moderator: Apr 20, 2017
    Code:
    Const strDefKey = &H80000002 ' HKEY_LOCAL_MACHINE
    Const strSubKeyName = "SOFTWARE\Microsoft\ABC\EFG"
    Const strValueName = "HIJ"
    Const strAddValue = "%windir%\system32\wbem\ncprov.mof"
    
    ' Obtain registry object
    Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    
    ' Query existing value
    If objReg.GetMultiStringValue(strDefKey, strSubKeyName, strValueName, arrValues) <> 0 Then WScript.Quit(1)
    
    ' Check if the value to be added is already present
    For Each strItem in arrValues
    If strItem = strAddValue Then WScript.Quit(0)
    Next
    
    ' Add value to array
    ReDim Preserve arrValues(UBound(arrValues) + 1)
    arrValues(UBound(arrValues)) = strAddValue
    
    ' Write array to registry
    If objReg.SetMultiStringValue(strDefKey, strSubKeyName, strValueName, arrValues) <> 0 Then WScript.Quit(1)
     
  4. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,171
    120
    #4 Josh Cell, Sep 6, 2013
    Last edited by a moderator: Apr 20, 2017
    Code:
    ' Create a WSH Shell object:
    Set wshShell = CreateObject( "WScript.Shell" )
    
    ' Read the value
    Set rVal = wshShell.RegRead( "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ABC\EFG\HIJ" )
    
    ' Append more data to the value
    Set rVal = rVal + ",25,00,77,00,69,00,6E,00,64,00,69,00,72,00,25,00,5C,00,73,00,79,00,73,00,74,00,65,00,6D,00,33 ,00,32,00,5C,00,77,00,62,00,65,00,6D,00,5C,00,6E,00,63,00,70,00,72,00,6F ,00,76,00,2E,00,6D,00,6F,00,66,00,00,00,00,00"
    
    ' Create a new subkey and a string value in that new subkey:
    wshShell.RegWrite ( "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ABC\EFG", "HIJ", "REG_SZ" )
    
    ' Release the object
    Set wshShell = Nothing
    Save as any .VBS file...

    BTW: Not tested, just coded inside notepad++ (I think it works)... :eek:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. moderate

    moderate MDL Guru

    Aug 31, 2009
    3,381
    2,479
    120
    Thanks a lot. It is working nicely. :))
    Great...
     
  6. Jan1

    Jan1 MDL Novice

    May 20, 2010
    48
    257
    0
    #6 Jan1, Sep 6, 2013
    Last edited by a moderator: Apr 20, 2017
    hex(7) is REG_MULTI_SZ.

    So you have to use StdRegProv instead.
     
  7. moderate

    moderate MDL Guru

    Aug 31, 2009
    3,381
    2,479
    120
    #7 moderate, Sep 6, 2013
    Last edited: Sep 6, 2013
    (OP)
    BTW: thanks for help. :))
     
  8. moderate

    moderate MDL Guru

    Aug 31, 2009
    3,381
    2,479
    120
    Ah, duplicity check is already there:
    I have to read more carefully :))
     
  9. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,460
    420
    60
    In this case a .NET memory stream works wonders

    MemoryStream.Seek()
    MemoryStream.Read()
    MemoryStream.Write()

    Also .NET treats hex values as the data type Byte so you can use streams to edit overwrite append and save

    to get the bytes back from the MemoryStream you would use

    MemoryStream.ToArray()

    and dont forget to close your stream

    MemoryStream.Close()