Pythonist
Pythonist

Reputation: 11

Python Script to Exe Win32api not working

I tried looking for a similar problem that someone perhaps had but could not find. Long story short. I put together a python script that creates a files and writes to it and reads from it. I then created the .exe with nssm-2.24 and installed the exe as a service and it works.

I then created a second script that invokes the win32api and win32con from the python modules and just moves the mouse around on the screen and performs a click or two. This work from within python and when compiled as .exe.

When I install the second exe as a service it shows up as a service and runs but the mouse does not move across the screen.

Code below:
import autopy
import time
import win32api
import win32con

def click(x, y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

def move_mouse_around():
    time.sleep(300)
    autopy.mouse.smooth_move(500,500)
    time.sleep(.`enter code here`5)
    autopy.mouse.smooth_move(200,200)
    time.sleep(1.5)
    autopy.mouse.smooth_move(200,600)
    time.sleep(1)
    autopy.mouse.smooth_move(200,500)
    time.sleep(1)
    autopy.mouse.smooth_move(200,400)
    time.sleep(1)
    autopy.mouse.smooth_move(400,200)
    time.sleep(.5)
    autopy.mouse.smooth_move(400,300)
    time.sleep(.5)
    autopy.mouse.smooth_move(400,400)
    time.sleep(.5)
    autopy.mouse.smooth_move(400,450)
    time.sleep(1)

    autopy.mouse.smooth_move(50,50)
    time.sleep(1)

    click(50,50)
    click(50,50)
    time.sleep(.5)
    autopy.mouse.smooth_move(50,150)
    click(50,150)

    autopy.mouse.smooth_move(500,500)
    time.sleep(.5)
    autopy.mouse.smooth_move(200,200)
    time.sleep(1.5)
    autopy.mouse.smooth_move(200,600)
    time.sleep(1)
    autopy.mouse.smooth_move(200,500)
    time.sleep(1)
    autopy.mouse.smooth_move(200,400)
    time.sleep(1)
    autopy.mouse.smooth_move(400,200)
    time.sleep(.5)
    autopy.mouse.smooth_move(400,300)
    time.sleep(.5)
    autopy.mouse.smooth_move(400,400)
    time.sleep(.5)
    autopy.mouse.smooth_move(400,450)
    time.sleep(1)

    autopy.mouse.smooth_move(17,50)
    click(17,50)
    click(17,50)
    time.sleep(1.5)


def close_window():
    autopy.mouse.smooth_move(1360,5)
    click(1360,5)
    time.sleep(30)




#close_window()


while True:
    move_mouse_around()

Upvotes: 0

Views: 294

Answers (1)

David Heffernan
David Heffernan

Reputation: 613461

Services execute in a non-interactive session, session 0, and are therefore isolated from the user's interactive desktop. Users have desktops in session 1, session 2 etc.

What all of this means is that you cannot interact with a user's desktop from a service. You will have to execute this code in the user's session, on the same desktop as the user.

Upvotes: 2

Related Questions