IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 Delphi Discussion :

Arrêter ou demarrer un service


Sujet :

Delphi

  1. #1
    Membre du Club
    Homme Profil pro
    Electronicien
    Inscrit en
    Août 2008
    Messages
    360
    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 : 360
    Points : 53
    Points
    53
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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

  2. #2
    Membre éprouvé Avatar de BuzzLeclaire
    Homme Profil pro
    Dev/For/Vte/Ass
    Inscrit en
    Août 2008
    Messages
    1 606
    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 606
    Points : 1 113
    Points
    1 113
    Par défaut
    @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

  3. #3
    Membre expérimenté
    Avatar de ouiouioui
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Août 2006
    Messages
    984
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    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 : 984
    Points : 1 418
    Points
    1 418
    Par défaut
    faut que le processus ai les droits admin sinon avec sa ils seront demandés.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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.

  4. #4
    Membre du Club
    Homme Profil pro
    Electronicien
    Inscrit en
    Août 2008
    Messages
    360
    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 : 360
    Points : 53
    Points
    53
    Par défaut
    OK merci pour votre aide rapide, je vais essayer.

  5. #5
    Membre du Club
    Homme Profil pro
    Electronicien
    Inscrit en
    Août 2008
    Messages
    360
    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 : 360
    Points : 53
    Points
    53
    Par défaut
    Merci ouiouioui, ça fonctionne nikel

    Bon maintenant il me reste à comprendre comment ça fonctionne

    Merci encore

  6. #6
    Membre du Club
    Homme Profil pro
    Electronicien
    Inscrit en
    Août 2008
    Messages
    360
    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 : 360
    Points : 53
    Points
    53
    Par défaut
    Citation Envoyé par ouiouioui Voir le message
    faut que le processus ai les droits admin sinon avec sa ils seront demandés.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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

  7. #7
    Membre du Club
    Homme Profil pro
    Electronicien
    Inscrit en
    Août 2008
    Messages
    360
    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 : 360
    Points : 53
    Points
    53
    Par défaut
    J'ai donc repris cette procedure

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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

  8. #8
    Modérateur
    Avatar de Rayek
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2005
    Messages
    5 235
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    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 : 5 235
    Points : 8 504
    Points
    8 504
    Par défaut
    Ou alors on utilise l'existant fait pour le traitement des services

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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

    Le guide du bon forumeur :
    __________
    Rayek World : Youtube Facebook

  9. #9
    Membre du Club
    Homme Profil pro
    Electronicien
    Inscrit en
    Août 2008
    Messages
    360
    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 : 360
    Points : 53
    Points
    53
    Par défaut
    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

  10. #10
    Modérateur
    Avatar de Rayek
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2005
    Messages
    5 235
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    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 : 5 235
    Points : 8 504
    Points
    8 504
    Par défaut
    Dans la

    donc

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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

    Le guide du bon forumeur :
    __________
    Rayek World : Youtube Facebook

  11. #11
    Membre du Club
    Homme Profil pro
    Electronicien
    Inscrit en
    Août 2008
    Messages
    360
    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 : 360
    Points : 53
    Points
    53
    Par défaut
    Merci Rayek

  12. #12
    Membre du Club
    Homme Profil pro
    Electronicien
    Inscrit en
    Août 2008
    Messages
    360
    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 : 360
    Points : 53
    Points
    53
    Par défaut
    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

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Comment arreter et demarrer un service
    Par Lounnas dans le forum C++Builder
    Réponses: 8
    Dernier message: 14/10/2007, 18h41
  2. WD11 demarrer un service windows
    Par python69 dans le forum WinDev
    Réponses: 5
    Dernier message: 23/09/2007, 00h26
  3. [Batch]Arrêter et démarrer un service
    Par hegros dans le forum Windows
    Réponses: 1
    Dernier message: 01/12/2006, 13h57
  4. Réponses: 2
    Dernier message: 01/11/2006, 21h48
  5. [D7]Demarrer un service windows desactivé
    Par goldkey dans le forum Delphi
    Réponses: 4
    Dernier message: 24/10/2006, 18h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo