WindowsError: [Error 5] Access is denied With _winreg.enum

Discussion in 'Python' started by black_13, Nov 28, 2007.

  1. black_13

    black_13 Guest

    I have included a small script the reproduces the error I am having in
    larger script.
    The line 'hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,name)'
    seems
    to be causing the error but im not sure why.
    --------------------- script ----------------

    import _winreg
    import string

    def reproduce_error():

    for index in
    xrange(_winreg.QueryInfoKey(_winreg.HKEY_LOCAL_MACHINE)[0]):
    #get names
    name =
    _winreg.EnumKey(_winreg.HKEY_LOCAL_MACHINE,index)
    hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,name)
    print name

    hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,'SOFTWARE')
    print hkey
    hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,'SAM')
    print hkey

    if __name__ == '__main__':
    reproduce_error()
    ------------------- end script
    -----------------------------------------
    HARDWARE
    SAM
    Traceback (most recent call last):
    File "winreg_error.py", line 19, in <module>
    reproduce_error()
    File "winreg_error.py", line 10, in reproduce_error
    hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,name)
    WindowsError: [Error 5] Access is denied
     
    black_13, Nov 28, 2007
    #1
    1. Advertising

  2. black_13

    Jerry Hill Guest

    On Nov 28, 2007 11:04 AM, black_13 <> wrote:
    > I have included a small script the reproduces the error I am having in
    > larger script. The line 'hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,name)'
    > seems to be causing the error but im not sure why.

    ....
    > WindowsError: [Error 5] Access is denied


    Your user does not have permission to open the registry key you
    requested. On my local machine, HKEY_LOCAL_MACHINE/SECURITY is only
    accessible by the SYSTEM account. Even Administrative users do not
    have Read rights to that key by default.

    If you want to skip keys you don't have permission to access, you
    could do something like this:

    import _winreg

    for index in xrange(_winreg.QueryInfoKey(_winreg.HKEY_LOCAL_MACHINE)[0]):
    try:
    name = _winreg.EnumKey(_winreg.HKEY_LOCAL_MACHINE,index)
    hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,name)
    except WindowsError:
    print "Could not access registry key", name
    else:
    print name, hkey

    --
    Jerry
     
    Jerry Hill, Nov 28, 2007
    #2
    1. Advertising

  3. black_13

    Guest

    On Nov 28, 10:04 am, black_13 <> wrote:
    > I have included a small script the reproduces the error I am having in
    > larger script.
    > The line 'hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,name)'
    > seems
    > to be causing the error but im not sure why.
    > --------------------- script ----------------
    >
    > import _winreg
    > import string
    >
    > def reproduce_error():
    >
    > for index in
    > xrange(_winreg.QueryInfoKey(_winreg.HKEY_LOCAL_MACHINE)[0]):
    > #get names
    > name =
    > _winreg.EnumKey(_winreg.HKEY_LOCAL_MACHINE,index)
    > hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,name)
    > print name
    >
    > hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,'SOFTWARE')
    > print hkey
    > hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,'SAM')
    > print hkey
    >
    > if __name__ == '__main__':
    > reproduce_error()
    > ------------------- end script
    > -----------------------------------------
    > HARDWARE
    > SAM
    > Traceback (most recent call last):
    > File "winreg_error.py", line 19, in <module>
    > reproduce_error()
    > File "winreg_error.py", line 10, in reproduce_error
    > hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,name)
    > WindowsError: [Error 5] Access is denied


    If you move the "print name" line up one line, you'll notice that it's
    choking on the Security key. Go to Start --> Run and type regedit. Go
    to that key and right-click it and check its permissions.

    On my PC the only user that has complete control is the SYSTEM. So
    you'll probably just want to put an exception block in your function
    that records the keys that you can't open and continues to run.

    Mike
     
    , Nov 28, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sparky
    Replies:
    3
    Views:
    2,573
    Sparky
    Aug 4, 2009
  2. Nick Stinemates
    Replies:
    0
    Views:
    547
    Nick Stinemates
    Nov 17, 2009
  3. Mark Hammond
    Replies:
    0
    Views:
    695
    Mark Hammond
    Nov 18, 2009
  4. News123
    Replies:
    4
    Views:
    802
    Dennis Lee Bieber
    Mar 7, 2010
  5. Andrew Berg
    Replies:
    8
    Views:
    1,219
    Andrew Berg
    May 27, 2011
Loading...

Share This Page