Specific implementation of Windows 2000 and NT services registration

zhaozj2021-02-17  31

Specific implementation of WINDWOS 2000 and NT service registration

{********************************************************** ************}

{********************************************************** ******} {* programming: Chen Linmao write {* date: 2003-01-05 {* Contact the author: linmaochen@sohu.com{**************** ***************************************

InterfaceUses Windows, Classes, Sysutils, Winsvc, NB30, Winsock

Type

{Service handle information} Tscminfo = Record hostname: string; DatabaseName: String; DesireAccess: DWORD; END;

{Service Information} TSVCINFO = Record HscManager: Integer; ServerName: String; // Service Name DisplayName: String; // Service Display Name DesireAccess: DWORD; // ServiceType: DWORD; // Service Categories StartType: DWORD; // Launch mode error ErrorControl: DWORD; // Service Error Control Mode BinaryPathName: String; // Service Executive Path: String; TagID: DWORD; Dependencies: String; // Dependendencies: String; // Related: When there is a number of dependencies, In the middle of space separation} ServerStartName: String; Password: string;

/ / An array of service information that needs to be registered

SvcaRray = array [1..20] of tsvcinfo;

Type

{Service Control Class} TService = Class (TOBJECT) Private SCM_INFO: Tscminfo; SVC_INFO: TSVCINFO; SCMHANDLE: Integer;

{Service handle} svchandle: integer;

Public Constructor Create (); Destructor Destroy (); Override; Function SetScmInfo (): Boolean; Function OpenScmHandle (): Integer; Function SetServiceInfo (TmpSvc: TsvcInfo): Boolean; Function CreateService (): Boolean;

{Delete a service} Function delete_asexy: Boolean;

{Establish a service} function open_service (serviceName: string): boolean

{/} {// Function Name: Issvcexists () {// Function: Determines whether the relevant service in the system exists {// parameter information: Service name to be judged {// return value: True: Windows Service Presented, False: The service in Windows does not exist {/ {/} function issvcexists (ServiceName: string): boolean;

{/} {// function name: start_service () {// Function function: Start related service process {// parameter information: Service name you need to start {// Return value: True: Service has been started; false: service startup Failure; {/} Function Control_Service (ServiceName: integer): boolean; {/} {// function name: isserviceStart () {// function function: Judgment service starts {// parameter information: need to be judged Service Name {// Return Value: True: Service has been started; false: Service has stopped {/} Function isserviceStart (ServiceName: String): Boolean;

END;

IMPLEMENTATION

{TService}

Constructor TService.create; Begin Try SCMHANDLE: = 0; setscminfo (); SCMHANDLE: = OpenScManager (Pchar (SCM_INFO.HOSTNAME), NIL, SCM_INFO.DESIREACCESS; EXCEPT END;

END;

Establishing a service {} function TService.CreateService (): Boolean; Var i: Integer; begin Try OpenScmHandle (); svc_info.HscManager: = ScmHandle; Result: = False; SvcHandle: = CreateService (svc_Info.HscManager, Pchar (svc_Info. ServerName), Pchar (Svc_Info.DisplayName), Svc_info.DesireAccess, svc_INfo.ServiceType, Svc_Info.StartType, Svc_Info.ErrorControl, pchar (Svc_Info.BinaryPathName), pchar ( ''), LPDWORD (0), Pchar (Svc_Info.Dependencies) Pchar (SVC_INFO.SERVERSTARTNAME), PCHAR (SVC_INFO.PASSWORD);

If Svchandle <> 0 THEN BEGIN RESULT: = true; ClosESERVICEHANDE (SVCHANDLE); END; i: = i 1; Except End;

END;

A service cancellation {} function TService.Delete_AService (ServiceName: String): Boolean; Var RHandle: Integer; rc: Boolean; begin Try OpenScmHandle (); Result: = False; If ScmHandle <> 0 Then Begin RHandle: = OpenService (ScmHandle , Pchar (ServiceName), Service_all_Access; if rhandle <> 0 THEN BEGIN

Rc: = DeleteService (Rhandle); Result: = Rc; {close the handle} CloseServiceHandle (RHandle); End Else Result: = True; End; Except End; end; destructor TService.Destroy; begin try CloseServiceHandle (ScmHandle); Except End ;

END;

Open handle database {} function TService.isSvcExists (ServiceName: string): Boolean; Var RHandle: Integer; rc: Boolean; begin Try OpenScmHandle (); Result: = False; If ScmHandle <> 0 Then Begin RHandle: = OpenService ( SCMHANDLE, PCHAR (SERVICENAME), Service_all_Access; if rhandle <> 0 THEN BEGIN

RESULT: = true; {Close Handle} ClosESERVICEHANDE (RHANDE); END; End;

function TService.OpenScmHandle: Integer; begin try SetScmInfo (); if ScmHandle <> 0 Then CloseServiceHandle (ScmHandle); ScmHandle: = 0; ScmHandle: = OpenSCManager (PChar (Scm_Info.HostName), Nil, Scm_Info.DesireAccess); if ScmHandle = 0 THEN SCMHANDLE: = OpenScManager (pchar (scm_info.hostname), nil, scm_info.desireAccess; Except end;

{Open a service} Function TService.Open_Service (ServiceName: String): boolean; var shandle: integer; begin tryle: = false; shandle: = 0; shandle: = openscmhandle (); if shandle <> 0 THEN BEGIN SVCHANDLE: = OpenService (SHANDLE, PCHAR (SERVICENAME), Service_all_Access); if Svchandle <> 0 THEN Result: = true; end;

END;

function TService.SetScmInfo (): Boolean; Var hostName: array [0..255] of char; wsdata: TWSAData; begin try WSAStartup ($ 0101, wsdata); Result: = False; gethostname (hostName, sizeof (hostName));

If Hostname = ''.

SCM_INFO.HOSTNAME: = HostName; SCM_INFO.DESIREACCESS: = SC_MANAGER_ALL_ACCESS; EXCEPT END;

/ / Set the specific information of the Windows service that needs to register

function TService.SetServiceInfo (TmpSvc: TsvcInfo): Boolean; begin try Result: = False; If ScmHandle> 0 Then Begin Svc_Info: = TmpSvc; Svc_Info.HscManager: = ScmHandle; Result: = True; End; Except End;

END;

/ / Control the service inside the Windows system

Function TSERVICE.CONTROL_SERVICE (ServiceName: Integer): boolean; var rhandle: integer; rc: boolean; rec_status: tServiceStatus; servicearg: pchar; i: integer;

Begin i: = 0; servicearg: = pChar ('); try results: = false; if SCMHANDE <> 0 THEN BEGIN RHANDLE: = OpenService (SCMHANDLE, PCHAR (ServiceName), service_all_access); if rhandle <> 0 THEN Begin Case controlType of 1: // start the server Begin // ControlService (rHandle, SERVICE_CONTROL_START, rec_status); StartService (rHandle, 0, servicearg); i: = 0; While i <30 Do Begin QueryServiceStatus (RHandle, rec_status); if rec_status .dwCurrentState = SERVICE_RUNNING then Begin result: = True; CloseServiceHandle (rHandle); Break; End; sleep (1000); i: = i 1; End; End; 2: // suspend service process Begin ControlService (rHandle, SERVICE_CONTROL_PAUSE, REC_STATUS); i: = 0; While i <30 do begin QueryServiceStatus (RHandle, Rec_status); if rec_status.dwCurrentState = SERVICE_PAUSED then Begin result: = true; CloseServiceHandle (RHandle); Break; End; Sleep (1000); i: = i 1; End; End; 3: // wakeup Services Begin ControlService (Rthila, Service_Control_Continue, REC_STATUS); i: = 0; While I <30 DO Begin QueryServiceStatus; if REC_STATUS.DWCURRENTSTATE =

Service_running the beginning: = true; closeServiceHandle (RHANDLE); BREAK; END; SLEEP (1000); i: = i 1; end; end; 4: // Stop service process Begin ControlService (rhandle, service_control_stop, r_status); i: = 0; While i <30 Do Begin QueryServiceStatus (RHandle, Rec_status); if rec_status.dwCurrentState = SERVICE_STOPPED then Begin result: = True; CloseServiceHandle (RHandle); Break; End; Sleep (1000); i: = i 1; end; end; 5: // Close Service Service_Control_Shutdown Begin Rec_status.dwcurrentState: = service_running; controlservice (RHANDLE, Service_Control_Stop, REC_STATUS); i: = 0; While i <30 do Begin QueryServiceStatus (RHandle, Rec_status); if rec_status.dwCurrentState = SERVICE_STOPPED then Begin result: = True; CloseServiceHandle (RHandle); Break; End; Sleep (1000); i: = i 1; End; End; end; Result: = True; {Close handle} if rhandle <> 0 Then ClosESERVICEHANDLE (RHANDLE); END; End; Except End;

END;

function TService.IsServiceStart (ServiceName: String): Boolean; Var RHandle: Integer; rc: Boolean; Rec_status: TServiceStatus; begin Try OpenScmHandle (); Result: = False; If ScmHandle <> 0 Then Begin RHandle: = 0; RHandle: = OpenService (ScmHandle, PChar (ServiceName), SERVICE_ALL_ACCESS); If RHandle <> 0 Then Begin QueryServiceStatus (RHandle, Rec_status); if rec_status.dwCurrentState = SERVICE_RUNNING then result: = True; {close the handle} CloseServiceHandle (RHandle); End; End; Except end;

END;

End.

转载请注明原文地址:https://www.9cbs.com/read-29024.html

New Post(0)