知识大全 给WINDOWS服务加上描述

Posted

篇首语:业无高卑志当坚,男儿有求安得闲?本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 给WINDOWS服务加上描述相关的知识,希望对你有一定的参考价值。

高手进阶:给WINDOWS服务加上描述  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

    当我们创建一个WINDOWS服务后 却发觉我们所创建的服务没有相关的描述 (你可以打开服务管理器程序查看) 而System ServiceProcess ServiceBase这些相关的类都没有提供这方面的信息 同样如果我们需要给我们的服务加上恰当的描述 我们也只能通过非托管代码来处理

  

  using System;using System Runtime InteropServices;

  namespace FileWatchService public class modAPI   [DllImport( advapi dll )]  public static extern int LockServiceDatabase(int hSCManager);   [DllImport( advapi dll )]  public static extern bool UnlockServiceDatabase(int hSCManager);   [DllImport( kernel dll )]  public static extern void CopyMemory(IntPtr pDst SC_ACTION[] pSrc int ByteLen);   [DllImport( advapi 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( advapi dll )]  public static extern bool ChangeServiceConfig A(   int hService InfoLevel dwInfoLevel    [MarshalAs(UnmanagedType Struct)] ref SERVICE_DESCRIPTION lpInfo);

  [DllImport( advapi dll )]  public static extern bool ChangeServiceConfig A(   int hService InfoLevel dwInfoLevel    [MarshalAs(UnmanagedType Struct)] ref SERVICE_FAILURE_ACTIONS lpInfo);

  [DllImport( advapi dll )]  public static extern int OpenServiceA(   int hSCManager string lpServiceName ACCESS_TYPE dwDesiredAccess);

  [DllImport( advapi dll )]  public static extern int OpenSCManagerA(   string lpMachineName string lpDatabaseName ServiceControlManagerType dwDesiredAccess);

  [DllImport( advapi dll )]  public static extern bool CloseServiceHandle(   int hSCObject);

  [DllImport( advapi dll )]  public static extern bool QueryServiceConfigA(   int hService [MarshalAs(UnmanagedType Struct)] ref QUERY_SERVICE_CONFIG lpServiceConfig int cbBufSize    int pcbBytesNeeded);  [DllImport( advapi dll )]  public static extern int StartService(int SVHANDLE int dwNumServiceArgs string lpServiceArgVectors);

  public const int STANDARD_RIGHTS_REQUIRED = xF ;  public const int GENERIC_READ = ;  public const int ERROR_INSUFFICIENT_BUFFER = ;  public const int SERVICE_NO_CHANGE = ;  //public const int SERVICE_NO_CHANGE = xFFFF;

  public enum ServiceType     SERVICE_KERNEL_DRIVER = x    SERVICE_FILE_SYSTEM_DRIVER = x    SERVICE_WIN _OWN_PROCESS = x    SERVICE_WIN _SHARE_PROCESS = x    SERVICE_INTERACTIVE_PROCESS = x    SERVICETYPE_NO_CHANGE = SERVICE_NO_CHANGE 

  public enum ServiceStartType:int     SERVICE_BOOT_START = x    SERVICE_SYSTEM_START = x    SERVICE_AUTO_START = x    SERVICE_DEMAND_START = x    SERVICE_DISABLED = x    SERVICESTARTTYPE_NO_CHANGE = SERVICE_NO_CHANGE 

  public enum ServiceErrorControl:int     SERVICE_ERROR_IGNORE = x    SERVICE_ERROR_NORMAL = x    SERVICE_ERROR_SEVERE = x    SERVICE_ERROR_CRITICAL = x    msidbServiceInstallErrorControlVital = x    SERVICEERRORCONTROL_NO_CHANGE = SERVICE_NO_CHANGE 

  public enum ServiceStateRequest:int     SERVICE_ACTIVE = x    SERVICE_INACTIVE = x    SERVICE_STATE_ALL = (SERVICE_ACTIVE + SERVICE_INACTIVE) 

  public enum ServiceControlType:int     SERVICE_CONTROL_STOP = x    SERVICE_CONTROL_PAUSE = x    SERVICE_CONTROL_CONTINUE = x    SERVICE_CONTROL_INTERROGATE = x    SERVICE_CONTROL_SHUTDOWN = x    SERVICE_CONTROL_PARAMCHANGE = x    SERVICE_CONTROL_NETBINDADD = x    SERVICE_CONTROL_NETBINDREMOVE = x    SERVICE_CONTROL_NETBINDENABLE = x    SERVICE_CONTROL_NETBINDDISABLE = xA    SERVICE_CONTROL_DEVICEEVENT = xB    SERVICE_CONTROL_HARDWAREPROFILECHANGE = xC    SERVICE_CONTROL_POWEREVENT = xD    SERVICE_CONTROL_SESSIONCHANGE = xE  

  public enum ServiceState:int     SERVICE_STOPPED = x    SERVICE_START_PENDING = x    SERVICE_STOP_PENDING = x    SERVICE_RUNNING = x    SERVICE_CONTINUE_PENDING = x    SERVICE_PAUSE_PENDING = x    SERVICE_PAUSED = x  

  public enum ServiceControlAccepted:int     SERVICE_ACCEPT_STOP = x    SERVICE_ACCEPT_PAUSE_CONTINUE = x    SERVICE_ACCEPT_SHUTDOWN = x    SERVICE_ACCEPT_PARAMCHANGE = x    SERVICE_ACCEPT_NETBINDCHANGE = x    SERVICE_ACCEPT_HARDWAREPROFILECHANGE = x    SERVICE_ACCEPT_POWEREVENT = x    SERVICE_ACCEPT_SESSIONCHANGE = x  

  public enum ServiceControlManagerType:int     SC_MANAGER_CONNECT = x    SC_MANAGER_CREATE_SERVICE = x    SC_MANAGER_ENUMERATE_SERVICE = x    SC_MANAGER_LOCK = x    SC_MANAGER_QUERY_LOCK_STATUS = x    SC_MANAGER_MODIFY_BOOT_CONFIG = x    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 = x    SERVICE_CHANGE_CONFIG = x    SERVICE_QUERY_STATUS = x    SERVICE_ENUMERATE_DEPENDENTS = x    SERVICE_START = x    SERVICE_STOP = x    SERVICE_PAUSE_CONTINUE = x    SERVICE_INTERROGATE = x    SERVICE_USER_DEFINED_CONTROL = x    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 dwWin ExitCode;   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 =    SC_ACTION_RESTART =    SC_ACTION_REBOOT =    SC_ACTION_RUN_MAND =  

  [StructLayout(LayoutKind Sequential)]   public struct SC_ACTION     public SC_ACTION_TYPE SCActionType;   public int Delay; 

  public enum InfoLevel:int     SERVICE_CONFIG_DESCRIPTION =    SERVICE_CONFIG_FAILURE_ACTIONS =  

  [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 = ;   int iSCManagerLockHandle = ;   int iServiceHandle = ;   bool bChangeServiceConfig = false;   bool bChangeServiceConfig = false;   modAPI SERVICE_DESCRIPTION ServiceDescription;   modAPI SERVICE_FAILURE_ACTIONS ServiceFailureActions;   modAPI SC_ACTION[] ScActions = new modAPI SC_ACTION[ ];

  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 < )         throw new Exception( 不能打开服务管理器 );   

    iSCManagerLockHandle = modAPI LockServiceDatabase(iSCManagerHandle);

    if (iSCManagerLockHandle < )         throw new Exception( 不能锁定服务管理器 );   

  //服务名    iServiceHandle = modAPI OpenServiceA(iSCManagerHandle JadeWatchService      modAPI ACCESS_TYPE SERVICE_ALL_ACCESS);

    if (iServiceHandle < )         throw new Exception( 不能打开服务进行修改 );            bChangeServiceConfig = modAPI ChangeServiceConfigA(iServiceHandle      modAPI ServiceType SERVICE_WIN _OWN_PROCESS | modAPI ServiceType SERVICE_INTERACTIVE_PROCESS      modAPI SERVICE_NO_CHANGE modAPI SERVICE_NO_CHANGE null null      null null null null);

    if (bChangeServiceConfig==false)         throw new Exception( 不能改变服务设置 );   

    ServiceDescription lpDescription = 青鸟文件监控服务 如果停止该服务 数据将不能正常进行备份! ;

  bChangeServiceConfig = modAPI ChangeServiceConfig A(iServiceHandle      modAPI InfoLevel SERVICE_CONFIG_DESCRIPTION ref ServiceDescription);

    if (bChangeServiceConfig ==false)         throw new Exception( 不能进行服务描述更改 );     

    ServiceFailureActions dwResetPeriod = ;    ServiceFailureActions lpRebootMsg = 服务启动失败! 重启中 ;    // ServiceFailureActions lpCommand = SomeCommand exe Param Param ;    ServiceFailureActions lpCommand = ;    ServiceFailureActions cActions = ScActions Length;

  //故障恢复设置 这里没有设置    ScActions[ ] Delay = ;    ScActions[ ] SCActionType = modAPI SC_ACTION_TYPE SC_ACTION_NONE; //不要对失败操作做任何处理 如果重启服务等    ScActions[ ] Delay = ;    ScActions[ ] SCActionType = modAPI SC_ACTION_TYPE SC_ACTION_NONE;    ScActions[ ] Delay = ;    ScActions[ ] SCActionType = modAPI SC_ACTION_TYPE SC_ACTION_NONE;

    iScActionsPointer = Marshal AllocHGlobal(Marshal SizeOf(new modAPI SC_ACTION()) * );

    modAPI CopyMemory(iScActionsPointer ScActions Marshal SizeOf(new modAPI SC_ACTION()) * );

  

  ServiceFailureActions lpsaActions = iScActionsPointer ToInt ();

    bChangeServiceConfig = modAPI ChangeServiceConfig A(iServiceHandle      modAPI InfoLevel SERVICE_CONFIG_FAILURE_ACTIONS ref ServiceFailureActions);

    if (bChangeServiceConfig ==false)         throw new Exception( 不能设置服务的故障恢复设置 );              catch(Exception ex)           throw new Exception(ex Message);      finally           Marshal FreeHGlobal(iScActionsPointer);

  if (iServiceHandle > )         bCloseService = modAPI CloseServiceHandle(iServiceHandle);   

  if (iSCManagerLockHandle > )         bUnlockSCManager = modAPI UnlockServiceDatabase(iSCManagerLockHandle);   

  if (iSCManagerHandle != )         bCloseSCManager = modAPI CloseServiceHandle(iSCManagerHandle);           

  在安装完成后 我们对服务进行 这里可以修改的内容包括服务的描述 服务的故障处理等

  如果你在安装时 需要对服务进行自动处于运行状态 或卸载时需要自动将服务也卸载 你只要注册

  this beforeuninstall+=new InstallEventHandler(ProjectInstaller_BeforeUninstall);   this Committed+=new InstallEventHandler(ProjectInstaller_Committed);

  这二个事件

  mitted事件 在这里可以将安装的服务进行调整到运行状态

cha138/Article/program/net/201311/13252

相关参考

知识大全 C#创建Windows服务

C#创建Windows服务  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  &

知识大全 Delphi开发Windows服务程序

Delphi开发Windows服务程序  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  开发步骤:

知识大全 使用C# 创建 Windows 服务

使用C#创建Windows服务  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  

知识大全 Windows下常见Oracle服务介绍

Windows下常见Oracle服务介绍  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  注SID

知识大全 .Net Framework下安装Windows服务

.NetFramework下安装Windows服务  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 

知识大全 从Window系统托盘控制Windows服务

从Window系统托盘控制Windows服务  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  有许

知识大全 Visual C#中调用Windows服务初探

VisualC#中调用Windows服务初探  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Wi

知识大全 .NET下开发Windows 服务应用程序

.NET下开发Windows服务应用程序  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Wind

知识大全 为Tomcat注册的Windows服务增加安全参数

为Tomcat注册的Windows服务增加安全参数  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 

知识大全 如何配置windows server 2012 服务器配置

如何配置windowsserver2012服务器配置WindowsServer2012现已推向企业服务提供商提供伸缩、态、支持租户及通云计算优化基础结构WindowsServer2012帮助组织安全进