1

I'm trying to automate a series of GUI controls in a ClickOnce application, however I'm having problems to launch the application through my automation code. What I'd like to do is launch the application with CreateProcess(), so I can have a window handle to execute my controls.

This is my current code (I omitted the path to the app):

import win32process
import win32con

path_to_app = "path_to_application\\application.appref-ms"
startupinfo = win32process.STARTUPINFO()

(hprocess, hthread, dwprocessid, dwthreadid) = win32process.CreateProcess(path_to_app, None, None, None, 0, win32con.NORMAL_PRIORITY_CLASS, None, None, startupinfo)

# Execute controls here

And this is the error I'm getting:

pywintypes.error: (193, 'CreateProcess', '%1 is not a valid Win32 application.')

If I try to directly open the .exe that will eventually run, it fails with a message that I should run through the shortcut (in this case the .appref-ms).

How is it possible to start the application and get its window handle?

Bernardo
  • 119
  • 1
  • 10
  • Take a look at the shortcut to see what else it does - it might have some command-line arguments. – cdarke Mar 22 '15 at 21:17
  • Already did that. It simply launches the application. – Bernardo Mar 22 '15 at 21:23
  • 1
    There must be some way 'it' knows that the .exe has not been launched from the shortcut. Anyway, another possibility is to launch the shortcut from Python using `subprocess.Popen` with `shell=True`. Popen allows STARTUPINFO and PROCESS_INFORMATION, see the doc. – cdarke Mar 23 '15 at 07:23
  • I believe the `.exe` knows whether it has been launched from the shortcut because ClickOnce seems to act as a middle layer. I have tested using `subprocess.Popen` with `shell=True` and I was able to successfully launch the application. However, the process that is started through this seems to be only the ClickOnce process (which will then start the `.exe`), so I can't even get the PID to try and get the window handle. I can't guarantee the title of the application will be always the same, and the window class is randomly created (seems to be created by ClickOnce). How can I proceed with that? – Bernardo Mar 24 '15 at 01:52
  • That's about as far as I can go I'm afraid. Sounds like you should contact your `ClickOnce` support route. – cdarke Mar 24 '15 at 10:55

2 Answers2

0

Generally to get a click once application to run from within another application you have to first run dfsvc.exe from the .Net framework runtime directory. (see https://stackoverflow.com/a/11996812/3803708). Then try running the .appref-ms and hopefully it will work.

Community
  • 1
  • 1
bmadtiger
  • 671
  • 6
  • 17
  • I managed to open ClickOnce through `dfsvc.exe`, however launching the `.appref-ms` through `CreateProcess()` still gives the same error. The command in the linked answer uses `rundll32` to launch the application, however when trying the same (using a local path instead of a network one) I get an error with wrong path/application. Any ideas? – Bernardo Apr 02 '15 at 21:46
  • Sorry @Bernardo, I'm not familiar with how python's `CreateProcess()` works, but in .Net we MUST supply a valid path for the Working Directory as well as the full path of the executable. I hope this helps. – bmadtiger Apr 10 '15 at 22:05
0

This code works for me:

import subprocess, time
import pywinauto

p = subprocess.Popen(['cmd.exe', '/c', r'C:\Users\<user_name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\GitHub, Inc\GitHub.appref-ms'])
time.sleep(5)
app = pywinauto.Application.connect(path='github.exe')
dlg = app.Windows_(visible_only=True)[0]
print dlg.handle

Of course, you need to install


The code was tested on 32-bit github application, Win7 x64, Python 2.6 32-bit (pywinauto-64 clone).

Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78