1

I need some assistance in writing a line feed to a registry value. The Value is of type REG_SZ.

I'm able to do this manually, by adding a "0A" to each break position when modifying the hex value in the Registry, but I'm not sure how to do this programmatically.

This is my current code to write the String to Registry, which WORKS, but does not allow for the line feeds:

(identifying information redacted, and text shortened. The "#" is the position for a line feed)

import os
import _winreg
from _winreg import *

Key = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
Field = [_winreg.REG_SZ, _winreg.REG_BINARY]
Sub_Key = ["legalnoticetext",
           "legalnoticecaption"]

value = ["Terms of Use",
        "By logging in to this PC, users agree to the Terms of Use"
        "\nThe User agrees to the following:#- The User may not alter, in any way, "
        "with Settings on this PC without the approval from any Authorised Persons."]

parmLen = len(Sub_Key)
z = 0  # Loop Counter for list iteration

try:
    Key = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
    if not os.path.exists(Key):
        key = _winreg.CreateKey(HKEY_LOCAL_MACHINE, Key)
    Registrykey = OpenKey(HKEY_LOCAL_MACHINE, Key, 0, KEY_WRITE)
    while z < parmLen:
        _winreg.SetValueEx(Registrykey, Sub_Key[z], 0, Field[z], value[z])
        print ("Setting <" + Sub_Key[z] + "> with value: " + value[z])
        z += 1
    CloseKey(Registrykey)
    print ("SUCCESSFUL! Procedure: Show Logon Terms of Use")
except WindowsError:
    print ("Error")

I've tested the following code to see if I can write directly in Hex, as I have the modified hex value, but it results in the value being interpreted as a string, and then formatting it incorrectly (again) to Hex.

import os
import _winreg
from _winreg import *

Key = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
Field = [_winreg.REG_SZ, _winreg.REG_BINARY]
Sub_Key = ["legalnoticetext",
           "legalnoticecaption"]

value = ["Terms of Use",

         "42,00,79,00,20,00,6C,00,6F,00,67,00,67,00,69,00,6E,00,67,00,20,00,69,00,"
         "6E,00,20,00,74,00,6F,00,20,00,74,00,68,00,69,00,73,00,20,00,50,00,43,00,"
         "2C,00,20,00,75,00,73,00,65,00,72,00,73,00,20,00,61,00,67,00,72,00,65,00,"
         "65,00,20,00,74,00,6F,00,20,00,74,00,68,00,65,00,20,00,54,00,65,00,72,00,"
         "6D,00,73,00,20,00,6F,00,66,00,20,00,55,00,73,00,65,00,0A,00,54,00,68,00,"
         "65,00,20,00,55,00,73,00,65,00,72,00,20,00,61,00,67,00,72,00,65,00,65,00,"
         "73,00,20,00,74,00,6F,00,20,00,74,00,68,00,65,00,20,00,66,00,6F,00,6C,00,"
         "6C,00,6F,00,77,00,69,00,6E,00,67,00,3A,00,0A,00,2D,00,20,00,54,00,68,00,"
         "65,00,20,00,55,00,73,00,65,00,72,00,20,00,6D,00,61,00,79,00,20,00,6E,00,"
         "6F,00,74,00,20,00,61,00,6C,00,74,00,65,00,72,00,2C,00,20,00,69,00,6E,00,"
         "20,00,61,00,6E,00,79,00,20,00,77,00,61,00,79,00,2C,00,20,00,77,00,69,00,"
         "74,00,68,00,20,00,53,00,65,00,74,00,74,00,69,00,6E,00,67,00,73,00,20,00,"
         "6F,00,6E,00,20,00,74,00,68,00,69,00,73,00,20,00,50,00,43,00,20,00,77,00,"
         "69,00,74,00,68,00,6F,00,75,00,74,00,20,00,74,00,68,00,65,00,20,00,61,00,"
         "70,00,70,00,72,00,6F,00,76,00,61,00,6C,00,20,00,66,00,72,00,6F,00,6D,00,"
         "20,00,61,00,6E,00,79,00,20,00,41,00,75,00,74,00,68,00,6F,00,72,00,69,00,"
         "73,00,65,00,64,00,20,00,50,00,65,00,72,00,73,00,6F,00,6E,00,73,00,2E,00,"
         "00,00"]

parmLen = len(Sub_Key)
z = 0  # Loop Counter for list iteration

try:
    Key = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
    if not os.path.exists(Key):
        key = _winreg.CreateKey(HKEY_LOCAL_MACHINE, Key)
    Registrykey = OpenKey(HKEY_LOCAL_MACHINE, Key, 0, KEY_WRITE)
    while z < parmLen:
        _winreg.SetValueEx(Registrykey, Sub_Key[z], 0, Field[z], value[z])
        print ("Setting <" + Sub_Key[z] + "> with value: " + value[z])
        z += 1
    CloseKey(Registrykey)
    print ("SUCCESSFUL! Procedure: Show Logon Terms of Use")
except WindowsError:
    print ("Error")

RTFM'ing doesn't prove to be very helpful with this specific issue, so any guidance would be appreciated!

Johan Brink
  • 333
  • 5
  • 20

1 Answers1

1

You have a couple of issues going on. os.path.exists checks for file path existence. Feeding it the key string won't check the registry.

It looks like you are using Python 2.7, please consider upgrading to 3. The issue you are running into is that writing a REG_BINARY expects binary data; in Python 3, this means you just need to encode your string. Here is the code in Python 3.

import winreg

Key_str = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
Field = [winreg.REG_SZ, winreg.REG_BINARY]
Sub_Key = ["legalnoticetext", "legalnoticecaption"]

value = ["Terms of Use",
        "By logging in to this PC, users agree to the Terms of Use"
        "\nThe User agrees to the following:#- The User may not alter, in any way, "
        "with Settings on this PC without the approval from any Authorised Persons."]

try:
    key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, Key_str)
except FileNotFoundError:
    key = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, Key_str)

for sk, f, v in zip(Sub_Key, Field, value):
    if f == winreg.REG_BINARY:
        winreg.SetValueEx(key, sk, 0, f, v.encode('latin-1'))
    else:
        winreg.SetValueEx(key, sk, 0, f, v)
key.Close()

In Python 2, your standard strings are byte strings, so there is no need to encode the string to a bytes encoding. Here is the Python 2 code:

import _winreg

Key_str = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
Field = [_winreg.REG_SZ, _winreg.REG_BINARY]
Sub_Key = ["legalnoticetext", "legalnoticecaption"]

value = ["Terms of Use",
        "By logging in to this PC, users agree to the Terms of Use"
        "\nThe User agrees to the following:#- The User may not alter, in any way, "
        "with Settings on this PC without the approval from any Authorised Persons."]

try:
    key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, Key_str)
except WindowsError:
    key = _winreg.CreateKey(_winreg.HKEY_LOCAL_MACHINE, Key_str)

for sk, f, v in zip(Sub_Key, Field, value):
    _winreg.SetValueEx(key, sk, 0, f, v)
key.Close()
James
  • 32,991
  • 4
  • 47
  • 70
  • Why are you handling a `FileNotFoundError` from a `winreg.OpenKey()` call (in the Python 3 code)? Is that the kind of exception it will raise? – martineau Nov 24 '19 at 22:33
  • Yes. Try it for yourself. `winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'TEST\TEST')` – James Nov 25 '19 at 19:59