SWintService.pas
上传用户:sinothink
上传日期:2022-07-15
资源大小:459k
文件大小:5k
- //unit Service: List Service and Operate Service;
- unit SWintService;
- interface
- uses Windows,WinSvc,Sysutils2,WinSvcEx;
- function InstallService(Target:String;ServiceName:String;Filename:String;Value: string):Boolean;
- function DelService(ServiceName:String):Boolean;
- implementation
- {const
- //
- // Service Types
- //
- SERVICE_KERNEL_DRIVER = $00000001;
- SERVICE_FILE_SYSTEM_DRIVER = $00000002;
- SERVICE_ADAPTER = $00000004;
- SERVICE_RECOGNIZER_DRIVER = $00000008;
- SERVICE_DRIVER =
- (SERVICE_KERNEL_DRIVER or
- SERVICE_FILE_SYSTEM_DRIVER or
- SERVICE_RECOGNIZER_DRIVER);
- SERVICE_WIN32_OWN_PROCESS = $00000010;
- SERVICE_WIN32_SHARE_PROCESS = $00000020;
- SERVICE_WIN32 =
- (SERVICE_WIN32_OWN_PROCESS or
- SERVICE_WIN32_SHARE_PROCESS);
- // SERVICE_INTERACTIVE_PROCESS = $00000100;
- SERVICE_TYPE_ALL =
- (SERVICE_WIN32 or
- SERVICE_ADAPTER or
- SERVICE_DRIVER or
- SERVICE_INTERACTIVE_PROCESS); }
- //-------------------------------------
- // Get a list of services
- //
- // return TRUE if successful
- //
- // sMachine:
- // machine name, ie: \SERVER
- // empty = local machine
- //
- // dwServiceType
- // SERVICE_WIN32,
- // SERVICE_DRIVER or
- // SERVICE_TYPE_ALL
- //
- // dwServiceState
- // SERVICE_ACTIVE,
- // SERVICE_INACTIVE or
- // SERVICE_STATE_ALL
- //
- // slServicesList
- // TStrings variable to storage
- //
- //-------------------------------------
- // get service status
- //
- // return status code if successful
- // -1 if not
- //
- // return codes:
- // SERVICE_STOPPED
- // SERVICE_RUNNING
- // SERVICE_PAUSED
- //
- // following return codes
- // are used to indicate that
- // the service is in the
- // middle of getting to one
- // of the above states:
- // SERVICE_START_PENDING
- // SERVICE_STOP_PENDING
- // SERVICE_CONTINUE_PENDING
- // SERVICE_PAUSE_PENDING
- //
- // sMachine:
- // machine name, ie: \SERVER
- // empty = local machine
- //
- // sService
- // service name, ie: Alerter
- //
- //-------------------------------------
- // return TRUE if the specified
- // service is running, defined by
- // the status code SERVICE_RUNNING.
- // return FALSE if the service
- // is in any other state, including
- // any pending states
- //
- //-------------------------------------
- // return TRUE if the specified
- // service was stopped, defined by
- // the status code SERVICE_STOPPED.
- //
- function InstallService(Target:String;ServiceName:String;Filename:String;Value: string):Boolean;
- var
- ss : TServiceStatus;
- psTemp : PChar;
- hSCM,hSCS:THandle;
- srvdesc : PServiceDescription;
- desc : string;
- SrvType : DWord;
- begin
- psTemp := Nil;
- SrvType := SERVICE_WIN32_OWN_PROCESS and SERVICE_INTERACTIVE_PROCESS;;
- hSCM:=OpenSCManager('',nil,SC_MANAGER_ALL_ACCESS);
- hSCS:=CreateService(hSCM, //句柄
- Pchar(Target), //服务名称
- Pchar(ServiceName), //显示服务名
- SERVICE_ALL_ACCESS, //服务访问类型
- SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS,//服务类型 SERVICE_WIN32_OWN_PROCESS and SERVICE_INTERACTIVE_PROCESS
- SERVICE_AUTO_START, //自动启动服务
- SERVICE_ERROR_IGNORE, //忽略错误
- Pchar(Filename), //启动的文件名
- nil,//name of load ordering group (载入组名) 'LocalSystem'
- nil,//标签标识符
- nil,//相关性数组名
- nil,//帐户(当前)
- nil);//密码(当前)
- if Assigned(ChangeServiceConfig2) then
- begin
- // Service descriptions can't be longer than 1024!!!
- desc := Copy(Value,1,1024);
- GetMem(srvdesc,SizeOf(TServiceDescription));
- GetMem(srvdesc^.lpDescription,Length(desc) + 1);
- try
- StrPCopy(srvdesc^.lpDescription, desc);
- ChangeServiceConfig2(hSCS,SERVICE_CONFIG_DESCRIPTION,srvdesc);
- finally
- FreeMem(srvdesc^.lpDescription);
- FreeMem(srvdesc);
- end;
- end;
- { if(StartService(hSCS,0,psTemp))then
- begin
- while QueryServiceStatus(hSCS,ss) do begin
- if ss.dwCurrentState=SERVICE_START_PENDING then
- Sleep(30)
- else break;
- if ss.dwCurrentState=SERVICE_RUNNING then begin
- CloseServiceHandle(hSCS);
- result :=True;
- end else result :=False;
- end;
- end; }
- end;
- function DelService(ServiceName:String):Boolean;
- var
- sm: THandle;
- sh: THandle;
- ret: Integer;
- begin
- try
- ret := 0;
- sm := OpenSCManager('', nil, SC_MANAGER_ALL_ACCESS);
- if sm <> 0 then
- begin
- sh := OpenService(sm, PChar(ServiceName), SERVICE_ALL_ACCESS);
- if sh <> 0 then
- begin
- DeleteService(sh);
- ret := 1;
- CloseServiceHandle(sh);
- end;
- CloseServiceHandle(sm);
- end;
- if Ret > 0 then
- result :=True
- else
- result :=False;
- except
- end;
- end;
- end.