Précédent   Forum du club des développeurs et IT Pro > Environnements de développement > Delphi > Débutant
Débutant Pour bien débuter avec Delphi
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 08/12/2012, 11h26   #1
davidif
 
Homme
Electronicien
Inscription : août 2008
Messages : 126
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Electronicien
Secteur : High Tech - Électronique et micro-électronique

Informations forums :
Inscription : août 2008
Messages : 126
Points : -3
Points : -3
Par défaut Arrêter ou demarrer un service

Bonjour,

J’essaie actuellement de d’arrêter et démarrer le service "Mysql" sur mon PC via Delphi, j'ai donc recherché la commande mais je ne comprends pas trop comment l'utiliser :

http://msdn.microsoft.com/en-us/library/ms686321.aspx

Je dois certainement utiliser 'startservice', mais ignore comment

Code :
1
2
3
4
5
6
7
8
procedure Start_Service(Srv_Hwnd:Sc_Handle);
Var ArrayOfArguments : PChar;
 
begin
 ArrayOfArguments := nil;
 if StartService(Handle_Service, 0, ArrayOfArguments) =0
  then MessageDlg(SysErrorMessage(GetLastError), mtError, [mbOK], 0);
end;
Pourriez vous m'éclairer s'il vous plait
Merci
davidif est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/12/2012, 11h37   #2
BuzzLeclaire
Membre Expert
 
Avatar de BuzzLeclaire
 
Homme
Dev/For/Vte/Ass
Inscription : août 2008
Messages : 1 499
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Dev/For/Vte/Ass

Informations forums :
Inscription : août 2008
Messages : 1 499
Points : 1 059
Points : 1 059
@davidif

Salut, pour démarrer ou arrêter un service :

1 - assure toi d'avoir tous les privilèges (UAC)
2 - utilise ShellExecute

Avec ShellExecute :

ShellExecute(0,'NET','START','Mysql',nil,SW_HIDE);

Ou alors
ShellExecute(0,'NET','START MYSQL',nil,nil,SW_HIDE);

'Mysql' = le nom de ton service
SW_HID2 = pour ne pas voir la fenêtre DOS, regarde les autres choix que tu auras.

bye
BuzzLeclaire est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 08/12/2012, 11h59   #3
ouiouioui
Membre Expert
 
Avatar de ouiouioui
 
Homme Alexandre
Administrateur systèmes et réseaux
Inscription : août 2006
Messages : 882
Détails du profil
Informations personnelles :
Nom : Homme Alexandre
Âge : 31
Localisation : France, Rhône (Rhône Alpes)

Informations professionnelles :
Activité : Administrateur systèmes et réseaux
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : août 2006
Messages : 882
Points : 1 197
Points : 1 197
Envoyer un message via MSN à ouiouioui
faut que le processus ai les droits admin sinon avec sa ils seront demandés.

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
Var
  Sei: TShellExecuteInfo;
Begin
  Fillchar(Sei, SizeOf(Sei), 0);
  Sei.cbSize       := SizeOf(Sei);
  Sei.Wnd          := Handle;
  Sei.fMask        := SEE_MASK_FLAG_DDEWAIT Or SEE_MASK_FLAG_NO_UI;
  Sei.lpfile       := PChar('net.exe');
  Sei.lpVerb       := 'runas';
  Sei.lpParameters := PChar('stop mysql');
  Sei.nShow        := SW_HIDE;
  If Not ShellExecuteEx(@Sei) Then
    RaiseLastOSError;
__________________
Il existe 3 sortes de gens: ceux qui savent compter et ceux qui ne savent pas.
ouiouioui est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 08/12/2012, 12h53   #4
davidif
 
Homme
Electronicien
Inscription : août 2008
Messages : 126
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Electronicien
Secteur : High Tech - Électronique et micro-électronique

Informations forums :
Inscription : août 2008
Messages : 126
Points : -3
Points : -3
OK merci pour votre aide rapide, je vais essayer.
davidif est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/12/2012, 20h05   #5
davidif
 
Homme
Electronicien
Inscription : août 2008
Messages : 126
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Electronicien
Secteur : High Tech - Électronique et micro-électronique

Informations forums :
Inscription : août 2008
Messages : 126
Points : -3
Points : -3
Merci ouiouioui, ça fonctionne nikel

Bon maintenant il me reste à comprendre comment ça fonctionne

