1. unit U_StartServices;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, WinSVC, StdCtrls;
  6. type
  7. TForm1 = class(TForm)
  8. btn_StartServices: TButton;
  9. btn_StopServices: TButton;
  10. procedure btn_StartServicesClick(Sender: TObject);
  11. procedure btn_StopServicesClick(Sender: TObject);
  12. private
  13. { Private declarations }
  14. public
  15. { Public declarations }
  16. end;
  17. var
  18. Form1: TForm1;
  19. function StartServices(const SvrName: string): Boolean;
  20. implementation
  21. {$R *.dfm}
  22. //开启服务
  23. function StartServices(const SvrName: string): Boolean;
  24. var
  25. SCH, SvcSCH: SC_HANDLE;
  26. arg: PChar;
  27. dwStartType: DWORD;
  28. begin
  29. Result := False;
  30. SCH := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  31. if SCH <= 0 then Exit;
  32. SvcSCH := OpenService(SCH, PChar(SvrName), SERVICE_ALL_ACCESS);
  33. if (ChangeServiceConfig(
  34. SvcSCH, //   handle   of   service
  35. SERVICE_NO_CHANGE, //SERVICE_NO_CHANGE,   //   service   type:   no   change
  36. SERVICE_AUTO_START, //   change   service   start   type
  37. SERVICE_NO_CHANGE, //   error   control:   no   change
  38. nil, //   binary   path:   no   change
  39. nil, //   load   order   group:   no   change
  40. nil, //   tag   ID:   no   change
  41. nil, //   dependencies:   no   change
  42. nil, //   account   name:   no   change
  43. nil, //   password:   no   change
  44. nil)) then
  45. showmessage('Auto Start OK')
  46. else
  47. showmessage('Auto Start Error');
  48. if SvcSCH <= 0 then Exit;
  49. try
  50. Result := StartService(SvcSCH, 0, arg);
  51. CloseServiceHandle(SvcSCH);
  52. CloseServiceHandle(SCH);
  53. except
  54. CloseServiceHandle(SvcSCH);
  55. CloseServiceHandle(SCH);
  56. Exit;
  57. end;
  58. end;
  59. //停止服务
  60. function StopServices(const SvrName: string): Boolean;
  61. var
  62. SCH, SvcSCH: SC_HANDLE;
  63. SS: TServiceStatus;
  64. begin
  65. Result := False;
  66. SCH := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  67. if SCH <= 0 then Exit;
  68. SvcSCH := OpenService(SCH, PChar(SvrName), SERVICE_ALL_ACCESS);
  69. if SvcSCH <= 0 then Exit;
  70. try
  71. Result := ControlService(SvcSCH, SERVICE_CONTROL_STOP, SS);
  72. CloseServiceHandle(SCH);
  73. CloseServiceHandle(SvcSCH);
  74. except
  75. CloseServiceHandle(SCH);
  76. CloseServiceHandle(SvcSCH);
  77. Exit;
  78. end;
  79. end;
  80. procedure TForm1.btn_StartServicesClick(Sender: TObject);
  81. begin
  82. if StartServices('PolicyAgent') = true then
  83. application.MessageBox(PChar('PolicyAgent 服务启动成功'), PChar('服务信息'), MB_ICONINFORMATION)
  84. else
  85. application.MessageBox(PChar('PolicyAgent 服务启动失败'), PChar('服务信息'), MB_ICONERROR);
  86. end;
  87. procedure TForm1.btn_StopServicesClick(Sender: TObject);
  88. begin
  89. if StopServices('PolicyAgent') = true then
  90. application.MessageBox(PChar('PolicyAgent 服务停止成功'), PChar('服务信息'), MB_ICONINFORMATION)
  91. else
  92. application.MessageBox(PChar('PolicyAgent 服务停止成功'), PChar('服务信息'), MB_ICONERROR);
  93. end;
  94. end.

复制代码

http://www.appweixin.net/thread-67-1-1.html

