OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python winreg says it opened a key that doesn't exist in my registry

  • Thread starter Thread starter not_a_generic_user
  • Start date Start date
N

not_a_generic_user

Guest
I'm trying to work with a registry key and my code tries to open the key and creates it if it's missing. The problem: it says it's there when it's NOT.

My registry has: Software\Classes\CLSID
But does not have: {86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32

This is the code:

Code:
import winreg

def hive_name(hive):
    if hive == winreg.HKEY_CURRENT_USER:
        return "HKEY_CURRENT_USER"
    elif hive == winreg.HKEY_LOCAL_MACHINE:
        return "HKEY_LOCAL_MACHINE"
    elif hive == winreg.HKEY_CLASSES_ROOT:
        return "HKEY_CLASSES_ROOT"
    elif hive == winreg.HKEY_USERS:
        return "HKEY_USERS"
    elif hive == winreg.HKEY_PERFORMANCE_DATA:
        return "HKEY_PERFORMANCE_DATA"
    elif hive == winreg.HKEY_CURRENT_CONFIG:
        return "HKEY_CURRENT_CONFIG"
    else:
        return "UNKNOWN_HIVE"

def open_or_create_key(hive, path):
    try:
        # Open the registry key for reading and writing
        key = winreg.OpenKey(hive, path, 0, winreg.KEY_READ | winreg.KEY_WRITE)
        print(f"Key opened: {hive_name(hive)}\\{path}")
    except FileNotFoundError:
        # Handle if the key doesn't exist
        print(f"Creating key: {hive_name(hive)}\\{path}")
        key = winreg.CreateKey(hive, path)
    except PermissionError:
        # Handle if there are permission issues
        print(f"Permission denied while accessing the key: {hive_name(hive)}\\{path}")
        key = None
    except Exception as e:
        # Handle any other exceptions
        print(f"An error occurred: {e}")
        key = None
    return key

def close_key(key):
    if key:
        winreg.CloseKey(key)
        print("Key closed.")

# Test the open_or_create_key function
if __name__ == "__main__":
    hive = winreg.HKEY_CURRENT_USER
    path = r"Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32"

    # Call the function with the test key
    key = open_or_create_key(hive, path)

    if key:
        # The key was successfully opened or created
        print("Test passed: Key opened or created successfully.")
        close_key(key)
    else:
        # The key could not be opened or created
        print("Test failed: Key could not be opened or created.")

When I run the above (in VsCode terminal with VsCode run as admin), I get the following output:

Code:
Key opened: HKEY_CURRENT_USER\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32
Test passed: Key opened or created successfully.
Key closed.

The output I want is for it to fail and then create the missing keys in the path. At the very least, it shouldn't show the key exists when it doesn't.

I have tried:

  • Opening and closing the registry
  • Restarting the Windows explorer process

Neither had any effect despite python seeming to think the key is there when it's not.
<p>I'm trying to work with a registry key and my code tries to open the key and creates it if it's missing. The problem: it says it's there when it's NOT.</p>
<p>My registry has: Software\Classes\CLSID<br />
But does not have: {86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32</p>
<p>This is the code:</p>
<pre class="lang-py prettyprint-override"><code>import winreg

def hive_name(hive):
if hive == winreg.HKEY_CURRENT_USER:
return "HKEY_CURRENT_USER"
elif hive == winreg.HKEY_LOCAL_MACHINE:
return "HKEY_LOCAL_MACHINE"
elif hive == winreg.HKEY_CLASSES_ROOT:
return "HKEY_CLASSES_ROOT"
elif hive == winreg.HKEY_USERS:
return "HKEY_USERS"
elif hive == winreg.HKEY_PERFORMANCE_DATA:
return "HKEY_PERFORMANCE_DATA"
elif hive == winreg.HKEY_CURRENT_CONFIG:
return "HKEY_CURRENT_CONFIG"
else:
return "UNKNOWN_HIVE"

def open_or_create_key(hive, path):
try:
# Open the registry key for reading and writing
key = winreg.OpenKey(hive, path, 0, winreg.KEY_READ | winreg.KEY_WRITE)
print(f"Key opened: {hive_name(hive)}\\{path}")
except FileNotFoundError:
# Handle if the key doesn't exist
print(f"Creating key: {hive_name(hive)}\\{path}")
key = winreg.CreateKey(hive, path)
except PermissionError:
# Handle if there are permission issues
print(f"Permission denied while accessing the key: {hive_name(hive)}\\{path}")
key = None
except Exception as e:
# Handle any other exceptions
print(f"An error occurred: {e}")
key = None
return key

def close_key(key):
if key:
winreg.CloseKey(key)
print("Key closed.")

# Test the open_or_create_key function
if __name__ == "__main__":
hive = winreg.HKEY_CURRENT_USER
path = r"Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32"

# Call the function with the test key
key = open_or_create_key(hive, path)

if key:
# The key was successfully opened or created
print("Test passed: Key opened or created successfully.")
close_key(key)
else:
# The key could not be opened or created
print("Test failed: Key could not be opened or created.")
</code></pre>
<p>When I run the above (in VsCode terminal with VsCode run as admin), I get the following output:</p>
<pre><code>Key opened: HKEY_CURRENT_USER\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32
Test passed: Key opened or created successfully.
Key closed.
</code></pre>
<p>The output I want is for it to fail and then create the missing keys in the path. At the very least, it shouldn't show the key exists when it doesn't.</p>
<p>I have tried:</p>
<ul>
<li>Opening and closing the registry</li>
<li>Restarting the Windows explorer process</li>
</ul>
<p>Neither had any effect despite python seeming to think the key is there when it's not.</p>
 

Latest posts

Online statistics

Members online
1
Guests online
2
Total visitors
3
Top