當我們建立一個WINDOWS服務後,卻發覺我們所建立的服務沒有相關的描述。(你可以開啟服務管理員程式查看)。而System.ServiceProcess.ServiceBase這些相關的類都沒有提供這方面的資訊。同樣如果我們需要給我們的服務加上恰當的描述,我們也只能通過Unmanaged 程式碼來處理。
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事件,您可以在這裡將服務自動卸載掉