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

C++Builder Discussion :

Comment arreter et demarrer un service


Sujet :

C++Builder

  1. #1
    Candidat au Club
    Inscrit en
    Août 2005
    Messages
    4
    Détails du profil
    Informations forums :
    Inscription : Août 2005
    Messages : 4
    Points : 2
    Points
    2
    Par défaut Comment arreter et demarrer un service
    Bonjour,
    Je souhaite savoir comment arreter et demarrer un service externe à l'application (Par exemple le service plug & play .. ou autre).
    Serveur Win 2000
    Langage : BCB 4 ou 5
    Merci d'avance de votre aide

  2. #2
    Membre expérimenté Avatar de 10_GOTO_10
    Profil pro
    Inscrit en
    Juillet 2004
    Messages
    886
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2004
    Messages : 886
    Points : 1 526
    Points
    1 526
    Par défaut
    Avec la fonction Windows StartService

    StartService
    The StartService function starts a service.

    BOOL StartService(
    SC_HANDLE hService, // handle of service
    DWORD dwNumServiceArgs, // number of arguments
    LPCTSTR *lpServiceArgVectors // array of argument strings
    // string pointers
    );

    Parameters
    hService
    Handle to the service. This handle is returned by the OpenService or CreateService function, and it must have SERVICE_START access.
    dwNumServiceArgs
    Specifies the number of argument strings in the lpServiceArgVectors array. If lpServiceArgVectors is NULL, this parameter can be zero.
    lpServiceArgVectors
    Pointer to an array of pointers that point to null-terminated argument strings passed to a service. Driver services do not receive these arguments. If no arguments are passed to the service being started, this parameter can be NULL. The service accesses these arguments through its ServiceMain function. The first argument (argv[0]) is the name of the service by default, followed by the arguments, if any, in the lpServiceArgVectors array.
    Return Values
    If the function succeeds, the return value is nonzero.

    If the function fails, the return value is zero. To get extended error information, call GetLastError.

  3. #3
    Candidat au Club
    Inscrit en
    Août 2005
    Messages
    4
    Détails du profil
    Informations forums :
    Inscription : Août 2005
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Merci pour la rapidite de ta reponse.
    Mais je ne sais pas comment passer les arguments.
    Ou dois je indiquer le nom du service ou le nom de l'executable qui est lancé, que cela soit pour la fonction StartService ou ControlService.
    Si tu pouvais me donner un exemple pour ces fonctions cela m'aiderait beaucoup.
    Merci.

  4. #4
    Membre expérimenté Avatar de 10_GOTO_10
    Profil pro
    Inscrit en
    Juillet 2004
    Messages
    886
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2004
    Messages : 886
    Points : 1 526
    Points
    1 526
    Par défaut
    Syntaxe non garantie à 100%, mais c'est quelque chose comme ça:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    char szServiceName[] = "MonService";
    SC_HANDLE hSCManager, hService;
     
    hSCManager = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);
    hService = OpenService(hSCManager, szServiceName, SERVICE_ALL_ACCESS);
    // Démarrage du service
    StartService(hService, 0, NULL);
    if (hService) CloseServiceHandle(hService);
    CloseServiceHandle(hSCManager);

  5. #5
    Candidat au Club
    Inscrit en
    Août 2005
    Messages
    4
    Détails du profil
    Informations forums :
    Inscription : Août 2005
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    La fonction pour demarrer le service fonctionne !
    Par contre j'ai essayé d'adapter ton code pour arreter le service avec les commandes suivantes mais cela ne marche pas, pourrais tu me dire ou je me plante.
    Merci.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    hSCManager = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);
    hService = OpenService(hSCManager, szServiceName, SERVICE_ALL_ACCESS);
     // Arret du service
    ControlService(hService, SERVICE_CONTROL_STOP, NULL);
    if (hService) CloseServiceHandle(hService);
    CloseServiceHandle(hSCManager);

  6. #6
    Membre expérimenté Avatar de 10_GOTO_10
    Profil pro
    Inscrit en
    Juillet 2004
    Messages
    886
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2004
    Messages : 886
    Points : 1 526
    Points
    1 526
    Par défaut
    Parce que le dernier argument de ControlService ne doit pas être NULL:

    ControlService
    The ControlService function sends a control code to a Win32-based service.

    BOOL ControlService(
    SC_HANDLE hService, // handle to service
    DWORD dwControl, // control code
    LPSERVICE_STATUS lpServiceStatus
    // pointer to service status structure
    );

    Parameters
    hService
    Handle to the service. This handle is returned by the OpenService or CreateService function. The access required for this handle depends on the dwControl code requested.
    dwControl
    Specifies the requested control code. This value can be one of the standard control codes in the following table: Value Meaning

    SERVICE_CONTROL_STOP Requests the service to stop. The hService handle must have SERVICE_STOP access.
    SERVICE_CONTROL_PAUSE Requests the service to pause. The hService handle must have SERVICE_PAUSE_CONTINUE access.
    SERVICE_CONTROL_
    CONTINUE Requests the paused service to resume. The hService handle must have SERVICE_PAUSE_CONTINUE access.
    SERVICE_CONTROL_
    INTERROGATE Requests the service to update immediately its current status information to the service control manager. The hService handle must have SERVICE_INTERROGATE access.
    SERVICE_CONTROL_
    SHUTDOWN The ControlService function fails if this control code is specified.


    This value can also be a user-defined control code, as described in the following table: Value Meaning
    Range 128 to 255. The service defines the action associated with the control code. The hService handle must have SERVICE_USER_DEFINED_CONTROL access.



    lpServiceStatus
    Pointer to a SERVICE_STATUS structure to receive the latest service status information. The information returned reflects the most recent status that the service reported to the service control manager.
    Return Values
    If the function succeeds, the return value is nonzero.

    If the function fails, the return value is zero. To get extended error information, call GetLastError.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    SERVICE_STATUS ServStat;
     
    ControlService(hService, SERVICE_CONTROL_INTERROGATE, &ServStat);
    if (ServStat.dwCurrentState != SERVICE_STOPPED) {
      // Arrêt du service
      ControlService(hService, SERVICE_CONTROL_STOP, &ServStat);
    }

  7. #7
    Candidat au Club
    Inscrit en
    Août 2005
    Messages
    4
    Détails du profil
    Informations forums :
    Inscription : Août 2005
    Messages : 4
    Points : 2
    Points
    2
    Par défaut Ca marche
    Ca marche sans aucun probleme.
    Merci beaucoup pour ton aide.
    Bonne journée.

  8. #8
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Janvier 2003
    Messages
    70
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2003
    Messages : 70
    Points : 38
    Points
    38
    Par défaut
    salut

    Il y a une possibiliter de passer le service en manuel ??

    merci
    Julien
    @+ JujuBois

  9. #9
    Membre confirmé Avatar de winow
    Inscrit en
    Novembre 2004
    Messages
    668
    Détails du profil
    Informations personnelles :
    Âge : 58

    Informations forums :
    Inscription : Novembre 2004
    Messages : 668
    Points : 628
    Points
    628
    Par défaut
    Bonjour
    Moi, pour passer un service en manuel, j'utilise le registre

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    //infos et ecriture Aide et Support
    svcsreg->RootKey = HKEY_LOCAL_MACHINE;
    svcsreg->OpenKey("SYSTEM\\ControlSet003\\Services\\helpsvc", false);//chemin
    svcsreg->WriteInteger("Start", 3);//mettre en manuel
    Label2->Caption = svcsreg->ReadString("DisplayName");//nom du service
    Label4->Caption = svcsreg->ReadInteger("Start");//savoir si en manuel
    if(Label4->Caption == 2)//convertir 2 en automatique
    Label4->Caption = "Automatique";
    if(Label4->Caption == 3)//convertir 3 en Manuel
    Label4->Caption = "Manuel";
    if(Label4->Caption == 4)//convertir 4 en désactiver
    Label4->Caption = "Désactiver";
    delete svcsreg;
    un peut barbare mais bon, ça marche

    A+
    .
    Why
    .
    //------------------

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

Discussions similaires

  1. Réponses: 2
    Dernier message: 01/11/2006, 21h48
  2. Réponses: 3
    Dernier message: 30/08/2005, 09h09
  3. comment on peut faire un service avec builder c++
    Par infoactif dans le forum C++Builder
    Réponses: 8
    Dernier message: 11/08/2005, 17h33
  4. Comment arreter le rafraichissement d'une page avec un popup
    Par Wanty dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 11/08/2005, 14h44
  5. [Thread] comment arreter un thread qui execute une methode b
    Par Cyber@l dans le forum Concurrence et multi-thread
    Réponses: 8
    Dernier message: 04/08/2004, 10h51

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