启动/停止服务

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了启动/停止服务相关的知识,希望对你有一定的参考价值。

  1. function ServiceStart(sMachine,sService : string ) : boolean;
  2. var
  3. schm,schs : SC_Handle;
  4. ss : TServiceStatus;
  5. psTemp : PChar;
  6. dwChkP : DWord;
  7. begin
  8. ss.dwCurrentState := 0;
  9. schm := OpenSCManager(PChar(sMachine),Nil,SC_MANAGER_CONNECT);
  10. if(schm > 0)then
  11. begin
  12. schs := OpenService(schm,PChar(sService),SERVICE_START or SERVICE_QUERY_STATUS);
  13. if(schs > 0)then
  14. begin
  15. psTemp := Nil;
  16. if(StartService(schs,0,psTemp))then
  17. begin
  18. if(QueryServiceStatus(schs,ss))then
  19. begin
  20. while(SERVICE_RUNNING <> ss.dwCurrentState)do
  21. begin
  22. dwChkP := ss.dwCheckPoint;
  23. Sleep(ss.dwWaitHint);
  24. if(not QueryServiceStatus(schs,ss))then
  25. begin
  26. break;
  27. end;
  28. if(ss.dwCheckPoint < dwChkP)then
  29. begin
  30. break;
  31. end;
  32. end;
  33. end;
  34. end;
  35. CloseServiceHandle(schs);
  36. end;
  37. CloseServiceHandle(schm);
  38. end;
  39. Result := SERVICE_RUNNING = ss.dwCurrentState;
  40. end;
  41.  
  42. function ServiceStop(sMachine,sService : string ) : boolean;
  43. var
  44. schm,schs : SC_Handle;
  45. ss : TServiceStatus;
  46. dwChkP : DWord;
  47. begin
  48. schm := OpenSCManager(PChar(sMachine),Nil,SC_MANAGER_CONNECT);
  49. if(schm > 0)then
  50. begin
  51. schs := OpenService(schm,PChar(sService),SERVICE_STOP or SERVICE_QUERY_STATUS);
  52. if(schs > 0)then
  53. begin
  54. if(ControlService(schs,SERVICE_CONTROL_STOP,ss))then
  55. begin
  56. if(QueryServiceStatus(schs,ss))then
  57. begin
  58. while(SERVICE_STOPPED <> ss.dwCurrentState)do
  59. begin
  60. dwChkP := ss.dwCheckPoint;
  61. Sleep(ss.dwWaitHint);
  62.  
  63. if(not QueryServiceStatus(schs,ss))then
  64. begin
  65. break;
  66. end;
  67.  
  68. if(ss.dwCheckPoint < dwChkP)then
  69. begin
  70. break;
  71. end;
  72. end;
  73. end;
  74. end;
  75. CloseServiceHandle(schs);
  76. end;
  77. CloseServiceHandle(schm);
  78. end;
  79. Result := SERVICE_STOPPED = ss.dwCurrentState;
  80. end;

以上是关于启动/停止服务的主要内容,如果未能解决你的问题,请参考以下文章

如何查看或停止Linux启动的服务?

MYSQL服务停止,Apach启动后自动停止是怎么回事?

mssqlserver服务启动后又停止了?

描述用命令如何启动,停止,重新启动和查看www服务?

本计算机上的mssqlserver服务启动后又停止了

Sql server 2005 服务启动很快又停止