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 :

Lier la fermeture de 2 applis indépendantes


Sujet :

C++Builder

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé Avatar de Revan777
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    87
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 87
    Par défaut Lier la fermeture de 2 applis indépendantes
    Bonjour,
    j'ai besoin de contrôler le nombre d'instances exécutées d'une application "CSAuto" (il n'en faut qu'UNE seule). N'ayant pas accès au code source de l'appli CSAuto, je dois développer une autre application qui exécutera CSAuto et l'empêchera d'être ouvert plusieurs fois. De même, elle devra fermer les 2 si une des 2 est fermée.

    J'ai fait un mutex sur mon appli qui exécute CSAuto donc je contrôle bien le nombre d'instances.

    J'exécute CSAuto dans le constructeur de l'appli :
    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
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
          /*  ShellExecute(NULL,      // Handle de la fenêtre parent
                     "open",        // Action à effectuer
                     "CSAuto.exe", // Fichier
                     "",            // Paramètres
                     "C:\\yep",     // Répertoire par défaut
                     SW_SHOWDEFAULT // Manière d'afficher
                     );       */
     
            system("start c:\\yep\\CSAuto.exe");
           // system("c:\\yep\\CSAuto.exe");
     
            HWND h = FindWindow("CSAuto", NULL);
            // ......
     
          //  Application->Minimize();
          //  Application->Terminate();
    }
    Je ne sais pas encore quelle est la meilleur façon de l'exécuter, sachant que system affiche une fenêtre de commandes gênante mais permet d'attendre la destruction de CSAuto avant de se fermer.

    Maintenant il s'agit de contrôler les fermetures des programmes. Comment forcer la fermeture d'un programme indépendant d'un autre ? Il ne faut pas qu'on puisse fermer mon appli et laisser ouvert CSAuto et inversement...
    Peut-être en récupérant le Handle de CSAuto mais comment le trouver ?
    J'ai essayé ceci mais h retourne 0 :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    HWND h = FindWindow("CSAuto", NULL);
    Merci par avance.

  2. #2
    Membre Expert
    Avatar de bakaneko
    Profil pro
    Inscrit en
    Février 2004
    Messages
    1 268
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Février 2004
    Messages : 1 268
    Par défaut
    Pour lancer ton CSAuto, je passerais plus par un ShellExecute.
    The ShellExecute function opens or prints a specified file. The file can be an executable file or a document file. See ShellExecuteEx also.

    HINSTANCE ShellExecute(

    HWND hwnd, // handle to parent window
    LPCTSTR lpOperation, // pointer to string that specifies operation to perform
    LPCTSTR lpFile, // pointer to filename or folder name string
    LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters
    LPCTSTR lpDirectory, // pointer to string that specifies default directory
    INT nShowCmd // whether file is shown when opened
    );


    Parameters

    hwnd

    Specifies a parent window. This window receives any message boxes that an application produces. For example, an application may report an error by producing a message box.

    lpOperation

    Pointer to a null-terminated string that specifies the operation to perform. The following operation strings are valid:

    String Meaning
    "open" The function opens the file specified by lpFile. The file can be an executable file or a document file. The file can be a folder to open.
    "print" The function prints the file specified by lpFile. The file should be a document file. If the file is an executable file, the function opens the file, as if "open" had been specified.
    "explore" The function explores the folder specified by lpFile.


    The lpOperation parameter can be NULL. In that case, the function opens the file specified by lpFile.

    lpFile

    Pointer to a null-terminated string that specifies the file to open or print or the folder to open or explore. The function can open an executable file or a document file. The function can print a document file.

    lpParameters

    If lpFile specifies an executable file, lpParameters is a pointer to a null-terminated string that specifies parameters to be passed to the application.
    If lpFile specifies a document file, lpParameters should be NULL.

    lpDirectory

    Pointer to a null-terminated string that specifies the default directory.

    nShowCmd

    If lpFile specifies an executable file, nShowCmd specifies how the application is to be shown when it is opened. This parameter can be one of the following values:

    Value Meaning
    SW_HIDE Hides the window and activates another window.
    SW_MAXIMIZE Maximizes the specified window.
    SW_MINIMIZE Minimizes the specified window and activates the next top-level window in the Z order.
    SW_RESTORE Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
    SW_SHOW Activates the window and displays it in its current size and position.
    SW_SHOWDEFAULT Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. An application should call ShowWindow with this flag to set the initial show state of its main window.
    SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window.
    SW_SHOWMINIMIZED Activates the window and displays it as a minimized window.
    SW_SHOWMINNOACTIVE Displays the window as a minimized window. The active window remains active.
    SW_SHOWNA Displays the window in its current state. The active window remains active.
    SW_SHOWNOACTIVATE Displays a window in its most recent size and position. The active window remains active.
    SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.


    If lpFile specifies a document file, nShowCmd should be zero.



    Return Values

    If the function succeeds, the return value is the instance handle of the application that was run, or the handle of a dynamic data exchange (DDE) server application.
    If the function fails, the return value is an error value that is less than or equal to 32. The following table lists these error values:

    Value Meaning
    0 The operating system is out of memory or resources.
    ERROR_FILE_NOT_FOUND The specified file was not found.
    ERROR_PATH_NOT_FOUND The specified path was not found.
    ERROR_BAD_FORMAT The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).
    SE_ERR_ACCESSDENIED The operating system denied access to the specified file.
    SE_ERR_ASSOCINCOMPLETE The filename association is incomplete or invalid.
    SE_ERR_DDEBUSY The DDE transaction could not be completed because other DDE transactions were being processed.
    SE_ERR_DDEFAIL The DDE transaction failed.
    SE_ERR_DDETIMEOUT The DDE transaction could not be completed because the request timed out.
    SE_ERR_DLLNOTFOUND The specified dynamic-link library was not found.
    SE_ERR_FNF The specified file was not found.
    SE_ERR_NOASSOC There is no application associated with the given filename extension.
    SE_ERR_OOM There was not enough memory to complete the operation.
    SE_ERR_PNF The specified path was not found.
    SE_ERR_SHARE A sharing violation occurred.


    Remarks

    The file specified by the lpFile parameter can be a document file or an executable file. If the file is a document file, the ShellExecute function opens or prints it, depending on the value of the lpOperation parameter. If the file is an executable file, the ShellExecute function opens it, even if lpOperation specifies printing.
    You can use ShellExecute to open or explore a shell folder. To open a folder, use either of the following calls:

    ShellExecute(handle, NULL, path_to_folder, NULL, NULL, SW_SHOWNORMAL);

    or


    ShellExecute(handle, "open", path_to_folder, NULL, NULL, SW_SHOWNORMAL);


    To explore a folder, use the following call:

    ShellExecute(handle, "explore", path_to_folder, NULL, NULL, SW_SHOWNORMAL);


    If lpOperation is NULL, the function opens the file specified by lpFile. If lpOperation is "open" or "explore", the function will force an open window or explorer.

    See Also

    FindExecutable, ShellExecuteEx
    En plus de ne pas faire apparaitre de fenetre de commande, tu pourras récupérer le HINSTANCE du programme exécuté...

  3. #3
    Membre émérite
    Avatar de bandit boy
    Profil pro
    Inscrit en
    Février 2006
    Messages
    916
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 916
    Par défaut
    Perso, je cacherais CSAuto dans un endroit où l'utilisateur n'ira pas le chercher, ensuite je lancerais ce logiciel comme bakaneko l'a dit, et je mettrais le mutex sur mon appli.

    Pour que l'utilisateur n'y voit que du feu, je mettrais même la même icone d'exe de manière à ce qu'il ait l'impression de le lancer. Si tu veux être sur qu'il n'y a pas d'autre instance de ton logiciel de lancer, tu peux lancer au démarrage de ton application une recherche si CSAuto est en cours de fonctionnement avec la fonction FindWindow.

    De la même manière, si tu veux fermer ton application en même temps que CSAuto, tu dois pouvoir récupérer le Handle de l'application que tu lances et tu le test pour savoir si elle est encore en cours, si elle ne l'est plus tu fermes ton application automatiquement.

  4. #4
    Membre confirmé Avatar de Revan777
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    87
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 87
    Par défaut
    Ok merci pour les infos mais comment faire pour tester si l'application est en cours d'exécution avec son Handle ?

  5. #5
    Membre expérimenté

    Profil pro
    Inscrit en
    Janvier 2003
    Messages
    288
    Détails du profil
    Informations personnelles :
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Janvier 2003
    Messages : 288
    Par défaut
    Le principe est d'ouvrir un thread qui ecoute ce qui se passe sur CSauto. Je te livre la théorie, mais pour le détail c'est un peu sioux.

    Dans ton thread tu as une boucle (WaitForSingleObject ou WaitForMutlipleObject) dans laquelle tu ecoute les événements qui surviennent dans l'application. Tu peux ainsi savoir si un WM_CLOSE a été demandé. Dès lors tu n'as plus qu'à te fermer toi-même.

    Fait un google search "waitforsingleobject wm_close" et tu trouveras ton bonheur.

    Pour fermer dans l'autre sens (ton appli qui ferme la première) je ne sais pas comment faire exactement mais c'est certainement très simple. Regarde dans le Msdn les fonctions windows qui prennent un process handle en paramètre.

  6. #6
    Membre confirmé Avatar de Revan777
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    87
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 87
    Par défaut
    Je pensais cacher mon appli une fois exécutée (en l'enlevant de la barre des taches ou en la mettant en trayicon) pour éviter que les opérateurs "ne cliquent partout pour voir ce que ça fait" ...
    Donc il faut que je me penche surtout sur la fermeture de CSAuto qui entraine celle de mon appli.
    Merci yarp pour l'info, j'ai déjà tenté l'utilisation de thread (en BTS IRIS) mais je n'ai jamais vraiment réussi à le réutiliser... c'est encore obscur pour moi... il n'y a pas un événement que je pourrais utiliser à la place ?

Discussions similaires

  1. faire une fenetre pour la fermeture de l'appli
    Par kazylax dans le forum VB.NET
    Réponses: 3
    Dernier message: 09/08/2007, 01h22
  2. FileDialog sous Linux = fermeture de l'appli !
    Par Shadok dans le forum wxPython
    Réponses: 3
    Dernier message: 10/06/2007, 14h31
  3. Fermeture d'une appli
    Par splinternabs dans le forum Windows Forms
    Réponses: 3
    Dernier message: 05/02/2007, 18h11
  4. Réponses: 3
    Dernier message: 28/08/2006, 13h00
  5. Empecher fermeture ou cacher appli gestionnaire des taches
    Par quarkz dans le forum API, COM et SDKs
    Réponses: 3
    Dernier message: 07/09/2005, 11h34

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