I'm attempting to start a service that in turn requires the use of a separate application (EBSILON Professional; https://www.ebsilon.com/en/). The application is installed on the server, and when using pywin32 to access it using COM this works as expected in a simple Python script.
However, when starting a Windows Service performing the same operation, the dispatch times out.
The service is running on Windows Server 2025 Datacenter and Python 3.13.1. Here is a sample script exhibiting the problem (dispatch-test.py):
import win32serviceutil
import win32service
import win32event
import win32com
import win32com.client
import servicemanager
import socket
import time
import logging
import sys
def configure_logging():
logging.basicConfig(
filename = r'C:\Users\Administrator\Desktop\dispatch-test.log',
level = logging.DEBUG,
format = '[helloworld-service] %(levelname)-7.7s %(message)s'
)
def dispatch():
try:
win32com.client.Dispatch('EbsOpen.Application')
logging.info('Successful')
except Exception as e:
logging.error('Failed', exc_info=True)
class HelloWorldSvc(win32serviceutil.ServiceFramework):
_svc_name_ = "Dispatch-Test"
_svc_display_name_ = "Dispatch Test"
def __init__(self, args):
super().__init__(args)
configure_logging()
def SvcStop(self):
logging.info('Stopping service ...')
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
def SvcDoRun(self):
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, '')
)
logging.info('Running service ...')
dispatch()
if __name__ == '__main__':
if len(sys.argv) == 1:
configure_logging()
dispatch()
else:
win32serviceutil.HandleCommandLine(HelloWorldSvc)
The output for py dispatch-test.py
(direct approach):
[helloworld-service] INFO Successful
And for py dispatch-test.py install; py dispatch-test.py start
(via service):
[helloworld-service] INFO Running service ...
[helloworld-service] ERROR Failed
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python313\Lib\site-packages\win32com\client\dynamic.py", line 80, in _GetGoodDispatch
IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221021, 'Operation unavailable', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Administrator\DEsktop\helloworld-service.py", line 23, in dispatch
win32com.client.Dispatch('EbsOpen.Application')
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python313\Lib\site-packages\win32com\client\__init__.py", line 114, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch, userName, clsctx)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python313\Lib\site-packages\win32com\client\dynamic.py", line 100, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python313\Lib\site-packages\win32com\client\dynamic.py", line 82, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(
IDispatch, None, clsctx, pythoncom.IID_IDispatch
)
pywintypes.com_error: (-2146959355, 'Server execution failed', None, None)
Both commands are run in PowerShell as Administrator
.
The System
logs show this about two minutes after service start:
Log Name: System
Source: Microsoft-Windows-DistributedCOM
Date: 1/15/2025 8:04:40 PM
Event ID: 10010
Task Category: None
Level: Error
Keywords: Classic
User: SYSTEM
Computer: EC2AMAZ-M0C7SQ4
Description:
The server {F1A4BB7E-1E45-4040-ACBC-4E2600010118} did not register with DCOM within the required timeout.
Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="Microsoft-Windows-DistributedCOM" Guid="{1B562E86-B7AA-4131-BADC-B6F3A001407E}" EventSourceName="DCOM" />
<EventID Qualifiers="0">10010</EventID>
<Version>0</Version>
<Level>2</Level>
<Task>0</Task>
<Opcode>0</Opcode>
<Keywords>0x8080000000000000</Keywords>
<TimeCreated SystemTime="2025-01-15T20:04:40.5916644Z" />
<EventRecordID>14925</EventRecordID>
<Correlation ActivityID="{9df83d36-66ae-0000-8ec3-309eae66db01}" />
<Execution ProcessID="952" ThreadID="9044" />
<Channel>System</Channel>
<Computer>EC2AMAZ-M0C7SQ4</Computer>
<Security UserID="S-1-5-18" />
</System>
<EventData>
<Data Name="param1">{F1A4BB7E-1E45-4040-ACBC-4E2600010118}</Data>
</EventData>
</Event>
Thinking I might have a permission issue when running the service, I attempted to explicitly set which user starts the service process as follows (with the actual password):
from win32service import OpenSCManager, SC_MANAGER_ALL_ACCESS, OpenService, SERVICE_ALL_ACCESS, SERVICE_NO_CHANGE, ChangeServiceConfig
sc_manager = OpenSCManager(None, None, SC_MANAGER_ALL_ACCESS)
my_service = OpenService(sc_manager, 'Dispatch-Test', SERVICE_ALL_ACCESS)
ChangeServiceConfig(my_service, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, None, None, False, None, r'.\Administrator', '<redacted>', None)
This completes successfully, but does not help the issue: the service still starts and reports the same stack trace in the logs.
Static dispatch/early binding fails similarly.
It's hard to tell for sure without having access to the program you are trying to start, but a limitation with services is that they do not have access to all APIs a normal Windows program has. If the program you start from your Python service tries to call any "forbidden" API, it will just crash, or if it waits for user input, it will hang.
See: Opening a Microsoft Word document in a Windows service seems to hang which is a about a similar situation.