Merci encore
davidif est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/12/2012, 16h10   #6
davidif
 
Homme
Electronicien
Inscription : août 2008
Messages : 126
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Electronicien
Secteur : High Tech - Électronique et micro-électronique

Informations forums :
Inscription : août 2008
Messages : 126
Points : -3
Points : -3
Citation:
Envoyé par ouiouioui Voir le message
faut que le processus ai les droits admin sinon avec sa ils seront demandés.

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
Var
  Sei: TShellExecuteInfo;
Begin
  Fillchar(Sei, SizeOf(Sei), 0);
  Sei.cbSize       := SizeOf(Sei);
  Sei.Wnd          := Handle;
  Sei.fMask        := SEE_MASK_FLAG_DDEWAIT Or SEE_MASK_FLAG_NO_UI;
  Sei.lpfile       := PChar('net.exe');
  Sei.lpVerb       := 'runas';
  Sei.lpParameters := PChar('stop mysql');
  Sei.nShow        := SW_HIDE;
  If Not ShellExecuteEx(@Sei) Then
    RaiseLastOSError;
effectivement, sur ma session administrateur, je suis quand même obligé d'executer mon application en tant qu'administrateur et ça fonctionne, seulement sur d'autres system comme xp et même d'autres pc avec seven en admin ça ne fonctionne pas correctement.
Sur xp, il me demande de me logger en admin alors que je suis déja admin
et d'autre seven il tourne en boucle ?
Y a t-il un moyen de passer l'admin ?

Merci
davidif est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/12/2012, 16h37   #7
davidif
 
Homme
Electronicien
Inscription : août 2008
Messages : 126
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Electronicien
Secteur : High Tech - Électronique et micro-électronique

Informations forums :
Inscription : août 2008
Messages : 126
Points : -3
Points : -3
J'ai donc repris cette procedure

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
procedure services(etat: string);
 
var
Sei: TShellExecuteInfo;
 
begin
 
Fillchar(Sei, SizeOf(Sei), 0);
Sei.cbSize       := SizeOf(Sei);
//Sei.Wnd          := Handle;
Sei.fMask        := SEE_MASK_FLAG_DDEWAIT Or SEE_MASK_FLAG_NO_UI;
Sei.lpfile       := PChar('net.exe');
Sei.lpVerb       := 'runas';
Sei.lpParameters := PChar(etat);
Sei.nShow        := SW_HIDE;
If Not ShellExecuteEx(@Sei) Then
  RaiseLastOSError;
 
end;
puis dans mon code je fais comme ça :

Code :
1
2
3
4
5
6
7
8
9
10
11
     while not(etat_service=1) do begin
     services('STOP MySQL');
     etat_service:=StatusService('MySQL');
     if etat_service=1 then etat_arreter:=true;
     end;
 
     while not(etat_service=4) do begin
     services('START MySQL');
     etat_service:=StatusService('MySQL');
     if etat_service=4 then etat_demarrer:=true;
     end;
seulement sur une vm sous seven, celle-ci tourne en boucle avec conhost.exe et net.exe qui tourne en permanence dans mon gestionnaire de tache
davidif est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/12/2012, 16h40   #8
Rayek
Modérateur
 
Avatar de Rayek
 
Homme
Développeur informatique
Inscription : mars 2005
Messages : 4 992
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 39
Localisation : France, Haute Savoie (Rhône Alpes)

Informations professionnelles :
Activité : Développeur informatique
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : mars 2005
Messages : 4 992
Points : 7 732
Points : 7 732
Ou alors on utilise l'existant fait pour le traitement des services

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
var
  hSCManager: SC_HANDLE;
   hService: SC_HANDLE;
   Statut: TServiceStatus;
   tempMini: DWORD;
   CheckPoint: DWORD;
 
begin
   hSCManager := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
   hService := OpenService(hSCManager, 'Ton_service_a_stopper', SERVICE_QUERY_STATUS or SERVICE_START or SERVICE_STOP or SERVICE_PAUSE_CONTINUE);
   if hService = 0 then
   begin 
      Showmessage('Service non installé');
   end
   else
   begin
      QueryServiceStatus(hService, Statut);
      if statut.dwCurrentState = SERVICE_RUNNING then
      begin
         tempMini := Statut.dwWaitHint + 10;
         ControlService(hService, SERVICE_CONTROL_STOP, Statut);
         repeat
            CheckPoint := Statut.dwCheckPoint;
            Sleep(tempMini);
            QueryServiceStatus(hService, Statut);
         until (CheckPoint = Statut.dwCheckPoint) or
            (statut.dwCurrentState = SERVICE_PAUSED);
            // si CheckPoint = Statut.dwCheckPoint Alors le service est dans les choux
      end
      else
      begin
        Showmessage('service déjà arreté');
      end;
   end;
   CloseServiceHandle(hService);
   CloseServiceHandle(hSCManager);
