1

I'm trying to open/execute another program through windows service using python code. When the windows service gets started, another program i.e Notepad will be executed. The code is fine without error but it doesn't open the program. Code is given below.

Code:

import win32serviceutil
import win32service
import win32event
import win32com.shell.shell as w32shell
import os
import sys
import win32process as process

class SmallestPythonService(win32serviceutil.ServiceFramework):
  _svc_name_ = "BSmallestPythonService"
  _svc_display_name_ = "BSmallest possible Python Service"
def __init__(self, args):
    win32serviceutil.ServiceFramework.__init__(self, args)
    # Create an event which we will use to wait on.
    # The "service stop" request will set this event.
    self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)


def SvcStop(self):
    # Before we do anything, tell the SCM we are starting the stop process.
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    # And set my event.
    win32event.SetEvent(self.hWaitStop)

def SvcDoRun(self):
    win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
    import subprocess
    cmd = "notepad.exe"
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, creationflags=0x08000000)
    process.wait()

if __name__=='__main__':
    win32serviceutil.HandleCommandLine(SmallestPythonService)

In SvcDoRun method i've tried the following code but no success:

import subprocess
subprocess.Popen('calc.exe', shell=False)

Also tried but no success:

import subprocess 
subprocess.call('notepad.exe', shell=False)

also tried but no success:

import win32api
win32api.WinExec('NOTEPAD.exe') # Works seamlessly

I'm missing something? or I'm doing it in the wrong way! Please Help

1 Answer 1

3

Windows services run in session 0, and interactive programs run in a different session. Typically this will be session 1 when there is a single logged on user. Now, your code will be creating processes in session 0, since it runs in session 0. And so the interactive user desktop in session 1 cannot interact with those processes.

It is possible to start processes the run in a different session from the process parent, but it is not at all easy: http://blogs.msdn.com/b/winsdk/archive/2009/07/14/launching-an-interactive-process-from-windows-service-in-windows-vista-and-later.aspx

One possible way out for you is to run a background process that is started when each user logs on. The service can communicate with the background process using IPC and ask the background process to do the leg work of starting the process in the interactive desktop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.