#author("2023-04-11T09:19:38+08:00","default:Admin","Admin") #author("2023-04-11T09:21:08+08:00","default:Admin","Admin") [[Windows Service]] &color(red){※前提条件:本情報はAnt Design Vue 1.3.10を基づいて説明してる}; * 概要 [#p9d53ad0] 因为windows 服务和Application不是一个session,具体参考[[Windows技术架构>+.NET+技术架构#yc8dc063]] 因为有Session隔离机制的存在 从windows Service里面向桌面发送消息等有交互的操作必须使用 wtsapi32.h 标头来实现 https://learn.microsoft.com/en-us/windows/win32/api/wtsapi32/ 例如 消息提示框:WTSSendMessage ~ 启动其他的程序:CreateProcessAsUser~ * 例程 [#a0edccb0] 对于简单的交互,服务可以通过WTSSendMessage 函数,在用户Session 上显示消息窗口。对于一些复杂的UI 交互,必须调用CreateProcessAsUser 或其他方法(WCF、.NET远程处理等)进行跨Session 通信,在桌面用户上创建一个应用程序界面。 使用WTSSendMessage 函数实现服务简单的向桌面用户Session 发送消息窗口的例子代码: #codeprettify{{ public static IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero; public static void ShowMessageBox(string message, string title) { int resp = 0; WTSSendMessage( WTS_CURRENT_SERVER_HANDLE, WTSGetActiveConsoleSessionId(), title, title.Length, message, message.Length, 0, 0, out resp, false); } [DllImport("kernel32.dll", SetLastError = true)] public static extern int WTSGetActiveConsoleSessionId(); [DllImport("wtsapi32.dll", SetLastError = true)] public static extern bool WTSSendMessage( IntPtr hServer, int SessionId, String pTitle, int TitleLength, String pMessage, int MessageLength, int Style, int Timeout, out int pResponse, bool bWait); }} 在windows service里面调用 #codeprettify{{ protected override void OnStart(string[] args) { Interop.ShowMessageBox("This a message from AlertService.","AlertService Message"); } }} * 参考网址 [#a2d2a8b1] https://blog.csdn.net/technologyleader/article/details/125068329 #hr(); コメント: #comment_kcaptcha