C# 编写Windows Service(windows服务程序)
- Windows Service简介:
一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序。Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就能开始运行了,它必须有特定的启动方式。这些启动方式包括了自动启动和手动启动两种。对于自动启动的Windows服务程序,它们在Windows启动或是重启之后用户登录之前就开始执行了。只要你将相应的Windows服务程序注册到服务控制管理器(Service Control Manager)中,并将其启动类别设为自动启动就行了。而对于手动启动的Windows服务程序,你可以通过命令行工具的NET START 命令来启动它,或是通过控制面板中管理工具下的服务一项来启动相应的Windows服务程序。
同样,一个Windows服务程序也不能像一般的应用程序那样被终止。因为Windows服务程序一般是没有用户界面的,所以你也要通过命令行工具或是下面图中的工具来停止它,或是在系统关闭时使得Windows服务程序自动停止。因为Windows服务程序没有用户界面,所以基于用户界面的API函数对其是没有多大的意义。为了能使一个Windows服务程序能够正常并有效的在系统环境下工作,程序员必须实现一系列的方法来完成其服务功能。Windows服务程序的应用范围很广,典型的Windows服务程序包含了硬件控制、应用程序监视、系统级应用、诊断、报告、Web和文件系统服务等功能。
和Windows服务程序相关的命名空间涉及到以下两个:System.ServiceProcess 和System.Diagnostics。
- 用C#创建Windows服务的步骤:
1.创建Windows Service项目
从Visual C# 工程中选取 Windows 服务(Windows Service)选项,给工程一个新文件名,然后点击 确定。
2.向服务中函数功能实现
OnStart函数在启动服务时执行,OnStop函数在停止服务时执行。在这里,当启动和停止服务时,向一个文本文件中写入一些文字信息,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace MyService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("WindowsService: Service Started" + DateTime.Now.ToString() + "\n");
sw.Flush();
sw.Close();
fs.Close();
}
//protected override void OnContinue()
//{
// base.OnContinue();
//}
//protected override void OnPause()
//{
// base.OnPause(); // father class method inherit
//}
//protected override void OnShutdown()
//{
// base.OnShutdown();
//}
protected override void OnStop()
{
FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("WindowsService: Service Stopped" + DateTime.Now.ToString() + "\n");
sw.Flush();
sw.Close();
fs.Close();
}
}
}
4.回到设计窗口点右键选择-添加安装程序 -生成serviceInstaller1和 serviceProcessInstaller1两个组件
把serviceInstaller1的属性ServiceName改写为你的服务程序名,并把启动模 式设置为AUTOMATIC
把serviceProcessInstaller1的属性account改写为 LocalSystem
5.编译链接生成服务程序
通过从生成菜单中选择生成来生成项目。
6.安装服务
用.net framework工具INSTALLUTIL安装服务程序即可。
用项目的输出作为参数,从命令行运行 InstallUtil.exe。在命令行中输入下列代码:
installutil yourproject.exe
Hint: a windows service must first be installed using installutil.exe and then started with the serviceExplorer, windows Services Administrative tool or the NET START command.
7.卸载服务
用项目的输出作为参数,从命令行运行 InstallUtil.exe。
installutil /u yourproject.exe
如上服务程序运行结果截图:
- 补充:
1.Service启动属性:
Manual 服务安装后,必须手动启动。
Automatic 每次计算机重新启动时,服务都会自动启动。
Disabled 服务无法启动。
2.新建的Service项目,其中各属性的含义(设计视图->右键属性):
Autolog 是否自动写入系统的日志文件
CanHandlePowerEvent 服务时候接受电源事件
CanPauseAndContinue 服务是否接受暂停或继续运行的请求
CanShutdown 服务是否在运行它的计算机关闭时收到通知,以便能够调用 OnShutDown 过程
CanStop 服务是否接受停止运行的请求
ServiceName 服务名
3. 也可以在系统服务管理器中,设置相应Service的属性或启动方式等
计算机管理 -> 服务和应用程序 -> 服务 -> ...
给WINDOWS服务加上描述
当我们创建一个WINDOWS服务后,却发觉我们所创建的服务没有相关的描述。(你可以打开服务管理器程序查看)。而System.ServiceProcess.ServiceBase这些相关的类都没有提供这方面的信息。同样如果我们需要给我们的服务加上恰当的描述,我们也只能通过非托管代码来处理。
using System;
using System.Runtime.InteropServices; namespace FileWatchService
{
public class modAPI
{
[DllImport("advapi32.dll")]
public static extern int LockServiceDatabase(int hSCManager);
[DllImport("advapi32.dll")]
public static extern bool UnlockServiceDatabase(int hSCManager);
[DllImport("kernel32.dll")]
public static extern void CopyMemory(IntPtr pDst, SC_ACTION[] pSrc,int ByteLen);
[DllImport("advapi32.dll")]
public static extern bool ChangeServiceConfigA(
int hService, ServiceType dwServiceType, int dwStartType,
int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup,
int lpdwTagId, string lpDependencies, string lpServiceStartName,
string lpPassword, string lpDisplayName);
[DllImport("advapi32.dll")]
public static extern bool ChangeServiceConfig2A(
int hService, InfoLevel dwInfoLevel,
[MarshalAs(UnmanagedType.Struct)] ref SERVICE_DESCRIPTION lpInfo); [DllImport("advapi32.dll")]
public static extern bool ChangeServiceConfig2A(
int hService, InfoLevel dwInfoLevel,
[MarshalAs(UnmanagedType.Struct)] ref SERVICE_FAILURE_ACTIONS lpInfo); [DllImport("advapi32.dll")]
public static extern int OpenServiceA(
int hSCManager, string lpServiceName, ACCESS_TYPE dwDesiredAccess); [DllImport("advapi32.dll")]
public static extern int OpenSCManagerA(
string lpMachineName, string lpDatabaseName, ServiceControlManagerType dwDesiredAccess); [DllImport("advapi32.dll")]
public static extern bool CloseServiceHandle(
int hSCObject); [DllImport("advapi32.dll")]
public static extern bool QueryServiceConfigA(
int hService, [MarshalAs(UnmanagedType.Struct)] ref QUERY_SERVICE_CONFIG lpServiceConfig, int cbBufSize,
int pcbBytesNeeded);
[DllImport("advapi32.dll")]
public static extern int StartService(int SVHANDLE,int dwNumServiceArgs,string lpServiceArgVectors); public const int STANDARD_RIGHTS_REQUIRED = 0xF0000;
public const int GENERIC_READ = -2147483648;
public const int ERROR_INSUFFICIENT_BUFFER = 122;
public const int SERVICE_NO_CHANGE = -1;
//public const int SERVICE_NO_CHANGE = 0xFFFF; public enum ServiceType
{
SERVICE_KERNEL_DRIVER = 0x1,
SERVICE_FILE_SYSTEM_DRIVER = 0x2,
SERVICE_WIN32_OWN_PROCESS = 0x10,
SERVICE_WIN32_SHARE_PROCESS = 0x20,
SERVICE_INTERACTIVE_PROCESS = 0x100,
SERVICETYPE_NO_CHANGE = SERVICE_NO_CHANGE
} public enum ServiceStartType:int
{
SERVICE_BOOT_START = 0x0,
SERVICE_SYSTEM_START = 0x1,
SERVICE_AUTO_START = 0x2,
SERVICE_DEMAND_START = 0x3,
SERVICE_DISABLED = 0x4,
SERVICESTARTTYPE_NO_CHANGE = SERVICE_NO_CHANGE
} public enum ServiceErrorControl:int
{
SERVICE_ERROR_IGNORE = 0x0,
SERVICE_ERROR_NORMAL = 0x1,
SERVICE_ERROR_SEVERE = 0x2,
SERVICE_ERROR_CRITICAL = 0x3,
msidbServiceInstallErrorControlVital = 0x8000,
SERVICEERRORCONTROL_NO_CHANGE = SERVICE_NO_CHANGE
} public enum ServiceStateRequest:int
{
SERVICE_ACTIVE = 0x1,
SERVICE_INACTIVE = 0x2,
SERVICE_STATE_ALL = (SERVICE_ACTIVE + SERVICE_INACTIVE)
} public enum ServiceControlType:int
{
SERVICE_CONTROL_STOP = 0x1,
SERVICE_CONTROL_PAUSE = 0x2,
SERVICE_CONTROL_CONTINUE = 0x3,
SERVICE_CONTROL_INTERROGATE = 0x4,
SERVICE_CONTROL_SHUTDOWN = 0x5,
SERVICE_CONTROL_PARAMCHANGE = 0x6,
SERVICE_CONTROL_NETBINDADD = 0x7,
SERVICE_CONTROL_NETBINDREMOVE = 0x8,
SERVICE_CONTROL_NETBINDENABLE = 0x9,
SERVICE_CONTROL_NETBINDDISABLE = 0xA,
SERVICE_CONTROL_DEVICEEVENT = 0xB,
SERVICE_CONTROL_HARDWAREPROFILECHANGE = 0xC,
SERVICE_CONTROL_POWEREVENT = 0xD,
SERVICE_CONTROL_SESSIONCHANGE = 0xE,
} public enum ServiceState:int
{
SERVICE_STOPPED = 0x1,
SERVICE_START_PENDING = 0x2,
SERVICE_STOP_PENDING = 0x3,
SERVICE_RUNNING = 0x4,
SERVICE_CONTINUE_PENDING = 0x5,
SERVICE_PAUSE_PENDING = 0x6,
SERVICE_PAUSED = 0x7,
} public enum ServiceControlAccepted:int
{
SERVICE_ACCEPT_STOP = 0x1,
SERVICE_ACCEPT_PAUSE_CONTINUE = 0x2,
SERVICE_ACCEPT_SHUTDOWN = 0x4,
SERVICE_ACCEPT_PARAMCHANGE = 0x8,
SERVICE_ACCEPT_NETBINDCHANGE = 0x10,
SERVICE_ACCEPT_HARDWAREPROFILECHANGE = 0x20,
SERVICE_ACCEPT_POWEREVENT = 0x40,
SERVICE_ACCEPT_SESSIONCHANGE = 0x80
} public enum ServiceControlManagerType:int
{
SC_MANAGER_CONNECT = 0x1,
SC_MANAGER_CREATE_SERVICE = 0x2,
SC_MANAGER_ENUMERATE_SERVICE = 0x4,
SC_MANAGER_LOCK = 0x8,
SC_MANAGER_QUERY_LOCK_STATUS = 0x10,
SC_MANAGER_MODIFY_BOOT_CONFIG = 0x20,
SC_MANAGER_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED + SC_MANAGER_CONNECT + SC_MANAGER_CREATE_SERVICE + SC_MANAGER_ENUMERATE_SERVICE + SC_MANAGER_LOCK + SC_MANAGER_QUERY_LOCK_STATUS + SC_MANAGER_MODIFY_BOOT_CONFIG
} public enum ACCESS_TYPE:int
{
SERVICE_QUERY_CONFIG = 0x1,
SERVICE_CHANGE_CONFIG = 0x2,
SERVICE_QUERY_STATUS = 0x4,
SERVICE_ENUMERATE_DEPENDENTS = 0x8,
SERVICE_START = 0x10,
SERVICE_STOP = 0x20,
SERVICE_PAUSE_CONTINUE = 0x40,
SERVICE_INTERROGATE = 0x80,
SERVICE_USER_DEFINED_CONTROL = 0x100,
SERVICE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED + SERVICE_QUERY_CONFIG + SERVICE_CHANGE_CONFIG + SERVICE_QUERY_STATUS + SERVICE_ENUMERATE_DEPENDENTS + SERVICE_START + SERVICE_STOP + SERVICE_PAUSE_CONTINUE + SERVICE_INTERROGATE + SERVICE_USER_DEFINED_CONTROL
} [StructLayout(LayoutKind.Sequential)]
public struct SERVICE_STATUS
{
public int dwServiceType;
public int dwCurrentState;
public int dwControlsAccepted;
public int dwWin32ExitCode;
public int dwServiceSpecificExitCode;
public int dwCheckPoint;
public int dwWaitHint;
}
[StructLayout(LayoutKind.Sequential)]
public struct QUERY_SERVICE_CONFIG
{
public int dwServiceType;
public int dwStartType;
public int dwErrorControl;
public string lpBinaryPathName;
public string lpLoadOrderGroup;
public int dwTagId;
public string lpDependencies;
public string lpServiceStartName;
public string lpDisplayName;
} public enum SC_ACTION_TYPE:int
{
SC_ACTION_NONE = 0,
SC_ACTION_RESTART = 1,
SC_ACTION_REBOOT = 2,
SC_ACTION_RUN_COMMAND = 3,
} [StructLayout(LayoutKind.Sequential)]
public struct SC_ACTION
{
public SC_ACTION_TYPE SCActionType;
public int Delay;
} public enum InfoLevel:int
{
SERVICE_CONFIG_DESCRIPTION = 1,
SERVICE_CONFIG_FAILURE_ACTIONS = 2
} [StructLayout(LayoutKind.Sequential)]
public struct SERVICE_DESCRIPTION
{
public string lpDescription;
} [StructLayout(LayoutKind.Sequential)]
public struct SERVICE_FAILURE_ACTIONS
{
public int dwResetPeriod;
public string lpRebootMsg;
public string lpCommand;
public int cActions;
public int lpsaActions;
}
}
}
当我们给服务增加安装包时,我们可以在ProjectInstaller里加上我们修改服务描述的代码
private void InitializeComponent()
{//这里要增加代码
this.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.ProjectInstaller_AfterInstall);}
private void ProjectInstaller_AfterInstall(object sender,System.Configuration.Install.InstallEventArgs e)
{ int iSCManagerHandle = 0;
int iSCManagerLockHandle = 0;
int iServiceHandle = 0;
bool bChangeServiceConfig = false;
bool bChangeServiceConfig2 = false;
modAPI.SERVICE_DESCRIPTION ServiceDescription;
modAPI.SERVICE_FAILURE_ACTIONS ServiceFailureActions;
modAPI.SC_ACTION[] ScActions = new modAPI.SC_ACTION[3]; bool bCloseService = false;
bool bUnlockSCManager = false;
bool bCloseSCManager = false; IntPtr iScActionsPointer = new IntPtr();
try
{
//打开服务控制台
iSCManagerHandle = modAPI.OpenSCManagerA(null, null,
modAPI.ServiceControlManagerType.SC_MANAGER_ALL_ACCESS);
if (iSCManagerHandle < 1)
{
throw new Exception("不能打开服务管理器.");
}
iSCManagerLockHandle = modAPI.LockServiceDatabase(iSCManagerHandle);
if (iSCManagerLockHandle < 1)
{
throw new Exception("不能锁定服务管理器.");
} //服务名
iServiceHandle = modAPI.OpenServiceA(iSCManagerHandle, "JadeWatchService",
modAPI.ACCESS_TYPE.SERVICE_ALL_ACCESS);
if (iServiceHandle < 1)
{
throw new Exception("不能打开服务进行修改.");
}
bChangeServiceConfig = modAPI.ChangeServiceConfigA(iServiceHandle,
modAPI.ServiceType.SERVICE_WIN32_OWN_PROCESS | modAPI.ServiceType.SERVICE_INTERACTIVE_PROCESS,
modAPI.SERVICE_NO_CHANGE, modAPI.SERVICE_NO_CHANGE, null, null,
0, null, null, null, null);
if (bChangeServiceConfig==false)
{
throw new Exception("不能改变服务设置.");
}
ServiceDescription.lpDescription = "青鸟文件监控服务,如果停止该服务,数据将不能正常进行备份!";
bChangeServiceConfig2 = modAPI.ChangeServiceConfig2A(iServiceHandle,
modAPI.InfoLevel.SERVICE_CONFIG_DESCRIPTION,ref ServiceDescription);
if (bChangeServiceConfig2==false)
{
throw new Exception("不能进行服务描述更改.");
}
ServiceFailureActions.dwResetPeriod = 600;
ServiceFailureActions.lpRebootMsg = "服务启动失败! 重启中...";
// ServiceFailureActions.lpCommand = "SomeCommand.exe Param1 Param2";
ServiceFailureActions.lpCommand = "";
ServiceFailureActions.cActions = ScActions.Length; //故障恢复设置,这里没有设置
ScActions[0].Delay = 20000;
ScActions[0].SCActionType = modAPI.SC_ACTION_TYPE.SC_ACTION_NONE; //不要对失败操作做任何处理,如果重启服务等
ScActions[1].Delay = 20000;
ScActions[1].SCActionType = modAPI.SC_ACTION_TYPE.SC_ACTION_NONE;
ScActions[2].Delay = 20000;
ScActions[2].SCActionType = modAPI.SC_ACTION_TYPE.SC_ACTION_NONE;
iScActionsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(new modAPI.SC_ACTION()) * 3);
modAPI.CopyMemory(iScActionsPointer, ScActions, Marshal.SizeOf(new modAPI.SC_ACTION()) * 3);
ServiceFailureActions.lpsaActions = iScActionsPointer.ToInt32();
bChangeServiceConfig2 = modAPI.ChangeServiceConfig2A(iServiceHandle,
modAPI.InfoLevel.SERVICE_CONFIG_FAILURE_ACTIONS,ref ServiceFailureActions);
if (bChangeServiceConfig2==false)
{
throw new Exception("不能设置服务的故障恢复设置.");
}
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
Marshal.FreeHGlobal(iScActionsPointer); if (iServiceHandle > 0)
{
bCloseService = modAPI.CloseServiceHandle(iServiceHandle);
} if (iSCManagerLockHandle > 0)
{
bUnlockSCManager = modAPI.UnlockServiceDatabase(iSCManagerLockHandle);
} if (iSCManagerHandle != 0)
{
bCloseSCManager = modAPI.CloseServiceHandle(iSCManagerHandle);
}
}
}
在安装完成后,我们对服务进行。这里可以修改的内容包括服务的描述,服务的故障处理等.
如果你在安装时,需要对服务进行自动处于运行状态,或卸载时需要自动将服务也卸载,你只要注册
this.BeforeUninstall+=new InstallEventHandler(ProjectInstaller_BeforeUninstall);
this.Committed+=new InstallEventHandler(ProjectInstaller_Committed);
这二个事件
Committed事件,在这里可以将安装的服务进行调整到运行状态
BeforeUninstall事件,您可以在这里将服务自动卸载掉
更多资料请参考:
当我们创建一个WINDOWS服务后,却发觉我们所创建的服务没有相关的描述。(你可以打开服务管理器程序查看)。而System.ServiceProcess.ServiceBase这些相关的类都没有提供这方面的信息。同样如果我们需要给我们的服务加上恰当的描述,我们也只能通过非托管代码来处理。
using System;
using System.Runtime.InteropServices; namespace FileWatchService
{
public class modAPI
{
[DllImport("advapi32.dll")]
public static extern int LockServiceDatabase(int hSCManager);
[DllImport("advapi32.dll")]
public static extern bool UnlockServiceDatabase(int hSCManager);
[DllImport("kernel32.dll")]
public static extern void CopyMemory(IntPtr pDst, SC_ACTION[] pSrc,int ByteLen);
[DllImport("advapi32.dll")]
public static extern bool ChangeServiceConfigA(
int hService, ServiceType dwServiceType, int dwStartType,
int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup,
int lpdwTagId, string lpDependencies, string lpServiceStartName,
string lpPassword, string lpDisplayName);
[DllImport("advapi32.dll")]
public static extern bool ChangeServiceConfig2A(
int hService, InfoLevel dwInfoLevel,
[MarshalAs(UnmanagedType.Struct)] ref SERVICE_DESCRIPTION lpInfo); [DllImport("advapi32.dll")]
public static extern bool ChangeServiceConfig2A(
int hService, InfoLevel dwInfoLevel,
[MarshalAs(UnmanagedType.Struct)] ref SERVICE_FAILURE_ACTIONS lpInfo); [DllImport("advapi32.dll")]
public static extern int OpenServiceA(
int hSCManager, string lpServiceName, ACCESS_TYPE dwDesiredAccess); [DllImport("advapi32.dll")]
public static extern int OpenSCManagerA(
string lpMachineName, string lpDatabaseName, ServiceControlManagerType dwDesiredAccess); [DllImport("advapi32.dll")]
public static extern bool CloseServiceHandle(
int hSCObject); [DllImport("advapi32.dll")]
public static extern bool QueryServiceConfigA(
int hService, [MarshalAs(UnmanagedType.Struct)] ref QUERY_SERVICE_CONFIG lpServiceConfig, int cbBufSize,
int pcbBytesNeeded);
[DllImport("advapi32.dll")]
public static extern int StartService(int SVHANDLE,int dwNumServiceArgs,string lpServiceArgVectors); public const int STANDARD_RIGHTS_REQUIRED = 0xF0000;
public const int GENERIC_READ = -2147483648;
public const int ERROR_INSUFFICIENT_BUFFER = 122;
public const int SERVICE_NO_CHANGE = -1;
//public const int SERVICE_NO_CHANGE = 0xFFFF; public enum ServiceType
{
SERVICE_KERNEL_DRIVER = 0x1,
SERVICE_FILE_SYSTEM_DRIVER = 0x2,
SERVICE_WIN32_OWN_PROCESS = 0x10,
SERVICE_WIN32_SHARE_PROCESS = 0x20,
SERVICE_INTERACTIVE_PROCESS = 0x100,
SERVICETYPE_NO_CHANGE = SERVICE_NO_CHANGE
} public enum ServiceStartType:int
{
SERVICE_BOOT_START = 0x0,
SERVICE_SYSTEM_START = 0x1,
SERVICE_AUTO_START = 0x2,
SERVICE_DEMAND_START = 0x3,
SERVICE_DISABLED = 0x4,
SERVICESTARTTYPE_NO_CHANGE = SERVICE_NO_CHANGE
} public enum ServiceErrorControl:int
{
SERVICE_ERROR_IGNORE = 0x0,
SERVICE_ERROR_NORMAL = 0x1,
SERVICE_ERROR_SEVERE = 0x2,
SERVICE_ERROR_CRITICAL = 0x3,
msidbServiceInstallErrorControlVital = 0x8000,
SERVICEERRORCONTROL_NO_CHANGE = SERVICE_NO_CHANGE
} public enum ServiceStateRequest:int
{
SERVICE_ACTIVE = 0x1,
SERVICE_INACTIVE = 0x2,
SERVICE_STATE_ALL = (SERVICE_ACTIVE + SERVICE_INACTIVE)
} public enum ServiceControlType:int
{
SERVICE_CONTROL_STOP = 0x1,
SERVICE_CONTROL_PAUSE = 0x2,
SERVICE_CONTROL_CONTINUE = 0x3,
SERVICE_CONTROL_INTERROGATE = 0x4,
SERVICE_CONTROL_SHUTDOWN = 0x5,
SERVICE_CONTROL_PARAMCHANGE = 0x6,
SERVICE_CONTROL_NETBINDADD = 0x7,
SERVICE_CONTROL_NETBINDREMOVE = 0x8,
SERVICE_CONTROL_NETBINDENABLE = 0x9,
SERVICE_CONTROL_NETBINDDISABLE = 0xA,
SERVICE_CONTROL_DEVICEEVENT = 0xB,
SERVICE_CONTROL_HARDWAREPROFILECHANGE = 0xC,
SERVICE_CONTROL_POWEREVENT = 0xD,
SERVICE_CONTROL_SESSIONCHANGE = 0xE,
} public enum ServiceState:int
{
SERVICE_STOPPED = 0x1,
SERVICE_START_PENDING = 0x2,
SERVICE_STOP_PENDING = 0x3,
SERVICE_RUNNING = 0x4,
SERVICE_CONTINUE_PENDING = 0x5,
SERVICE_PAUSE_PENDING = 0x6,
SERVICE_PAUSED = 0x7,
} public enum ServiceControlAccepted:int
{
SERVICE_ACCEPT_STOP = 0x1,
SERVICE_ACCEPT_PAUSE_CONTINUE = 0x2,
SERVICE_ACCEPT_SHUTDOWN = 0x4,
SERVICE_ACCEPT_PARAMCHANGE = 0x8,
SERVICE_ACCEPT_NETBINDCHANGE = 0x10,
SERVICE_ACCEPT_HARDWAREPROFILECHANGE = 0x20,
SERVICE_ACCEPT_POWEREVENT = 0x40,
SERVICE_ACCEPT_SESSIONCHANGE = 0x80
} public enum ServiceControlManagerType:int
{
SC_MANAGER_CONNECT = 0x1,
SC_MANAGER_CREATE_SERVICE = 0x2,
SC_MANAGER_ENUMERATE_SERVICE = 0x4,
SC_MANAGER_LOCK = 0x8,
SC_MANAGER_QUERY_LOCK_STATUS = 0x10,
SC_MANAGER_MODIFY_BOOT_CONFIG = 0x20,
SC_MANAGER_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED + SC_MANAGER_CONNECT + SC_MANAGER_CREATE_SERVICE + SC_MANAGER_ENUMERATE_SERVICE + SC_MANAGER_LOCK + SC_MANAGER_QUERY_LOCK_STATUS + SC_MANAGER_MODIFY_BOOT_CONFIG
} public enum ACCESS_TYPE:int
{
SERVICE_QUERY_CONFIG = 0x1,
SERVICE_CHANGE_CONFIG = 0x2,
SERVICE_QUERY_STATUS = 0x4,
SERVICE_ENUMERATE_DEPENDENTS = 0x8,
SERVICE_START = 0x10,
SERVICE_STOP = 0x20,
SERVICE_PAUSE_CONTINUE = 0x40,
SERVICE_INTERROGATE = 0x80,
SERVICE_USER_DEFINED_CONTROL = 0x100,
SERVICE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED + SERVICE_QUERY_CONFIG + SERVICE_CHANGE_CONFIG + SERVICE_QUERY_STATUS + SERVICE_ENUMERATE_DEPENDENTS + SERVICE_START + SERVICE_STOP + SERVICE_PAUSE_CONTINUE + SERVICE_INTERROGATE + SERVICE_USER_DEFINED_CONTROL
} [StructLayout(LayoutKind.Sequential)]
public struct SERVICE_STATUS
{
public int dwServiceType;
public int dwCurrentState;
public int dwControlsAccepted;
public int dwWin32ExitCode;
public int dwServiceSpecificExitCode;
public int dwCheckPoint;
public int dwWaitHint;
}
[StructLayout(LayoutKind.Sequential)]
public struct QUERY_SERVICE_CONFIG
{
public int dwServiceType;
public int dwStartType;
public int dwErrorControl;
public string lpBinaryPathName;
public string lpLoadOrderGroup;
public int dwTagId;
public string lpDependencies;
public string lpServiceStartName;
public string lpDisplayName;
} public enum SC_ACTION_TYPE:int
{
SC_ACTION_NONE = 0,
SC_ACTION_RESTART = 1,
SC_ACTION_REBOOT = 2,
SC_ACTION_RUN_COMMAND = 3,
} [StructLayout(LayoutKind.Sequential)]
public struct SC_ACTION
{
public SC_ACTION_TYPE SCActionType;
public int Delay;
} public enum InfoLevel:int
{
SERVICE_CONFIG_DESCRIPTION = 1,
SERVICE_CONFIG_FAILURE_ACTIONS = 2
} [StructLayout(LayoutKind.Sequential)]
public struct SERVICE_DESCRIPTION
{
public string lpDescription;
} [StructLayout(LayoutKind.Sequential)]
public struct SERVICE_FAILURE_ACTIONS
{
public int dwResetPeriod;
public string lpRebootMsg;
public string lpCommand;
public int cActions;
public int lpsaActions;
}
}
}
当我们给服务增加安装包时,我们可以在ProjectInstaller里加上我们修改服务描述的代码
private void InitializeComponent()
{//这里要增加代码
this.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.ProjectInstaller_AfterInstall);}
private void ProjectInstaller_AfterInstall(object sender,System.Configuration.Install.InstallEventArgs e)
{ int iSCManagerHandle = 0;
int iSCManagerLockHandle = 0;
int iServiceHandle = 0;
bool bChangeServiceConfig = false;
bool bChangeServiceConfig2 = false;
modAPI.SERVICE_DESCRIPTION ServiceDescription;
modAPI.SERVICE_FAILURE_ACTIONS ServiceFailureActions;
modAPI.SC_ACTION[] ScActions = new modAPI.SC_ACTION[3]; bool bCloseService = false;
bool bUnlockSCManager = false;
bool bCloseSCManager = false; IntPtr iScActionsPointer = new IntPtr();
try
{
//打开服务控制台
iSCManagerHandle = modAPI.OpenSCManagerA(null, null,
modAPI.ServiceControlManagerType.SC_MANAGER_ALL_ACCESS);
if (iSCManagerHandle < 1)
{
throw new Exception("不能打开服务管理器.");
}
iSCManagerLockHandle = modAPI.LockServiceDatabase(iSCManagerHandle);
if (iSCManagerLockHandle < 1)
{
throw new Exception("不能锁定服务管理器.");
} //服务名
iServiceHandle = modAPI.OpenServiceA(iSCManagerHandle, "JadeWatchService",
modAPI.ACCESS_TYPE.SERVICE_ALL_ACCESS);
if (iServiceHandle < 1)
{
throw new Exception("不能打开服务进行修改.");
}
bChangeServiceConfig = modAPI.ChangeServiceConfigA(iServiceHandle,
modAPI.ServiceType.SERVICE_WIN32_OWN_PROCESS | modAPI.ServiceType.SERVICE_INTERACTIVE_PROCESS,
modAPI.SERVICE_NO_CHANGE, modAPI.SERVICE_NO_CHANGE, null, null,
0, null, null, null, null);
if (bChangeServiceConfig==false)
{
throw new Exception("不能改变服务设置.");
}
ServiceDescription.lpDescription = "青鸟文件监控服务,如果停止该服务,数据将不能正常进行备份!";
bChangeServiceConfig2 = modAPI.ChangeServiceConfig2A(iServiceHandle,
modAPI.InfoLevel.SERVICE_CONFIG_DESCRIPTION,ref ServiceDescription);
if (bChangeServiceConfig2==false)
{
throw new Exception("不能进行服务描述更改.");
}
ServiceFailureActions.dwResetPeriod = 600;
ServiceFailureActions.lpRebootMsg = "服务启动失败! 重启中...";
// ServiceFailureActions.lpCommand = "SomeCommand.exe Param1 Param2";
ServiceFailureActions.lpCommand = "";
ServiceFailureActions.cActions = ScActions.Length; //故障恢复设置,这里没有设置
ScActions[0].Delay = 20000;
ScActions[0].SCActionType = modAPI.SC_ACTION_TYPE.SC_ACTION_NONE; //不要对失败操作做任何处理,如果重启服务等
ScActions[1].Delay = 20000;
ScActions[1].SCActionType = modAPI.SC_ACTION_TYPE.SC_ACTION_NONE;
ScActions[2].Delay = 20000;
ScActions[2].SCActionType = modAPI.SC_ACTION_TYPE.SC_ACTION_NONE;
iScActionsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(new modAPI.SC_ACTION()) * 3);
modAPI.CopyMemory(iScActionsPointer, ScActions, Marshal.SizeOf(new modAPI.SC_ACTION()) * 3);
ServiceFailureActions.lpsaActions = iScActionsPointer.ToInt32();
bChangeServiceConfig2 = modAPI.ChangeServiceConfig2A(iServiceHandle,
modAPI.InfoLevel.SERVICE_CONFIG_FAILURE_ACTIONS,ref ServiceFailureActions);
if (bChangeServiceConfig2==false)
{
throw new Exception("不能设置服务的故障恢复设置.");
}
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
Marshal.FreeHGlobal(iScActionsPointer); if (iServiceHandle > 0)
{
bCloseService = modAPI.CloseServiceHandle(iServiceHandle);
} if (iSCManagerLockHandle > 0)
{
bUnlockSCManager = modAPI.UnlockServiceDatabase(iSCManagerLockHandle);
} if (iSCManagerHandle != 0)
{
bCloseSCManager = modAPI.CloseServiceHandle(iSCManagerHandle);
}
}
}
在安装完成后,我们对服务进行。这里可以修改的内容包括服务的描述,服务的故障处理等.
如果你在安装时,需要对服务进行自动处于运行状态,或卸载时需要自动将服务也卸载,你只要注册
this.BeforeUninstall+=new InstallEventHandler(ProjectInstaller_BeforeUninstall);
this.Committed+=new InstallEventHandler(ProjectInstaller_Committed);
这二个事件
Committed事件,在这里可以将安装的服务进行调整到运行状态
BeforeUninstall事件,您可以在这里将服务自动卸载掉