How to fix "Could not convert the data to the specified type" error when using Python winreg to edit REG_DWORD value

0

I want to create a random hex string generator and change the value of "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\InstallDate" to the random hex string, but I failed with the error "ValueError: Could not convert the data to the specified type."

I tried to execute the "hex_generator()" separately and copy the result to replace the function call to "hex_generator()", that works for me (For example : winreg.SetValueEx(key, 'InstallDate', 0, winreg.REG_DWORD, 0xFFFFFFFF) it will successfully set the value of "InstallDate" to 0xFFFFFFFF) I do not know why the error happened, is my code totally wrong?

'''python import winreg,random

def hex_generator(): # For InstallDate return "0x" + "".join(random.sample('0123456789ABCDEF', 8))

key = winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion", 0, winreg.KEY_ALL_ACCESS) winreg.SetValueEx(key, 'InstallDate', 0, winreg.REG_DWORD, hex_generator()) '''

https://imgur.com/ev2s6hQ "the code"

I expect I can set the value of "InstallDate" with the result of the function "hex_generator()" directly instead of copy and paste a value. Thanks

python
winreg
asked on Stack Overflow Apr 16, 2019 by Black Frank

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0