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. (asp.net MVC学习)System.Web.Mvc.UrlHelper的学习与使用

    上一次学习了HtmlHelper帮助类,这次我们学习一下UrlHelper帮 助类,看类名也都知道这个类是用来帮我们生成URL在ASP.NET MVC应用程序中.让我们来看看该类给我们带来了哪些方便的 ...

  2. Mathematica

    Mathematica是一款科学计算软件,很好地结合了数值和符号计算引擎.图形系统.编程语言.文本系统.和与其他应用程序的高级连接.很多功能在相应领域内处于世界领先地位,它也是使用最广泛的数学软件之一 ...

  3. 安装 Rational Rose 启动报错:无法启动此程序,因为计算机中丢失 suite objects.dll

    安装完以后提示找不到 suite objects.dll: 经查找,该 dll 存在: 找不到的原因是,安装程序自动设置在 Path 中的环境变量有误: 把最后的 common 改成 Common: ...

  4. MySQL DBA成长之路

    http://blog.51cto.com/zt/579 :GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'm ...

  5. angulajs 如何在controller 之间共享数据

    当在一个controller中 通过设置$scope的数据,来影响其它control的范围时,可以使用$rootScope 举个例子: 在一个control A范围内设置 mainApp.contro ...

  6. What is a good EPUB reader on Linux

    Last updated on August 20, 2014 Authored by Adrien Brochard 12 Comments If the habit on reading book ...

  7. Windows Azure 存储的冗余存储选项和只读访问跨地域冗余存储

    我们很高兴地宣布,现在我们使客户可以获得对数据更高的读取可用性.该预览功能称为"只读访问- 跨地域冗余存储(RA-GRS)",使客户可以在存储帐户主要区域无法读取数据时,通过跨 ...

  8. 基于Visual C++2013拆解世界五百强面试题--题11-查找数字出现次数

    在排序数组中,找出给定数字出现的次数比如{ 1, 2, 2, 2, 3}中2的出现次数是3次 我们可使用二分查找发,分别查找出2最先出现的位置和最后出现的位置相减即可. 下面是上代码: #includ ...

  9. flexjson 的使用

    日期转换             JSONSerializer serializer = new JSONSerializer();         serializer.exclude(new St ...

  10. IE升级到10.0,VS2010启动调试时报“未能将脚本调试器附加到计算机..”

    IE升级到10.0,VS2010启动调试时报“未能将脚本调试器附加到计算机..” 今天,在调试代码时,调试器弹出提示框,报:未能将脚本调试器附加到计算机XXX上的进程iexplore.exe . 已附 ...