WPF UI 自动化:来自 Windows 服务

WPF UI Automation: from Windows Service

我有一个应用程序使用 UI 自动化 API 与其他 WPF 应用程序一起工作。 我创建了 Windows 服务,它使用相同的功能,但是当服务试图找到 window hwd 时,它收到 NULL:

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

是否可以在 Windows 服务中使用 UI 自动化 API?

A Windows 服务无权访问用户的桌面。参见:Interaction between Windows service and desktop application

所以简短的回答是:没有。

一种解决方案可能是通过套接字将服务 'talk' 提供给桌面上的进程,并让该进程执行自动化。 (这可能会导致摆脱服务并将所有逻辑移动到桌面应用程序)