简体   繁体   中英

Call .exe from windows system service python?

I have a Windows System Service that I am trying to write. I'm trying to an interface for a POS machine, so ideally I would like to include this code inside of the system service. However some experimentation has lead me to believe that the windows system service will only execute basic tasks and not oter iterations.

I have another function that I need to call every x seconds, this additional function is a while loop, but I cannot get my function and the win32 loop to wait for system calls to play nicely together. I go into greater detail in my code below.

import win32service  
import win32serviceutil  
import win32event

class PySvc(win32serviceutil.ServiceFramework):  
    # net name  
    _svc_name_ = "test"  

    _svc_display_name_ = "test"  

    _svc_description_ = "Protects your computer."  

    def __init__(self, args):  
        win32serviceutil.ServiceFramework.__init__(self,args)  
        # create an event to listen for stop requests on  
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)


    # core logic of the service     
    def SvcDoRun(self):


        # if the stop event hasn't been fired keep looping
        while rc != win32event.WAIT_OBJECT_0:




            # block for 60 seconds and listen for a stop event  
            rc = win32event.WaitForSingleObject(self.hWaitStop, 60000)

        ## I want to put an additional function that uses a while loop here.
        ## The service will not work correctly with additional iterations, inside or 
        ## the above api calls.    
        ## Due to the nature of the service and the api call above, 
        ## this leads me to have to compile an additional .exe and somehow call that 
        ## from the service.     

    # called when we're being shut down      

    def SvcStop(self):  
            # tell the SCM we're shutting down  
            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)  
            # fire the stop event  
            win32event.SetEvent(self.hWaitStop)  

if __name__ == '__main__':  

    win32serviceutil.HandleCommandLine(PySvc) 

My research has shown me that I need to somehow call a .exe from a windows system service. Does anyone know how to do this? I have tried using os.system, and variant calls of the subprocess module to no avail, it seems that windows simply ignores them. Any ideas?

EDIT: revert to original question

Can't say as I'm familiar with Windows development but in *nix I've found sockets are very useful in situations where two things shouldn't be able to talk by definition but you need them to anyway eg making web browsers launch desktop apps, making the clipboard interact with the browser etc.

In most cases UDP sockets are all that you need for a little IPC and they are trivial to code for in Python. You do have to be extra careful though, often restrictions are there for a good reason and you need to really understand a rule before you go breaking it... Bear in mind anyone can send a UDP packet so make sure the receiving app only accept packets from localhost and make sure you sanity check all incoming packets to protect against local hackers/malware. If the data transmitted is particularly sensitive or the action initiated is powerful it may not be a good idea at all, only you know your app well enough to say really.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM