Windows Service的注册的两种方法(转贴加整理)
一. 用VS2005实现;
二.调用API注册和注销Windows Service (转自:疾风之狼的BLOG)

一. 用VS2005实现;
1.新建一WINDOWS SERVICES工程WindowsService1;
2.双击打开Service1.cs,右键选择INSTALLER添加;
3.编译生成WindowsService1.exe.
4.使用命令installutil -i D:/Qiuwu/ProLab/2005C#.NET/WindowsService1/WindowsService1/bin/Debug/WindowsService1.exe,注册服务,
installutil -u D:/Qiuwu/ProLab/2005C#.NET/WindowsService1/WindowsService1/bin/Debug/WindowsService1.exe,卸载服务,
5.打开“允许服务与桌面交互”的功能,是得服务可以运行带画面的程序.方法:控制面板->管理工具->服务->选择刚安装的服务右击->属性->登陆(LOGON)->选择(LOCAL SYSTEM ACCOUNT),将“允许服务与桌面交互”复选框钩上即可.
一下是一些实现类似功能的C#代码:
-----------立即启动--------------
private   void   serviceInstaller1_AfterInstall(object   sender,   System.Configuration.Install.InstallEventArgs   e)
{
ServiceController   myService   =   new   ServiceController("TestWindowsServices");
myService.Start();
myService.Dispose();
}
添加描述:1.1没有直接方法,2.0里有直接的方法   ServiceInstaller.Description
//----------------------------添加服务描述信息   开始   ------------
public   override   void   Install(IDictionary   stateServer)
{
Microsoft.Win32.RegistryKey   system,
//HKEY_LOCAL_MACHINE/Services/CurrentControlSet
currentControlSet,
//.../Services
services,
//.../ <Service   Name>
service,
//.../Parameters   -   this   is   where   you   can   put   service-specific   configuration
config;  
try
{
//Let   the   project   installer   do   its   job
base.Install(stateServer);

//Open   the   HKEY_LOCAL_MACHINE/SYSTEM   key
system   =   Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
//Open   CurrentControlSet
currentControlSet   =   system.OpenSubKey("CurrentControlSet");
//Go   to   the   services   key
services   =   currentControlSet.OpenSubKey("Services");
//Open   the   key   for   your   service,   and   allow   writing
service   =   services.OpenSubKey(this.serviceInstaller1.ServiceName,   true);
//Add   your   service's   description   as   a   REG_SZ   value   named   "Description"
service.SetValue("Description","Test Windows Services");
//(Optional)   Add   some   custom   information   your   service   will   use...
//注意此处即为实现“允许服务与桌面交互”
service.SetValue("Type",0x00000110);
config   =   service.CreateSubKey("Parameters");
}
catch(Exception   e)
{
Console.WriteLine("An   exception   was   thrown   during   service   installation:/n"   +   e.ToString());
}
}

public   override   void   Uninstall(IDictionary   stateServer)
{
Microsoft.Win32.RegistryKey   system,
currentControlSet,
services,
service;

try
{
//Drill   down   to   the   service   key   and   open   it   with   write   permission
system   =   Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
currentControlSet   =   system.OpenSubKey("CurrentControlSet");
services   =   currentControlSet.OpenSubKey("Services");
service   =   services.OpenSubKey(this.serviceInstaller1.ServiceName,   true);
//Delete   any   keys   you   created   during   installation   (or   that   your   service   created)
service.DeleteSubKeyTree("Parameters");
//...
}
catch(Exception   e)
{
Console.WriteLine("Exception   encountered   while   uninstalling   service:/n"   +   e.ToString());
}
finally
{
//Let   the   project   installer   do   its   job
base.Uninstall(stateServer);
}
}
//----------------------------   结束   ----------------------------

