111,101
社区成员




/// <summary>
/// 启动服务
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
uiThread = new Thread(new ThreadStart(StartService.FormShow));
uiThread.Start();
//this.StartProcessor();
}
#region 服务启动窗体
public static 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;
CtrlForm f = new CtrlForm();
System.Windows.Forms.Application.Run(f);
dwGuiThreadId = IntPtr.Zero;
SetThreadDesktop(hdeskSave);
SetProcessWindowStation(hwinstaSave);
CloseDesktop(hdeskUser);
CloseWindowStation(hwinstaUser);
}
[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);
#endregion
#region 启动WCF服务
private void LoadService()
{
if (dicHost == null || dicHost.Count == 0)
{
dicHost = new Dictionary<string, ServiceHost>();
Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
ServiceModelSectionGroup svcmod = (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
foreach (ServiceElement el in svcmod.Services.Services)
{
Type svcType = Type.GetType(el.Name + "," + "Services");
//Type svcType = Type.GetType(el.Name + "," + el.Name.Split('.')[0]);
ListViewItem lvItem = new ListViewItem(el.Name);
if (svcType == null)
{
lvItem.SubItems.Add("已停止");
lvItem.SubItems.Add("服务类型无效" + el.Name + "配置文件中。");
}
else
{
ServiceHost aServiceHost = new ServiceHost(svcType);
aServiceHost.Open();
dicHost.Add(el.Name, aServiceHost); //启动的服务保存在字典
lvItem.SubItems.Add("已启动");
lvItem.SubItems.Add("");
}
this.lvList.Items.Add(lvItem);
}
}
else
{
HG.WinAPI.Win32.ShowWindow(this.Handle, HG.WinAPI.Win32.SW_SHOWNORMAL);
SetMid(this);
HG.WinAPI.Win32.SetForegroundWindow(this.Handle);
//HG.WinAPI.Win32.SendMessage(this.Handle, HG.WinAPI.Win32.WM_NCACTIVATE, HG.WinAPI.Win32.WA_ACTIVE, 0);
}
}
#endregion
public void FormShow()
{
Form1 StartService = new Form1();
StartService.ShowDialog(); //不要用Show
}
protected override void OnStart(string[] args)
{
Thread uiThread = new Thread(new ThreadStart(() => FormShow()));
//Thread uiThread = new Thread(new ThreadStart(FormShow));
uiThread.Start();
}
/// <summary>
/// Windows服务类
/// </summary>
/// <summary>
/// 检查服务存在的存在性
/// </summary>
/// <param name=" NameService ">服务名</param>
/// <returns>存在返回 true,否则返回 false;</returns>
public static bool isServiceIsExisted(string NameService)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName.ToLower() == NameService.ToLower())
{
return true;
}
}
return false;
}
/// <summary>
/// 安装Windows服务
/// </summary>
/// <param name="stateSaver">集合</param>
/// <param name="filepath">程序文件路径</param>
public static void InstallmyService(IDictionary stateSaver, string filepath)
{
AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
AssemblyInstaller1.UseNewContext = true;
AssemblyInstaller1.Path = filepath;
AssemblyInstaller1.Install(stateSaver);
AssemblyInstaller1.Commit(stateSaver);
AssemblyInstaller1.Dispose();
}
/// <summary>
/// 卸载Windows服务
/// </summary>
/// <param name="filepath">程序文件路径</param>
public static void UnInstallmyService(string filepath)
{
AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
AssemblyInstaller1.UseNewContext = true;
AssemblyInstaller1.Path = filepath;
AssemblyInstaller1.Uninstall(null);
AssemblyInstaller1.Dispose();
}
/// <summary>
/// 检查Windows服务是否在运行
/// </summary>
/// <param name="name">程序的服务名</param>
public static bool IsRunning(string name)
{
bool IsRun = false;
try
{
if (!isServiceIsExisted(name))
{
return false;
}
ServiceController sc = new ServiceController(name);
if (sc.Status == ServiceControllerStatus.StartPending ||
sc.Status == ServiceControllerStatus.Running)
{
IsRun = true;
}
sc.Close();
}
catch
{
IsRun = false;
}
return IsRun;
}
/// <summary>
/// 启动Windows服务
/// </summary>
/// <param name="name">程序的服务名</param>
/// <returns>启动成功返回 true,否则返回 false;</returns>
public static bool StarmyService(string name)
{
ServiceController sc = new ServiceController(name);
if (sc.Status == ServiceControllerStatus.Stopped || sc.Status == ServiceControllerStatus.StopPending
)
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10));
}
else
{
}
sc.Close();
return true;
}
/// <summary>
/// 停止Windows服务
/// </summary>
/// <param name="name">程序的服务名</param>
/// <returns>停止成功返回 true,否则返回 false;</returns>
public static bool StopmyService(string name)
{
ServiceController sc = new ServiceController(name);
if (sc.Status == ServiceControllerStatus.Running ||
sc.Status == ServiceControllerStatus.StartPending)
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10));
}
else
{
}
sc.Close();
return true;
}
/// <summary>
/// 重启Windows服务
/// </summary>
/// <param name="name">程序的服务名</param>
/// <returns>重启成功返回 true,否则返回 false;</returns>
public static bool RefreshmyService(string name)
{
return StopmyService(name) && StarmyService(name);
}