I'm trying to set registry keys based on values stored in a JSON object. I have no trouble creating keys in HKEY_LOCAL_USER. Anything I try to create in HKEY_LOCAL_MACHINE doesn't get created. The keys fail to create when running the command prompt with and without admin permissions. Running Python 3.8.
from winreg import *
import ctypes, json, sys, time
#Determine the version of PDM and the branch location.
settingsData = 'EPDM_Vault_settings.txt'
solidworksVersion = 'TEST'
branch = '02'
pdmConfig = solidworksVersion + '_' + branch #Used to select settings from EPDM_Vault_settings.txt
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def set_reg(key, subKey, name, regType, value):
try:
openKey=CreateKey(key, subKey)
SetValueEx(openKey, name, 0, regType, value)
CloseKey(openKey)
return False
except WindowsError:
print("\n**** error creating key for " + str(key) + ' > ' + str(subKey) + ' > ' + str(name) + ' > ' + str(regType) + ' > ' + str(value) + ' : ' + str(WindowsError))
return True
def get_key(v):
switcher={
"HKEY_CURRENT_USER": HKEY_CURRENT_USER,
"HKEY_LOCAL_MACHINE": HKEY_LOCAL_MACHINE
}
return switcher.get(v, False)
def get_regType(v):
switcher={
'REG_DWORD': 4,
'REG_SZ': 1,
}
return switcher.get(v, False)
def set_dataType(v, t):
if t == 'REG_DWORD':
return int(v)
elif t == 'REG_SZ':
return str(v)
else:
return False
def configure_settings():
with open(settingsData) as json_file:
data = json.load(json_file)
error = False
for p in data[pdmConfig]:
key = get_key(p['key'])
subKeys = p['subKeys']
for e in subKeys:
subKey = e['subKey']
settings = e['settings']
for i in settings:
name = i[0]
regType = get_regType(i[1])
value = set_dataType(i[2], i[1])
print(key, subKey, name, regType, value)
if key is not False and subKey is not False and value is not False:
error = set_reg(key, subKey, name, regType, value)
print(error)
else:
error = True
print('False detected at ' + str(key) + ' > ' + str(subKey) + ' > ' + str(name) + ' > ' + str(regType) + ' > ' + str(value))
if error == False:
print('\n****Success creating registry keys for SolidWorks EPDM configuration ' + pdmConfig + '\n')
else:
print('\n****WARNING: Registry key creation has failed. Check for typos and errors for configuration ' + pdmConfig + ' in ' + settingsData + '.\n')
print('****IMPORTANT: SolidWorks EPDM will not function propertly until the registry keys are created.\n')
if is_admin():
# Code of your program here
print('already running as admin')
configure_settings()
else:
# Re-run the program with admin rights
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
print('switched to running as admin')
configure_settings()
The JSON is as follows:
{
"TEST_02":[
{
"key": "HKEY_CURRENT_USER",
"subKeys": [
{
"subKey": "Software\\SolidWorks\\Solidworks 2017\\TEST FOLDER1",
"settings": [
["", "REG_SZ", "{ED78333F-D5DB-11D4-BD5A-00C04F019808}"]
]
},
{
"subKey": "Software\\SolidWorks\\Solidworks 2017\\TEST FOLDER2",
"settings": [
["the reg_sz", "REG_SZ", "some\\folder\\path"],
["the dword", "REG_DWORD", "123"]
]
}
]
},
{
"key": "HKEY_LOCAL_MACHINE",
"subKeys": [
{
"subKey": "SOFTWARE\\SolidWorks\\SOLIDWORKS 2017\\test folder",
"settings": [
["the dword", "REG_DWORD", "123"]
]
}
]
}
]
}