Delphi Start/Stop Windows service, Startup type modified to "Auto"

Source: Internet
Author: User

  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. Open service
  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. Stop Service
  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 service started successfully '), PChar (' Service Information '), mb_iconinformation)
  84. Else
  85. Application. MessageBox (PChar (' PolicyAgent service failed to start '), PChar (' Service Information '), mb_iconerror);
  86. End
  87. Procedure Tform1.btn_stopservicesclick (Sender:tobject);
  88. Begin
  89. If Stopservices (' policyagent ') = True Then
  90. Application. MessageBox (PChar (' PolicyAgent Service stopped successfully '), PChar (' Service Information '), mb_iconinformation)
  91. Else
  92. Application. MessageBox (PChar (' PolicyAgent Service stopped successfully '), PChar (' Service Information '), mb_iconerror);
  93. End
  94. End.
Copy Code

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

Delphi Start/Stop Windows service, Startup type modified to "Auto"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.