Windows服务启动时,如何与UI进行交互

可以参考MSDN《.NET Windows编程系列课程(14):Windows 服务 (Level 200)》

   http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/Series/NETWindows.aspx
  1. public partial class Server1 : ServiceBase  
  2. {  
  3.     Thread threadForm = null;  
  4.     public Server1()  
  5.     {  
  6.         InitializeComponent();  
  7.     }  
  8.     [DllImport("user32.dll")]  
  9.     static extern int GetDesktopWindow();  
  10.   
  11.     [DllImport("user32.dll")]  
  12.     static extern IntPtr GetProcessWindowStation();  
  13.   
  14.     [DllImport("kernel32.dll")]  
  15.     static extern IntPtr GetCurrentThreadId();  
  16.   
  17.     [DllImport("user32.dll")]  
  18.     static extern IntPtr GetThreadDesktop(IntPtr dwThread);  
  19.   
  20.     [DllImport("user32.dll")]  
  21.     static extern IntPtr OpenWindowStation(string a, bool b, int c);  
  22.   
  23.     [DllImport("user32.dll")]  
  24.     static extern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags,  
  25.     bool fInherit, uint dwDesiredAccess);  
  26.   
  27.     [DllImport("user32.dll")]  
  28.     static extern IntPtr CloseDesktop(IntPtr p);  
  29.   
  30.     [DllImport("rpcrt4.dll", SetLastError = true)]  
  31.     static extern IntPtr RpcImpersonateClient(int i);  
  32.   
  33.   
  34.     [DllImport("rpcrt4.dll", SetLastError = true)]  
  35.     static extern IntPtr RpcRevertToSelf();  
  36.   
  37.     [DllImport("user32.dll")]  
  38.     static extern IntPtr SetThreadDesktop(IntPtr a);  
  39.   
  40.     [DllImport("user32.dll")]  
  41.     static extern IntPtr SetProcessWindowStation(IntPtr a);  
  42.     [DllImport("user32.dll")]  
  43.     static extern IntPtr CloseWindowStation(IntPtr a);  
  44.   
  45.     protected override void OnStart(string[] args)  
  46.     {  
  47.         threadForm = new Thread(new ThreadStart(FormShow));  
  48.         threadForm.Start();  
  49.     }  
  50.   
  51.     protected override void OnStop()  
  52.     {  
  53.         if (threadForm != null)  
  54.         {  
  55.             if (threadForm.IsAlive)  
  56.             {  
  57.                 threadForm.Abort();  
  58.                 threadForm = null;  
  59.             }  
  60.         }  
  61.   
  62.     }  
  63.     void FormShow()  
  64.     {  
  65.   
  66.         GetDesktopWindow();  
  67.         IntPtr hwinstaSave = GetProcessWindowStation();  
  68.         IntPtr dwThreadId = GetCurrentThreadId();  
  69.         IntPtr hdeskSave = GetThreadDesktop(dwThreadId);  
  70.         IntPtr hwinstaUser = OpenWindowStation("WinSta0"false, 33554432);  
  71.         if (hwinstaUser == IntPtr.Zero)  
  72.         {  
  73.             RpcRevertToSelf();  
  74.             return;  
  75.         }  
  76.         SetProcessWindowStation(hwinstaUser);  
  77.         IntPtr hdeskUser = OpenDesktop("Default", 0, false, 33554432);  
  78.         RpcRevertToSelf();  
  79.         if (hdeskUser == IntPtr.Zero)  
  80.         {  
  81.             SetProcessWindowStation(hwinstaSave);  
  82.             CloseWindowStation(hwinstaUser);  
  83.             return;  
  84.         }  
  85.         SetThreadDesktop(hdeskUser);  
  86.   
  87.         IntPtr dwGuiThreadId = dwThreadId;  
  88.   
  89.         Form1 f = new Form1(); //此FORM1可以带notifyIcon,可以显示在托盘里,用户可点击托盘图标进行设置   
  90.         System.Windows.Forms.Application.Run(f);  
  91.   
  92.   
  93.         dwGuiThreadId = IntPtr.Zero;  
  94.         SetThreadDesktop(hdeskSave);  
  95.         SetProcessWindowStation(hwinstaSave);  
  96.         CloseDesktop(hdeskUser);  
  97.         CloseWindowStation(hwinstaUser);  
  98.     }  
  99.   
  100. }  
    public partial class Server1 : ServiceBase
    {
        Thread threadForm = null;
        public Server1()
        {
            InitializeComponent();
        }
        [DllImport("user32.dll")]
        static extern int GetDesktopWindow();
 
        [DllImport("user32.dll")]
        static extern IntPtr GetProcessWindowStation();
 
        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentThreadId();
 
        [DllImport("user32.dll")]
        static extern IntPtr GetThreadDesktop(IntPtr dwThread);
 
        [DllImport("user32.dll")]
        static extern IntPtr OpenWindowStation(string a, bool b, int c);
 
        [DllImport("user32.dll")]
        static extern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags,
        bool fInherit, uint dwDesiredAccess);
 
        [DllImport("user32.dll")]
        static extern IntPtr CloseDesktop(IntPtr p);
 
        [DllImport("rpcrt4.dll", SetLastError = true)]
        static extern IntPtr RpcImpersonateClient(int i);
 
 
        [DllImport("rpcrt4.dll", SetLastError = true)]
        static extern IntPtr RpcRevertToSelf();
 
        [DllImport("user32.dll")]
        static extern IntPtr SetThreadDesktop(IntPtr a);
 
        [DllImport("user32.dll")]
        static extern IntPtr SetProcessWindowStation(IntPtr a);
        [DllImport("user32.dll")]
        static extern IntPtr CloseWindowStation(IntPtr a);
 
        protected override void OnStart(string[] args)
        {
            threadForm = new Thread(new ThreadStart(FormShow));
            threadForm.Start();
        }
 
        protected override void OnStop()
        {
            if (threadForm != null)
            {
                if (threadForm.IsAlive)
                {
                    threadForm.Abort();
                    threadForm = null;
                }
            }
 
        }
        void FormShow()
        {
 
            GetDesktopWindow();
            IntPtr hwinstaSave = GetProcessWindowStation();
            IntPtr dwThreadId = GetCurrentThreadId();
            IntPtr hdeskSave = GetThreadDesktop(dwThreadId);
            IntPtr hwinstaUser = OpenWindowStation("WinSta0", false, 33554432);
            if (hwinstaUser == IntPtr.Zero)
            {
                RpcRevertToSelf();
                return;
            }
            SetProcessWindowStation(hwinstaUser);
            IntPtr hdeskUser = OpenDesktop("Default", 0, false, 33554432);
            RpcRevertToSelf();
            if (hdeskUser == IntPtr.Zero)
            {
                SetProcessWindowStation(hwinstaSave);
                CloseWindowStation(hwinstaUser);
                return;
            }
            SetThreadDesktop(hdeskUser);
 
            IntPtr dwGuiThreadId = dwThreadId;
 
            Form1 f = new Form1(); //此FORM1可以带notifyIcon,可以显示在托盘里,用户可点击托盘图标进行设置
            System.Windows.Forms.Application.Run(f);
 
 
            dwGuiThreadId = IntPtr.Zero;
            SetThreadDesktop(hdeskSave);
            SetProcessWindowStation(hwinstaSave);
            CloseDesktop(hdeskUser);
            CloseWindowStation(hwinstaUser);
        }
 
    }
原文地址:https://www.cnblogs.com/bdzwater/p/2938707.html