ServerWindows.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. namespace US.Browser.Service
  6. {
  7. public class ServerWindows
  8. {
  9. /// <summary>
  10. /// 开启窗体 使用后用 System.Windows.Forms.Application.Run(_Form);来启动窗体
  11. /// </summary>
  12. /// <param name="s">日志</param>
  13. public static void ServiceForm(System.Diagnostics.EventLog p_ServiceLog)
  14. {
  15. try
  16. {
  17. GetDesktopWindow();
  18. IntPtr _WindowsStation = GetProcessWindowStation();
  19. IntPtr _ThreadID = GetCurrentThreadId();
  20. IntPtr _DeskTop = GetThreadDesktop(_ThreadID);
  21. IntPtr _HwinstaUser = OpenWindowStation("WinSta0", false, 33554432);
  22. if (_HwinstaUser == IntPtr.Zero)
  23. {
  24. RpcRevertToSelf();
  25. return;
  26. }
  27. SetProcessWindowStation(_HwinstaUser);
  28. IntPtr _HdeskUser = OpenDesktop("Default", 0, false, 33554432);
  29. RpcRevertToSelf();
  30. if (_HdeskUser == IntPtr.Zero)
  31. {
  32. SetProcessWindowStation(_WindowsStation);
  33. CloseWindowStation(_HwinstaUser);
  34. return;
  35. }
  36. SetThreadDesktop(_HdeskUser);
  37. IntPtr _GuiThreadId = _ThreadID;
  38. _GuiThreadId = IntPtr.Zero;
  39. SetThreadDesktop(_DeskTop);
  40. SetProcessWindowStation(_WindowsStation);
  41. CloseDesktop(_HdeskUser);
  42. CloseWindowStation(_HwinstaUser);
  43. }
  44. catch (Exception ex)
  45. {
  46. p_ServiceLog.WriteEntry(ex.ToString());
  47. }
  48. }
  49. #region User32.DLL
  50. [DllImport("user32.dll")]
  51. private static extern int GetDesktopWindow();
  52. [DllImport("user32.dll")]
  53. private static extern IntPtr GetProcessWindowStation();
  54. [DllImport("kernel32.dll")]
  55. private static extern IntPtr GetCurrentThreadId();
  56. [DllImport("user32.dll")]
  57. private static extern IntPtr GetThreadDesktop(IntPtr dwThread);
  58. [DllImport("user32.dll")]
  59. private static extern IntPtr OpenWindowStation(string lpszWinSta, bool fInherit, int dwDesiredAccess);
  60. [DllImport("User32.dll")]
  61. private static extern IntPtr OpenDesktop(string lpsxDesktop, uint dwFlags, bool fInherit, uint dwDesiredAccess);
  62. [DllImport("user32.dll")]
  63. private static extern IntPtr CloseDesktop(IntPtr hDesktop);
  64. [DllImport("user32.dll")]
  65. private static extern IntPtr SetThreadDesktop(IntPtr hDesktop);
  66. [DllImport("user32.dll")]
  67. private static extern IntPtr SetProcessWindowStation(IntPtr hWinSta);
  68. [DllImport("user32.dll")]
  69. private static extern IntPtr CloseWindowStation(IntPtr hWinSta);
  70. #endregion
  71. #region Rpcrt4.dll
  72. [DllImport("rpcrt4.dll", SetLastError = true)]
  73. private static extern IntPtr RpcImpersonatClient(int rpc);
  74. [DllImport("rpcrt4.dll", SetLastError = true)]
  75. private static extern IntPtr RpcRevertToSelf();
  76. #endregion
  77. }
  78. }