end;
Les uses qui vont bien : WinSvc

Edit : Plus d'infos sur les services dans la
__________________
Modérateur Delphi Combattez la brute
Aucune réponse aux sollicitations techniques par MP

Le guide du bon forumeur :__________
[Projet en cours] Des unités pour faciliter l'utilisation d'indy : EasyIndy 1.3
Rayek est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Vieux 12/12/2012, 09h55   #9
davidif
 
Homme
Electronicien
Inscription : août 2008
Messages : 126
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Electronicien
Secteur : High Tech - Électronique et micro-électronique

Informations forums :
Inscription : août 2008
Messages : 126
Points : -3
Points : -3
Merci Rayek pour ton soft qui fonctionne correctement pour arrêter un service, mes connaissance limité en delphi font que je me demande comment redémarrer ce meme service via delphi maintenant.

si tu peux m'éclairer à ce sujet
merci
davidif est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 12/12/2012, 10h10   #10
Rayek
Modérateur
 
Avatar de Rayek
 
Homme
Développeur informatique
Inscription : mars 2005
Messages : 4 992
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 39
Localisation : France, Haute Savoie (Rhône Alpes)

Informations professionnelles :
Activité : Développeur informatique
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : mars 2005
Messages : 4 992
Points : 7 732
Points : 7 732
Dans la

donc

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
var
  hSCManager: SC_HANDLE;
   hService: SC_HANDLE;
   Statut: TServiceStatus;
   tempMini: DWORD;
   CheckPoint: DWORD;
  ArrayOfArguments : PCHAR;
 
begin
   hSCManager := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
   hService := OpenService(hSCManager, 'Ton_service_a_demarrer', SERVICE_QUERY_STATUS or SERVICE_START or SERVICE_STOP or SERVICE_PAUSE_CONTINUE);
   if hService = 0 then
   begin 
      Showmessage('Service non installé');
   end
   else
   begin
     ArrayOfArguments := nil;
     StartService(hService, 0, ArrayOfArguments);
   end;
   CloseServiceHandle(hService);
   CloseServiceHandle(hSCManager);
end;
__________________
Modérateur Delphi Combattez la brute
Aucune réponse aux sollicitations techniques par MP

Le guide du bon forumeur :__________
[Projet en cours] Des unités pour faciliter l'utilisation d'indy : EasyIndy 1.3
Rayek est actuellement connecté   Envoyer un message privé Réponse avec citation 10
Vieux 12/12/2012, 10h32   #11
davidif
 
Homme
Electronicien
Inscription : août 2008
Messages : 126
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Electronicien
Secteur : High Tech - Électronique et micro-électronique

Informations forums :
Inscription : août 2008
Messages : 126
Points : -3
Points : -3
Merci Rayek
davidif est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 18/12/2012, 19h14   #12
davidif
 
Homme
Electronicien
Inscription : août 2008
Messages : 126
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Electronicien
Secteur : High Tech - Électronique et micro-électronique

Informations forums :
Inscription : août 2008
Messages : 126
Points : -3
Points : -3
Je reviens à nouveau pour un souci sur la commande du service MySQL.
En faite celle annoncé ci_dessus fonctionne très bien sous seven 32BIT et windows xp mais pas apparemment sous seven 64bit.

Avant je faisais la manip avec la commande dos avec un fichier.bat "net stop mysql" et "net start MySQL" et pour cette commande je remarque qu'avec xp et seven32 je peux faire :

net stop MySQL
net start MySQL

sous SEVEN 64 BIT, je doit faire :

C:\windows\system32\net stop MySQL
C:\windows\system32\net START Mysql

Mais j'aimerai bien réussir à faire avec seven64 comme vous m'avez montré sous seven32 et xp.

Merci pour votre aide
davidif est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 12h31.


 
 
 
 
Partenaires

Hébergement Web