Gerer les services de windows avec winsvc

Description

Ce matin par MP quelqu'un m'a demandé comment contrôler les services de Windows.
Voilà donc un petit exemple "très succint" de certaines fonctionalités de la librairie WinSVC, fourni avec Delphi. Cette librairie permet d'accéder à différentes API Windows permettant la gestion des services Windows.

Possibilité dans cette source de définir une application comme service.
Detruire un service.
Démarrer et arreter un service.

Et tous ceci sur une machine distante si vous le souhaitez.

Source / Exemple :


{

  • ----------------------------------------------*
Exemple d'utilisation de la librairie WinSVC et des services Windows. Par LEVEUGLE Damien (c) 2006 Pour Code-Source / DelphiFr.Com
  • ----------------------------------------------*
Unité à complété et à finir ! } unit ElgServiceNT; {.DATA} interface uses Windows, WinSVC; function ServiceCreate( SrvName : string; Libelle : string; Chemin : string; Machine : string = '' ) : Boolean; // function ServiceOpen( SrvName : string; Machine : string = '' ) : Cardinal; function ServiceRemove( SrvName : string; Machine : string = '' ) : Boolean; function ServiceStart ( SrvName : string; Machine : string = '' ) : Boolean; function ServiceStop ( SrvName : string; Machine : string = '' ) : Boolean; function ServiceState ( SrvName : string; Machine : string = '' ) : string; {.CODE} implementation { Ouvre un service } function ServiceOpen( SrvName : string; Machine : string = '' ) : Cardinal; var H_SC : SC_Handle; begin if ( Machine = '' ) then H_SC := OpenSCManager( nil, nil, SC_MANAGER_ALL_ACCESS ) else H_SC := OpenSCManager( PChar( Machine ), nil, SC_MANAGER_ALL_ACCESS ); Result := OpenService( H_SC, PChar( SrvName ), SC_MANAGER_ALL_ACCESS ); end; { Créé un service } function ServiceCreate( SrvName : string; Libelle : string; Chemin : string; Machine : string = '' ) : Boolean; var H_SC : SC_Handle; H_Sr : SC_Handle; begin Result := False; H_SC := ServiceOpen( SrvName, Machine ); if ( H_SC > 0 ) then begin H_Sr := CreateService( H_SC, PChar( SrvName ), PChar( Libelle ), SC_MANAGER_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, PChar( Chemin ), nil, nil, nil, nil, nil ); if ( H_Sr > 0 ) then Result := True else begin MessageBoxA( 0, PChar( 'Une erreur c''est produite à la création du service' ), PChar('Erreur'), MB_ICONWARNING ); Result := False end; CloseServiceHandle(H_Sr); CloseServiceHandle(H_SC); end; end; { Supprime un service } function ServiceRemove( SrvName : string; Machine : string = '' ) : Boolean; var SrvHandle : Cardinal; begin Result := False; SrvHandle := ServiceOpen( SrvName, Machine ); try Result := DeleteService( SrvHandle ); finally CloseServiceHandle( SrvHandle ); end; end; { Démarre un service } function ServiceStart( SrvName : string; Machine : string = '' ) : Boolean; var SrvHandle : Cardinal; ServiceArgVectors : PAnsiChar; SrvState : _SERVICE_STATUS; begin Result := False; ServiceArgVectors := nil; SrvHandle := ServiceOpen( SrvName, Machine ); try Result := ( StartService( SrvHandle, 0, ServiceArgVectors ) ); finally CloseServiceHandle( SrvHandle ); end; end; { Arrête un service } function ServiceStop( SrvName : string; Machine : string = '' ) : Boolean; var SrvHandle : Cardinal; ServiceArgVectors : PAnsiChar; SrvState : _SERVICE_STATUS; begin Result := False; ServiceArgVectors := nil; SrvHandle := ServiceOpen( SrvName, Machine ); try Result := ControlService( SrvHandle, SERVICE_CONTROL_STOP, SrvState ); (* Si çà vous interesse, les différents autres status sont : - SERVICE_CONTROL_STOP - SERVICE_CONTROL_PAUSE - SERVICE_CONTROL_CONTINUE - SERVICE_CONTROL_INTERROGATE - SERVICE_CONTROL_SHUTDOWN
  • )
finally CloseServiceHandle( SrvHandle ); end; end; { Renvoi l'etat actuel du service } function ServiceState( SrvName : string; Machine : string = '' ) : string; var SrvHandle : Cardinal; SrvState : _SERVICE_STATUS; begin SrvHandle := ServiceOpen( SrvName, Machine ); try if not ( QueryServiceStatus( SrvHandle, SrvState ) ) then Result := 'Le service est inexistant !' else begin case ( SrvState.dwCurrentState ) of SERVICE_CONTINUE_PENDING : Result := 'Le service est en train d''être relancé après une opération continue'; SERVICE_PAUSE_PENDING : Result := 'le service est en train d''être relancé après une opération pause'; SERVICE_PAUSED : Result := 'Le service est en pause'; SERVICE_RUNNING : Result := 'Le service est démarré'; SERVICE_START_PENDING : Result := 'Le service est en cours de démarrage'; SERVICE_STOP_PENDING : Result := 'Le service est en cours d''arrêt'; SERVICE_STOPPED : Result := 'Le service est stoppé'; else Result := 'Etat du service inconnu ou service inexistant !'; end; end; finally CloseServiceHandle( SrvHandle ); end; end; end.

Conclusion :


Notes, questions, commentaires, et insultes sont les bienvenus !

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.