[python-win32] pywintypes.error: (5, 'RegSetValueEx', 'Access denied')

eryk sun eryksun at gmail.com
Thu Mar 15 19:31:15 EDT 2018


On Thu, Mar 15, 2018 at 8:44 AM, Robin Kluth <kontakt at kluthr.de> wrote:
>
> I use Python 2.7 on win10 and pywin32 223. I want to log messqages to the
> EventLog:
>
> dllname = os.path.dirname(__file__)
> ntl = NTEventLogHandler("Python Logging Test", dllname=dllname)

The Python module should not be used for dllname. This needs to be a
PE image (e.g. PYD, DLL, EXE) that has an embedded message table.
NTEventLogHandler currently just hard codes using event ID 1, which is
"%1\r\n" in win32evtlog.pyd or win32service.pyd. The "%1" insert gets
replaced by the message text when the log record is formatted.

In your case, just leave it as the default.

> Traceback (most recent call last):
>   File "fpp_user_import.py", line 44, in <module>
>     ntl = NTEventLogHandler("Python Logging Test", dllname=dllname)
>   File "C:\Python27\lib\logging\handlers.py", line 987, in __init__
>     self._welu.AddSourceToRegistry(appname, dllname, logtype)
>   File "C:\Python27\lib\site-packages\win32\lib\win32evtlogutil.py", line
> 42, in AddSourceToRegistry
>     msgDLL)
> pywintypes.error: (5, 'RegSetValueEx', 'Zugriff verweigert')
>
> If I run the script with Admin privs, it is working.
>
> How to log as "normal" user? Am I missing something?

This should be enhanced in NTEventLogHandler. It's not the end of the
world if this key doesn't get created. The event viewer will just show
a warning that the event ID can't be found for the given source. Also,
when installed an application can request elevation to register the
event log source. Given this, I suggest you work around the problem
with a subclass that ignores this error. For example:

    import logging
    import logging.handlers

    class NTEventLogHandler(logging.handlers.NTEventLogHandler):
        __doc__ = logging.handlers.NTEventLogHandler.__doc__
        def __init__(self, appname, dllname=None, logtype="Application"):
            logging.Handler.__init__(self)
            self.appname = appname
            self.dllname = dllname
            self.logtype = logtype
            try:
                import win32evtlogutil, win32evtlog, winerror
            except ImportError:
                print('The Python Win32 extensions for NT (service, event '
                      'logging) appear not to be available.')
                self._welu = None
                return
            self._welu = win32evtlogutil
            self.deftype = win32evtlog.EVENTLOG_ERROR_TYPE
            self.typemap = {
                logging.DEBUG   : win32evtlog.EVENTLOG_INFORMATION_TYPE,
                logging.INFO    : win32evtlog.EVENTLOG_INFORMATION_TYPE,
                logging.WARNING : win32evtlog.EVENTLOG_WARNING_TYPE,
                logging.ERROR   : win32evtlog.EVENTLOG_ERROR_TYPE,
                logging.CRITICAL: win32evtlog.EVENTLOG_ERROR_TYPE,
            }
            try:
                win32evtlogutil.AddSourceToRegistry(appname, dllname, logtype)
            except win32evtlogutil.error as e:
                if e.winerror != winerror.ERROR_ACCESS_DENIED:
                    raise


More information about the python-win32 mailing list