jithurjacob / Windows-10-Toast-Notifications

Python library to display Windows 10 Toast Notifications
MIT License
970 stars 168 forks source link

windows python service error: (-2147467259, 'Shell_NotifyIcon', 'Unspecified error') #23

Closed hoatk closed 6 years ago

hoatk commented 6 years ago

I wrote code below:

    import pythoncom
    import win32serviceutil
    import win32service
    import win32event
    import servicemanager
    import socket
    import time
    import sys
    import pyodbc
    from win10toast import ToastNotifier

    class AlertSertvice(win32serviceutil.ServiceFramework):
        _svc_name_ = "Alert"
        _svc_display_name_ = "Alert"
        _svc_description_ = "Notification of Windows"
        def __init__(self, args):
            win32serviceutil.ServiceFramework.__init__(self, args)
            self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
            socket.setdefaulttimeout(600)

        def SvcDoRun(self):
            #servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,  servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, ''))
            #self.main()
    #    def main(self):
            rc = None
            while rc != win32event.WAIT_OBJECT_0:
                try:
                    toaster = ToastNotifier()
                    toaster.show_toast('test',duration=10)
                except:
                    servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, sys.exc_info()[0]))
                rc = win32event.WaitForSingleObject(self.hWaitStop, 10*1000)

        def SvcStop(self):
            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
            win32event.SetEvent(self.hWaitStop)

    if __name__ == '__main__':
        if len(sys.argv) == 1:
            servicemanager.Initialize()
            servicemanager.PrepareToHostSingle(AlertSertvice)
            servicemanager.StartServiceCtrlDispatcher()
        else:
            win32serviceutil.HandleCommandLine(AlertSertvice)

Then compile and install service (python AlertService.py install) then start it.

It dump an error on Event Viewer: The instance's SvcRun() method failed Traceback (most recent call last): File "C:\PYTHON27\lib\site-packages\pypiwin32-219-py2.7-win-amd64.egg\win32serviceutil.py", line 835, in SvcRun self.SvcDoRun() File "D:\DB\Python\Test\AlertService.py", line 33, in SvcDoRun toaster.show_toast('test',duration=10) File "build\bdist.win-amd64\egg\win10toast__init__.py", line 115, in show_toast Shell_NotifyIcon(NIM_ADD, nid) error: (-2147467259, 'Shell_NotifyIcon', 'Unspecified error') %2: %3

Please help me fix, thank so much

jithurjacob commented 6 years ago

Hi, as mentioned in #18 this requires some serious understanding of Win API's which unfortunately I don't possess. Could you please post it on stack overflow?

yanxun827 commented 6 years ago

I tested this code on a Windows 10:

from win10toast import ToastNotifier                                                                
toaster = ToastNotifier()                                                                           

toaster.show_toast("TEST", duration=5)

The code runs fine and got expected results when being run on native shell.

However I got the error when I run the code through a SSH connection from other machine.