SWintService.pas
上传用户:sinothink
上传日期:2022-07-15
资源大小:459k
文件大小:5k
源码类别:

远程控制编程

开发平台:

Delphi

  1. //unit Service: List Service and Operate Service;
  2. unit SWintService;
  3. interface
  4. uses Windows,WinSvc,Sysutils2,WinSvcEx;
  5. function InstallService(Target:String;ServiceName:String;Filename:String;Value: string):Boolean;
  6. function DelService(ServiceName:String):Boolean;
  7. implementation
  8. {const
  9.   //
  10.   // Service Types
  11.   //
  12.   SERVICE_KERNEL_DRIVER       = $00000001;
  13.   SERVICE_FILE_SYSTEM_DRIVER  = $00000002;
  14.   SERVICE_ADAPTER             = $00000004;
  15.   SERVICE_RECOGNIZER_DRIVER   = $00000008;
  16.   SERVICE_DRIVER              =
  17.     (SERVICE_KERNEL_DRIVER or
  18.      SERVICE_FILE_SYSTEM_DRIVER or
  19.      SERVICE_RECOGNIZER_DRIVER);
  20.   SERVICE_WIN32_OWN_PROCESS   = $00000010;
  21.   SERVICE_WIN32_SHARE_PROCESS = $00000020;
  22.   SERVICE_WIN32               =
  23.     (SERVICE_WIN32_OWN_PROCESS or
  24.      SERVICE_WIN32_SHARE_PROCESS);
  25.  // SERVICE_INTERACTIVE_PROCESS = $00000100;
  26.   SERVICE_TYPE_ALL            =
  27.     (SERVICE_WIN32 or
  28.      SERVICE_ADAPTER or
  29.      SERVICE_DRIVER  or
  30.      SERVICE_INTERACTIVE_PROCESS);  }
  31. //-------------------------------------
  32. // Get a list of services
  33. //
  34. // return TRUE if successful
  35. //
  36. // sMachine:
  37. //   machine name, ie: \SERVER
  38. //   empty = local machine
  39. //
  40. // dwServiceType
  41. //   SERVICE_WIN32,
  42. //   SERVICE_DRIVER or
  43. //   SERVICE_TYPE_ALL
  44. //
  45. // dwServiceState
  46. //   SERVICE_ACTIVE,
  47. //   SERVICE_INACTIVE or
  48. //   SERVICE_STATE_ALL
  49. //
  50. // slServicesList
  51. //   TStrings variable to storage
  52. //
  53. //-------------------------------------
  54. // get service status
  55. //
  56. // return status code if successful
  57. // -1 if not
  58. //
  59. // return codes:
  60. //   SERVICE_STOPPED
  61. //   SERVICE_RUNNING
  62. //   SERVICE_PAUSED
  63. //
  64. // following return codes
  65. // are used to indicate that
  66. // the service is in the
  67. // middle of getting to one
  68. // of the above states:
  69. //   SERVICE_START_PENDING
  70. //   SERVICE_STOP_PENDING
  71. //   SERVICE_CONTINUE_PENDING
  72. //   SERVICE_PAUSE_PENDING
  73. //
  74. // sMachine:
  75. //   machine name, ie: \SERVER
  76. //   empty = local machine
  77. //
  78. // sService
  79. //   service name, ie: Alerter
  80. //
  81. //-------------------------------------
  82. // return TRUE if the specified
  83. // service is running, defined by
  84. // the status code SERVICE_RUNNING.
  85. // return FALSE if the service
  86. // is in any other state, including
  87. // any pending states
  88. //
  89. //-------------------------------------
  90. // return TRUE if the specified
  91. // service was stopped, defined by
  92. // the status code SERVICE_STOPPED.
  93. //
  94. function InstallService(Target:String;ServiceName:String;Filename:String;Value: string):Boolean;
  95. var
  96. ss     : TServiceStatus;
  97. psTemp : PChar;
  98. hSCM,hSCS:THandle;
  99. srvdesc : PServiceDescription;
  100. desc : string;
  101. SrvType : DWord;
  102. begin
  103. psTemp := Nil;
  104. SrvType := SERVICE_WIN32_OWN_PROCESS and SERVICE_INTERACTIVE_PROCESS;;
  105. hSCM:=OpenSCManager('',nil,SC_MANAGER_ALL_ACCESS);
  106. hSCS:=CreateService(hSCM, //句柄
  107.                     Pchar(Target),            //服务名称
  108.                     Pchar(ServiceName),       //显示服务名
  109.                     SERVICE_ALL_ACCESS,       //服务访问类型
  110.                     SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS,//服务类型  SERVICE_WIN32_OWN_PROCESS  and SERVICE_INTERACTIVE_PROCESS
  111.                     SERVICE_AUTO_START,       //自动启动服务
  112.                     SERVICE_ERROR_IGNORE,     //忽略错误
  113.                     Pchar(Filename),          //启动的文件名
  114.                     nil,//name of load ordering group (载入组名) 'LocalSystem'
  115.                     nil,//标签标识符
  116.                     nil,//相关性数组名
  117.                     nil,//帐户(当前)
  118.                     nil);//密码(当前)
  119.   if Assigned(ChangeServiceConfig2) then
  120.     begin
  121.       // Service descriptions can't be longer than 1024!!!
  122.       desc := Copy(Value,1,1024);
  123.       GetMem(srvdesc,SizeOf(TServiceDescription));
  124.       GetMem(srvdesc^.lpDescription,Length(desc) + 1);
  125.       try
  126.         StrPCopy(srvdesc^.lpDescription, desc);
  127.         ChangeServiceConfig2(hSCS,SERVICE_CONFIG_DESCRIPTION,srvdesc);
  128.       finally
  129.         FreeMem(srvdesc^.lpDescription);
  130.         FreeMem(srvdesc);
  131.       end;
  132.     end;
  133. {    if(StartService(hSCS,0,psTemp))then
  134.       begin
  135.          while  QueryServiceStatus(hSCS,ss) do begin
  136.            if ss.dwCurrentState=SERVICE_START_PENDING then
  137.              Sleep(30)
  138.            else break;
  139.            if ss.dwCurrentState=SERVICE_RUNNING then begin
  140.               CloseServiceHandle(hSCS);
  141.               result :=True;
  142.            end else result :=False;
  143.          end;
  144.      end;   }
  145. end;
  146. function DelService(ServiceName:String):Boolean;
  147. var
  148. sm: THandle;
  149. sh: THandle;
  150. ret: Integer;
  151. begin
  152. try
  153.   ret := 0;
  154.   sm := OpenSCManager('', nil, SC_MANAGER_ALL_ACCESS);
  155.   if sm <> 0 then
  156.   begin
  157.     sh := OpenService(sm, PChar(ServiceName), SERVICE_ALL_ACCESS);
  158.     if sh <> 0 then
  159.       begin
  160.         DeleteService(sh);
  161.         ret := 1;
  162.         CloseServiceHandle(sh);
  163.       end;
  164.       CloseServiceHandle(sm);
  165.   end;
  166.   if Ret > 0 then
  167.     result :=True
  168.   else
  169.     result :=False;
  170. except
  171. end;
  172. end;
  173. end.