Delphi启动/停止Windows服务,启动类型修改为"自动"的更多相关文章

  1. delphi 启动停止windows服务 转

    http://blog.csdn.net/haiou327/article/details/6106233 不用cmd用delphi如何实现启动停止windows服务建议参考一下Delphi的Sckt ...

  2. Web启动,停止Windows服务

    When you grow stronger,the world become more dangerous.当你变得越强大,这个世界反而会变得越危险. ServiceModel.cs代码: publ ...

  3. 玩转Windows服务系列——Windows服务启动超时时间

    最近有客户反映,机房出现断电情况,服务器的系统重新启动后,数据库服务自启动失败.第一次遇到这种情况,为了查看是不是断电情况导致数据库文件损坏,从客户的服务器拿到数据库的日志,进行分析. 数据库工作机制 ...

  4. 玩转Windows服务系列——无COM接口Windows服务启动失败原因及解决方案

    将VS创建的Windows服务项目编译生成的程序,通过命令行 “服务.exe -Service”注册为Windows服务后,就可以通过服务管理器进行管理了. 问题 通过服务管理器进行启动的时候,发现服 ...

  5. MongoDB做为一项windows服务启动

    MongoDB做为一项windows服务启动 Windows版本安装 MongoDB的官方下载站是http://www.mongodb.org/downloads,可以去上面下载最新的对应版本,有32 ...

  6. C#判断程序是由Windows服务启动还是用户启动

    在Windows系统做网络开发,很多时候都是使用Windows服务的模式,但在调度阶段,我们更多的是使用控制台的模式.在开发程序的时候,我们在Program的Main入口进行判断.最初开始使用Envi ...

  7. Linux Systemd——在RHEL/CentOS 7中启动/停止/重启服务

    RHEL/CentOS 7.0中一个最主要的改变,就是切换到了systemd.它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服务进行管理.systemd兼容SysV和Li ...

  8. windows服务启动 1053错误

    1.问题描述 今天在启动一个Windows服务时,服务启动不了,且提示:1053错误 那么是什么导致了1053错误呢? 2.他山之石 百度了一下,发现有人作出下面的解释并给出了解决方法: “常常是因为 ...

  9. bat启动/停止oracle服务

    原文:bat启动/停止oracle服务 自己的电脑比较慢,尤其装了oracle10g后,服务开启和关闭用bat文件操作省事点 开启服务 @echo offnet start OracleService ...

随机推荐

  1. Mac 让 iTerm2 记住用户名密码 expect 脚本

    刚刚用iTerm2的时候,总是要一遍遍的敲用户名.密码. 我在想, 能不能像Windows的软件一样,可以直接让软件记住.然后只要点击一下,就直接ssh到远程服务器上面去了. 之后经过搜索,可以用ex ...

  2. HDU 3398 String

    题目大意:一个长为n的01字符串,使前缀任意0的数量不大于1的数量,求方案数…… 题解:高一模拟赛时做过,是卡特兰数的几何意义,将字符串变为矩阵寻路,不可越过对角线,那么就是卡特兰数了,C(n+m, ...

  3. Objective-c 类的继承 方法重写 方法重载

    一.类的继承 Objective-c中类的继承与C++类似,不同的是Objective-c不支持多重继承,一个类只能有一个父类,单继承使Objective-c的继承关系很简单,易于管理程序. Obje ...

  4. Python 参数传递

    python中的变量: 一个变量是局部还是全局,在编译函数的时候就已经决定,因此读变量值的时候也不会逐层向外查找.变量是全局还是局域,根据如下3条: 1. 如果函数内部有global语句,那么它声明的 ...

  5. 调用AnimateWindow API来实现弹出效果

    下面是实例的cs代码 public partial class frm_Main : Form { //使用Windows Api AnimateWindow [DllImport("use ...

  6. Querying Microsoft SQL Server 2012 读书笔记:查询和管理XML数据 1 -使用FOR XML返回XML结果集

    XML 介绍 <CustomersOrders> <Customer custid="1" companyname="Customer NRZBB&qu ...

  7. 获得view所在的控制器

    - (UIViewController*)getViewController{ for (UIView* next = [self superview]; next; next = next.supe ...

  8. Week13(12月5日):不怕错误,慢慢来 :)

    Part I:提问 =========================== 1.ASP.NET MVC的最新版本是(      ). A.2    B.3    C.4   D.5 2.本学期授课中使 ...

  9. Objective-C 链式编程思想

    链式编程思想 链式编程是什么 链式编程就是将调用多个方法用点语法连接起来,让代码更加简洁和可读性更高刚开始接触链式编程是Masonry,用起来真的非常爽 1 make.left.right.top.e ...

  10. QTcpSocket通信编程时阻塞与非阻塞的问题

    目标,qt程序作为客户端,windows下winsock作为服务器端,实现两端通信. 开始时写了一个小函数测试: [cpp] view plaincopy QTcpSocket tmpSock;  t ...