Accueil > > > GERER LES SERVICES DE WINDOWS AVEC WINSVC
GERER LES SERVICES DE WINDOWS AVEC WINSVC
Information sur la source
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 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.
{
*----------------------------------------------*
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 !
Historique
- 21 novembre 2006 16:36:52 :
- Petite erreur !
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Les Api Windows [ par twoupy ]
Est-ce que quelqu'un possède ou sait où je pourrais trouver l'aide sur les Api Windows (win32.hlp) en français. ce serait bien cool, parce que l'angl
documentation API [ par Noureddine ]
Bonjour,je cherche la documentation pour comprendre L'API, si vous avez une adresse ou je peut télécharger une documentation en français SVPMerci.
problème avec une api [ par fabiin ]
SalutEst-ce que kelk'un rencontre un problème lors de l'utilisation desetDCbrushcoloren Delphi 6Merci par avance@+Fabse
Comment utiliser une API avec Delphi 5? [ par Manthis ]
Salut,Je débute tout juste en Delphi 5. J'avais commencer par le VB. Et donc voila j'ai un problème comment utiliser une API avec Delphi?Ou doit-on la
API sndPlaySoundA [ par jlg75 ]
j'utilise l'API 'sndPlaySoundA' tirée de 'winmm.dll' pour lire des .wav dans un prog DELPHI. Je déclare explicitement cet API comme fonction 'external
CHERCHE TUT DELPHI API [ par golum ]
Salut je suis a la recherche d'un tut Delphi et API un peu comme EstDev pour VBAuriez vous quelque chose ? des adresses ?
Winsock [ par SMoG ]
Yop... Je desespere de trouver un jour de la doc sur l'api winsock avec des exemples delphi...Si qqn pouvait m'expliquer comment deux machines se con
API msn messenger [ par achovovich ]
BonjourJe voudrai creer un add on pour msn messenger. Je sais ke ceci es faisable en VB et bcp d'exemples existent mais en delphi, rien. Je ne sais me
fenetres bizzarres [ par ak47 ]
bonjour a tous,J'ai lu dans un article qu'on pouvait faire des fenetres "bizarres" (de part leurs formes) grace a une api de windows. Malheureusement,
Fonction API GetOpenFileName ??? [ par PhGORMAND ]
Salut à tous.Je cherche à utiliser la fonction API GetOpenFileName, mais je ne parvient pas à l'utiliser.Dans le code ci dessous, je fais appel à la f
|
Derniers Blogs
UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|