Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

opening another program through windows service using python

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...