0

I have an application which work using UI Automation API with other WPF application. I have created Windows service which use the same functionality but when the service tries to find the window hwd it receives NULL:

System.ArgumentException: hwnd cannot be IntPtr.Zero or null.
at System.Windows.Automation.AutomationElement.FromHandle(IntPtr hwnd)
at MyService.Scheduler.GetNotificationWindow(IntPtr hWndtoolBar)

Is it possible to use UI Automation API in Windows Service?

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
ZedZip
  • 5,794
  • 15
  • 66
  • 119
  • 1
    I dont think this is possible or easy to achieve due to [Session 0 isolation](https://blogs.technet.microsoft.com/askperf/2007/04/27/application-compatibility-session-0-isolation/) – lokusking Nov 23 '16 at 11:24

1 Answers1

0

A Windows Service does not have access to the user's desktop. See: Interaction between Windows service and desktop application

So the short answer is: no.

A solution might be to have the service 'talk' to a process on the desktop through a socket and have this process do the automation. (That might lead to getting rid of the service and moving all logic to the desktop app)

Community
  • 1
  • 1
Emond
  • 50,210
  • 11
  • 84
  • 115
  • I thought about service only because it starts when OS restarted. May be it is easier to create a small "stupid" service which checks and runs the application if it is not active. And this app uses UIAtomation API and work as now. – ZedZip Nov 23 '16 at 11:38
  • It is even 'easier' to make an application that hides in the system tray and starts with the logging in from the user. That way you do not have a spying application on your machine that tries to spy the desktop of user. – Emond Nov 23 '16 at 11:40