Search code examples
pythonpython-2.7pywin32

python RegOpenKeyEx() HKEY_LOCAL_MACHINE not defined error


I have been trying to manupulate registry key from python using the win32api module however when I run this three lines of code

import win32api
import win32con

reghandle = win32api.RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Google\\",0,       win32con.KEY_READ)

it gives me back the following error

Traceback (most recent call last): File "C:\EclipseWorkspaces\csse120\MMS-auto\key.py", line 4, in reghandle = win32api.RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\Google\",0, win32con.KEY_READ) NameError: name 'HKEY_LOCAL_MACHINE' is not defined

I've looked up the win32api documentation and I reckon I gave the functinon the right argument... Can anyone pleae help out or give me any hint? thanks in advance


Solution

  • HKEY_LOCAL_MACHINE is a constant defined in the win32con module. You need to use the fully-qualified name. Try this:

    reghandle = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE,"SOFTWARE\\Google\\",0,win32con.KEY_READ)