Posted By

icebob on 08/02/10


Tagged

service


Versions (?)

Start/Stop service


 / Published in: Delphi
 

  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;

Report this snippet  

You need to login to post a comment.