二.调用API注册和注销Windows Service (转自:疾风之狼的BLOG) 
调用API注册和注销Windows Service
(转自:http://blog.csdn.net/Mittermeyer/archive/2004/01/04/11653.aspx)
0、写在前面

    DotNET平台下的类库封装的相当完善,普通的应用完全可以利用类库完成所有的工作。对于Windows Service的支持也是一样,只需要继承DotNET下提供的ServiceBase就可以创建Windows的Service,调用ServiceControl类的方法就可以控制Service的启动和关闭,非常容易。
    然而生成了一个Service类型的应用程序之后,必须在SCM(Service Control Manager)中注册,才能够被当作一个Service被系统调用。但是对于Service的注册和注销却比较麻烦,DotNET的确也提供了对应的类ServiceInstaller和ServiceProcessInstaller,但是只能够和Installer的安装过程集成使用,不能够单独在普通应用程序中调用,也就是说为了安装一个Service用作测试,我不得不创建一个安装包。
    怎样利用API向SCM注册以及注销一个Service呢,下面就简单的介绍一下。
 
1、实现概述
    打开Control Panel中Service的管理工具,可以看到本机所有已注册的Service,选择一个Service的属性会发现一个Service有很多设置项:名称、描述、类型、运行帐号等。所有这些都可以通过调用API函数来设定。
    在Service的API中,每一个服务的实例通过句柄来表示,而需要服务的句柄之前必须要先要的到SCM的句柄(Handle),所以所有的调用都是通过OpenSCManager开始,成功的调用OpenSCManager后将获得一个SCM的句柄。如果是注册Service,那么利用SCM句柄调用CreateService来创建一个新的服务;如果是注销Service,则调用OpenService函数,获得一个已经存在的Service的句柄,然后利用这个服务句柄调用DeleteService注销服务。最后通过CloseServiceHandle关闭Service的句柄和SCM的句柄。
    过程很简单,在CSharp下比较麻烦的地方就是API的声明,下面会一一提到。
 
2、注册Service
    前面已经提到注册Service一共用到了三个API,OpenSCManager、CreateService和CloseServiceHandle。使用之前先介绍一下这些API的声明。
  [DllImport("advapi32.dll")]
  public static extern System.IntPtr OpenSCManager(
    System.String lpMachineName,
    System.String lpDatabaseName,
    System.UInt32 dwDesiredAccess
   );
  [DllImport("advapi32.dll",EntryPoint = "CreateServiceA")]
  public static extern System.IntPtr CreateService(
    System.IntPtr hSCManager,
    System.String lpServiceName,
    System.String lpDisplayName,
    System.UInt32 dwDesiredAccess,
    System.UInt32 dwServiceType,
    System.UInt32 dwStartType,
    System.UInt32 dwErrorControl,
    System.String lpBinaryPathName,
    System.String lpLoadOrderGroup,
    System.IntPtr lpdwTagId,
    System.String lpDependencies,
    System.String lpServiceStartName,
    System.String lpPassword
   );
  [DllImport("advapi32.dll")]
  public static extern System.Boolean CloseServiceHandle(
    System.IntPtr hSCObject
   );
    这个了关于Service的API都是在Advapi32.dll中实现的,函数的原型可以自行查找头文件,在Winsvc和Winbase中。
    熟悉Windows编程的话一定会了解,句柄类型就是一个32位的整型,在DotNET下用IntPtr来声明,DWORD对应UINT32,LPCSTR对应String类型,唯一需要强调的是CreateService这个函数的lpdwTagId,是一个DWORD*,这里声明也IntPtr,因为在调用中绝大多数情况下传递NULL值,如果用out UINT32,无法传递NULL值。
    仔细看CreateService中声明可以发现,这个函数真的可以做很多事情,其中包括Service的名称(lpServiceName,服务的标识,调用OpenSerive等函数时用到)、显示名(lpDisplayName,就是我们在Service的管理工具中看到的名称 )、服务类型(dwServiceType,指定服务的运行方式:独立进程、共享进程、驱动程序还是交互式登录模式等等)、启动类型(dwStartType,自动、手动还是禁止等等)、服务失败的严重性(dwErrorControl)、实现服务代码的二进制文件的路径(lpBinaryPathName)、加载顺序组的名称(lpLoadOrderGroup)、接受Tag标志码(lpdwTagId )、依赖服务的名称组(lpDependencies)、启动服务的帐号(lpServiceStartName,如果为NULL,表示使用LocalSystem)、启动服务帐号的口令(lpPassword)。如果调用成功,那么将返回一个非0的句柄,表示服务注册成功。
    看了上面的一系列属性说明,大家也许发现还少了一样,就是在Service的管理工具中最长最醒目的一栏Description,Description的设置需要调用另一个API,如下:
  public static extern System.Boolean ChangeServiceConfig2(
    System.IntPtr hService,
    System.UInt32 dwInfoLevel,
    ref System.String lpInfo
   );
    其中lpInfo的声明原型是一个LPVOID,如果设置Description属性的话指向的是一个结构:
 typedef struct _SERVICE_DESCRIPTION {
   LPTSTR lpDescription;
 } SERVICE_DESCRIPTION, *LPSERVICE_DESCRIPTION;
    这个结构里面包含了一个字符指针,也就是说需要在函数调用时传递一个指向字符指针的指针,偷懒起见,ref System.String就足够了,无需再定义结构了,呵呵!
    完整的例子如下,常量定义见本文最后:
  private static System.Boolean RegistService()
  {
   System.Boolean  fRet = false;
   System.IntPtr  hServiceManager = IntPtr.Zero,hService = IntPtr.Zero;
   System.String  sServicePath = null,sDesc = null;
 
   sServicePath = Application.StartupPath + @"/sampleservice.exe";
   hServiceManager = OpenSCManager(Environment.MachineName,null,SC_MANAGER_ALL_ACCESS);
   if (hServiceManager != IntPtr.Zero)
   {
    hService = CreateService(hServiceManager,"sampleservice","Sample Service",SERVICE_ALL_ACCESS,
     SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,
     sServicePath,null,IntPtr.Zero,null,null,null);
    if (hService != IntPtr.Zero)
    {
     sDesc = "This is a sample service.";
     fRet = ChangeServiceConfig2(hService,SERVICE_CONFIG_DESCRIPTION,ref sDesc);
     CloseServiceHandle(hService);
     hService = IntPtr.Zero;
    }
    CloseServiceHandle(hServiceManager);
    hServiceManager = IntPtr.Zero;
   }
   return fRet;
  }
3、注销Service
    相对于注册,注销就简单很多了,用到了OpenSCManager、OpenService、DeleteService和CloseServiceHandle四个API,另两个API的声明如下:
  [DllImport("advapi32.dll")]
  public static extern System.IntPtr OpenService(
    System.IntPtr hSCManager,
    System.String lpServiceName,
    System.UInt32 dwDesiredAccess
   );
  [DllImport("advapi32.dll")]
  public static extern System.Boolean DeleteService(
    System.IntPtr hService
   );
    比较简单没什么可多说的看一个完整的例子:
  private static System.Boolean UnRegistService()
  {
   System.Boolean  fRet = false;
   System.IntPtr  hServiceManager = IntPtr.Zero,hService = IntPtr.Zero;
 
   hServiceManager = OpenSCManager(Environment.MachineName,null,SC_MANAGER_ALL_ACCESS);
   if (hServiceManager != IntPtr.Zero)
   {
    hService = OpenService(hServiceManager,"sampleservice",SERVICE_ALL_ACCESS);
    if (hService != IntPtr.Zero)
    {
     fRet = DeleteService(hService);
     CloseServiceHandle(hService);
     hService = IntPtr.Zero;
    }
    CloseServiceHandle(hServiceManager);
    hServiceManager = IntPtr.Zero;
   }
   return fRet;
  }
4、最后
    可以在实现Service的工程中增加一下额外的处理,Main函数中判断一下调用参数,如果是“-I”,则表示注册Service,如果是“-U”表示注销Service,反之则作为Service正常运行。
 
5、API和常量的声明
    本文所用到的所有API和常量如下:
  // declare APIs for service
  [DllImport("advapi32.dll")]
  public static extern System.IntPtr OpenSCManager(
    System.String lpMachineName,
    System.String lpDatabaseName,
    System.UInt32 dwDesiredAccess
   );
  [DllImport("advapi32.dll",EntryPoint = "CreateServiceA")]
  public static extern System.IntPtr CreateService(
    System.IntPtr hSCManager,
    System.String lpServiceName,
    System.String lpDisplayName,
    System.UInt32 dwDesiredAccess,
    System.UInt32 dwServiceType,
    System.UInt32 dwStartType,
    System.UInt32 dwErrorControl,
    System.String lpBinaryPathName,
    System.String lpLoadOrderGroup,
    System.IntPtr lpdwTagId,
    System.String lpDependencies,
    System.String lpServiceStartName,
    System.String lpPassword
   );
  [DllImport("advapi32.dll")]
  public static extern System.IntPtr OpenService(
    System.IntPtr hSCManager,
    System.String lpServiceName,
    System.UInt32 dwDesiredAccess
   );
  [DllImport("advapi32.dll")]
  public static extern System.Boolean DeleteService(
    System.IntPtr hService
   );
  [DllImport("advapi32.dll")]
  public static extern System.Boolean CloseServiceHandle(
    System.IntPtr hSCObject
   );
  [DllImport("advapi32.dll")]
  public static extern System.Boolean ChangeServiceConfig2(
    System.IntPtr hService,
    System.UInt32 dwInfoLevel,
    ref System.String lpInfo
   );
 
  public const System.UInt32 STANDARD_RIGHTS_REQUIRED = 0xF0000;
  // Service Control Manager object specific access types
  public const System.UInt32 SC_MANAGER_CONNECT = 0x0001;
  public const System.UInt32 SC_MANAGER_CREATE_SERVICE = 0x0002;
  public const System.UInt32 SC_MANAGER_ENUMERATE_SERVICE = 0x0004;
  public const System.UInt32 SC_MANAGER_LOCK = 0x0008;
  public const System.UInt32 SC_MANAGER_QUERY_LOCK_STATUS = 0x0010;
  public const System.UInt32 SC_MANAGER_MODIFY_BOOT_CONFIG = 0x0020;
  public const System.UInt32 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;
  // Service object specific access type
  public const System.UInt32 SERVICE_QUERY_CONFIG = 0x0001;
  public const System.UInt32 SERVICE_CHANGE_CONFIG = 0x0002;
  public const System.UInt32 SERVICE_QUERY_STATUS = 0x0004;
  public const System.UInt32 SERVICE_ENUMERATE_DEPENDENTS = 0x0008;
  public const System.UInt32 SERVICE_START = 0x0010;
  public const System.UInt32 SERVICE_STOP = 0x0020;
  public const System.UInt32 SERVICE_PAUSE_CONTINUE = 0x0040;
  public const System.UInt32 SERVICE_INTERROGATE = 0x0080;
  public const System.UInt32 SERVICE_USER_DEFINED_CONTROL = 0x0100;
  public const System.UInt32 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;
  // service type
  public const System.UInt32 SERVICE_KERNEL_DRIVER = 0x00000001;
  public const System.UInt32 SERVICE_FILE_SYSTEM_DRIVER = 0x00000002;
  public const System.UInt32 SERVICE_ADAPTER = 0x00000004;
  public const System.UInt32 SERVICE_RECOGNIZER_DRIVER = 0x00000008;
  public const System.UInt32 SERVICE_DRIVER = SERVICE_KERNEL_DRIVER |
    SERVICE_FILE_SYSTEM_DRIVER |
    SERVICE_RECOGNIZER_DRIVER;
  public const System.UInt32 SERVICE_WIN32_OWN_PROCESS = 0x00000010;
  public const System.UInt32 SERVICE_WIN32_SHARE_PROCESS = 0x00000020;
  public const System.UInt32 SERVICE_WIN32 = SERVICE_WIN32_OWN_PROCESS |
    SERVICE_WIN32_SHARE_PROCESS;
  public const System.UInt32 SERVICE_INTERACTIVE_PROCESS = 0x00000100;
  public const System.UInt32 SERVICE_TYPE_ALL = SERVICE_WIN32  |
    SERVICE_ADAPTER |
    SERVICE_DRIVER  |
    SERVICE_INTERACTIVE_PROCESS;
  // Start Type
  public const System.UInt32 SERVICE_BOOT_START = 0x00000000;
  public const System.UInt32 SERVICE_SYSTEM_START = 0x00000001;
  public const System.UInt32 SERVICE_AUTO_START = 0x00000002;
  public const System.UInt32 SERVICE_DEMAND_START = 0x00000003;
  public const System.UInt32 SERVICE_DISABLED = 0x00000004;
  // Error control type
  public const System.UInt32 SERVICE_ERROR_IGNORE = 0x00000000;
  public const System.UInt32 SERVICE_ERROR_NORMAL = 0x00000001;
  public const System.UInt32 SERVICE_ERROR_SEVERE = 0x00000002;
  public const System.UInt32 SERVICE_ERROR_CRITICAL = 0x00000003;
  // Info levels for ChangeServiceConfig2 and QueryServiceConfig2
  public const System.UInt32 SERVICE_CONFIG_DESCRIPTION = 1;
  public const System.UInt32 SERVICE_CONFIG_FAILURE_ACTIONS = 2;

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=11653

GridView CheckBox 实现全选,跨页 CheckBox选择相关推荐

  1. 微信小程序checkbox的全选以及所有checkbox选中之后的全选

    微信小程序checkbox的全选以及所有checkbox选中之后的全选 微信小程序checkbox的全选以及所有checkbox选中之后的全选 第一次写,软件都不懂,直接把代码拷过来了 模板 WXML ...

  2. layui 实现表单、表格中复选框checkbox的全选功能

    一.layui 实现表单中多选框的全选功能,代码如下: //html页面表单 <form class="layui-form"><div class=" ...

  3. 实现checkbox的全选和取消

    如果点击全选就把所有选项全选上,这个我们常用... View Code 1 <script type="text/javascript"> 2 /*实现checkbox ...

  4. JQuery 判断checkbox是否选中,checkbox全选,获取checkbox选中值

    2019独角兽企业重金招聘Python工程师标准>>> JQuery是一个非常容易上手的框架,但是有很多东西需要我们深入学习的. 判断checkbox是否被选中网上有选多种写法,这里 ...

  5. 在项目中学习.NET的JQuery CheckBox方法(全选、取消全选、其他)

    一.在项目中遇到的CheckBox的全选和取消全选以及其他等解决方案如下: // 对全选和取消全选的事件 $("#CheckAll").click(function () {    ...

  6. JS如何控制checkbox的全选反选

    JS代码: 1 <script language="javascript" type="text/javascript"> 2 3 //转载请保留出 ...

  7. jQuery实现checkbox的全选反选方法

    checkbox的全选.取消全选.选中所有奇数.选中所有偶数等方法的实现代码如下: 注意jQuery的版本:jQuery1.6增加了prop,1.6之前的还是使用attr()和removeAttr() ...

  8. JQuery实现复选框CheckBox的全选、反选、提交操作

    对复选框最基本的应用,就是对复选框进行全选.反选和提交等操作.复杂的操作需要与选项挂钩,来达到各种级联反应效果. [示例]使用Jquery实现复选框CheckBox的全选.反选.提交操作. (1)创建 ...

  9. Android高级控件(一)——ListView绑定CheckBox实现全选,增加和删除等功能

    Android高级控件(一)--ListView绑定CheckBox实现全选,增加和删除等功能 这个控件还是挺复杂的,也是项目中应该算是比较常用的了,所以写了一个小Demo来讲讲,主要是自定义adap ...

  10. layui checkbox 反选/全选/取消单个取消全选/全部选中勾上全选

    更: 下面连接文章就是解决checkbox只能取到最后一个值的解决办法 https://blog.csdn.net/qq_16513911/article/details/81981733 <d ...

最新文章

  1. 嵌入式四大通信接口的解释
  2. Java 必须掌握的 12 种 Spring 常用注解!
  3. 高校复试计算机英语文献翻译,专业文献英语翻译复试.pdf
  4. 你还记得当初为什么进入IT行业吗?
  5. 用clock()统计代码的执行时间(C语言)
  6. ASP.NET MVC:4 Ways To Prevent Duplicate Form Submission(转载)
  7. 软件外包故事 - 加入团队战斗
  8. 云服务器搭建代挂,服务器上搭建个人博客
  9. 【mybatis】xml中定义变量
  10. Nugine: Rust 性能调优
  11. 设计模式(4):生成器模式(Builder)
  12. Initialize flexnet service failed error code 50003错误
  13. oracle 18c,Oracle 18c
  14. 【C语言】扫雷游戏详解及完整代码
  15. 解决Establishing SSL connection without server‘s identity verification is not recommended问题
  16. AutoCAD系统变量大全
  17. 数据分析报告2:Superstore销售情况分析
  18. 56.com flash http://www.56.com/deux4_97177389.swf
  19. 新年新希望--爱摸鱼的美工(12)
  20. Python内置函数——最值与求和

热门文章

  1. 使用devstack在单机上安装openstack(stein版本)和zun的踩坑之路
  2. java中键盘录入对象
  3. 芯片工艺SSG/FFG/TT的区别与联系
  4. java数组空指针问题
  5. 让你秒懂什么是 SEM、EDM、CPS、CPA、ROI、SEO……
  6. 油井远程监控解决方案
  7. cta策略 有哪些_什么是CTA策略?什么是管理期货策略?
  8. VAAI和VASA以及vVOL
  9. 西安自考计算机大专学校,西安成人自考大专学校有哪些
  10. 《Python Web开发实战》踩地雷记17/3/21